The active worksheet is the sheet currently selected in the Excel interface. Returns: ExcelScript.Worksheet Reference the active worksheet Purpose: Represents the collection of worksheets associated with the workbook. Assign the second worksheet to the ws variable let ws = workbook.getWorksheets() About getWorksheets Assign the first worksheet to the ws variable getWorksheets is the method used to obtain the collection of all worksheets within the workbook.Īs Office Scripts has a zero-base, the first worksheet is, the second worksheet is, and so on. To reference a worksheet by position, we use its number within the worksheets collection. Returns: ExcelScript.Worksheet Reference a single worksheet by position Purpose: Gets a worksheet using its name or ID.
Let ws = workbook.getWorksheet( "Sheet1") About getWorksheet Assign the "Sheet1" worksheet to the ws variable The parameter within getWorksheet must be the text string containing the worksheet name.
The following code assigns a single worksheet called Sheet1 to the ws variable using the getWorksheet method. So, what we learn here will be used over and over again.Īll variables in this section are declared with the name of ws and can be used in the place of any other ws variables created in the remainder of the post.
Referencing worksheets doesn’t provide any visible output however, it is the start point for most of the examples in the remainder of the post.
In this section, we look at how to reference worksheets and assign them to a variable. It is used here as a simple way of providing a result for code that would otherwise not provide any visual output. In the real world, you would be unlikely to use this approach. Some examples use the console log to output a result. This consistency should help with adapting the code for your own needs. First, variables are declared worksheets assigned to them then those variables are used. Using and adapting the code in this post should cover 99% of the actions you would ever need to use.įor most examples, the code has been structured in a standard way. The Worksheet object is huge there are so many things we could do that we just can’t cover every option. This post is intended to be a reference guide to help you build your own Office Scripts. Copy a worksheet to a specific position.Reference a single worksheet by position.