Skip to content

Add a worksheet

This article explains how to add a worksheet from Office Scripts, with sample code.

Use the addWorksheet method of the Workbook object.

Type definition of addWorksheet
/**
* @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.

This sample specifies a sheet name and creates a worksheet.

Sample using addWorksheet
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);
}