Add worksheet
We will explain how to add a worksheet from the Office Scripts in conjunction with the sample code.
Method to use
Use the GetworkSheet
method provided in the Workbook
object.
GetWorksheet type definition
/**
* @param name Added sheet name
* @return Added worksheet
*/
ExcelScript.Workbook.addWorksheet(name?: string): Worksheet;
Specify the name in the argument and return the object of the worksheet created as a return value.
Sample code
This is a sample that specifies the sheet name and creates a work sheet.
Sample using getWorksheet
function main(workbook: ExcelScript.Workbook) {
/** Added sheet name */
const sheetname = 'created-by-office-script';
if (workbook.getWorksheet(sheetname)) {
throw `There is already a worksheet named ${sheetname}`;
}
/** Created worksheet */
const created = workbook.addWorksheet(sheetname);
}
tip
We are checking duplicate checks for worksheets with the same name.