Excel macro Flashcards
Flashcard 1
Front:
What is the main function in Excel Script and how is it structured?
Back:
–function main(workbook: ExcelScript.Workbook) { … }
–This is the entry point of all Excel Scripts
–workbook gives access to the current Excel file
–All operations are performed within this function block
Flashcard 2
Front:
How do you add a worksheet using Excel Script?
Back:
–let ws = workbook.addWorksheet(“SheetName”)
–Adds a new worksheet with a custom name
–Returns a Worksheet object that you can assign to a variable
–Name must be unique in the workbook
Flashcard 3
Front:
How do you select and modify a cell range in Excel Script?
Back:
–Use worksheet.getRange(“A1:D1”) to access a range
–.setValues([[…values]]) sets the contents of that range
–The array must be 2D, even for a single row
–Example: [“ID”, “Product”, “Sale”, “Tax”] becomes [[…]]
Flashcard 4
Front:
How do you create a table in Excel Script and what do its arguments mean?
Back:
–workbook.addTable(range, hasHeaders)
–range: a Range object (e.g., worksheet.getRange(“A1:D1”))
–hasHeaders: true if first row contains headers
–Returns a Table object
–Useful for dynamic data manipulation and formatting
Flashcard 5
Front:
How do you loop to create multiple worksheets in Excel Script?
Back:
–Use a for-loop: for (var i = 1; i <= 10; i++)
–workbook.addWorksheet(“Day” + i) creates sheets named “Day1” to “Day10”
–Each iteration can be used to set up headers, tables, etc.
Flashcard 6
Front:
How do you retrieve a worksheet by its name dynamically?
Back:
–Use: workbook.getWorksheet(“Day” + i)
–Useful when looping through multiple similarly named sheets
–Variable i changes per loop iteration
–Returns a Worksheet object for manipulation
Flashcard 7
Front:
How do you read a value from a specific cell in Excel Script?
Back:
–Syntax: worksheet.getCell(row, col).getValue()
–row and col are zero-based (e.g., A1 = row 0, col 0)
–Returns the value stored in that cell
–Common use: read control values from a master sheet
Flashcard 8
Front:
How do you delete a worksheet programmatically in Excel Script?
Back:
–Retrieve the sheet with workbook.getWorksheet(“Name”)
–Call .delete() on the worksheet object
–Example:
code;
let ws = workbook.getWorksheet(“Day1”)
ws.delete()
Flashcard 9
Front:
How do you determine how many worksheets to delete or create dynamically?
Back:
–Store number in a cell: e.g., Sheet1!C1 (row 0, col 2)
–Retrieve with: workbook.getWorksheet(“Sheet1”).getCell(0, 2).getValue()
–Use that value as a loop limit in a for-loop