Add a worksheet
This article explains how to add a worksheet from Office Scripts, with sample code.
Method to use
Section titled “Method to use”Use the addWorksheet method of the Workbook object.
/** * @param name Name of the sheet to add (optional) * @return The added worksheet */ExcelScript.Workbook.addWorksheet(name?: string): Worksheet;You specify a name as the argument, and the method returns the created worksheet object. The added worksheet is placed after the existing worksheets.
If you omit the argument, Excel chooses the sheet name automatically.
To display the added worksheet, call the activate method on the returned worksheet.
Sample code
Section titled “Sample code”This sample specifies a sheet name and creates a worksheet.
function main(workbook: ExcelScript.Workbook) { /** Name of the sheet to add */ const sheetname = 'created-by-office-script';
if (workbook.getWorksheet(sheetname)) { throw `A worksheet named ${sheetname} already exists`; }
/** The created worksheet */ const created = workbook.addWorksheet(sheetname);}