Skip to main content

Worksheet copy

This section describes how to copy a specific worksheet using an Office Scripts.

Method to use

Use the Copy method contained in the Worksheet object.

Method details
/**
* @param positionType Where to place the copied sheet (select from the prepared enum)
* @param relativeTo If you select Before or After in PositionType, the target worksheet
* @return Copyed worksheet object
*/
ExcelScript.Worksheet.copy(
positionType?: WorksheetPositionType,
relativeTo?: Worksheet
): ExcelScript.Worksheet;

Both arguments can be omitted.If the argument is omitted, the copied sheet is placed at the top of the workbook.

Sample code

This is a sample code that specifies the sheet name to be copied and placed a copy sheet behind the worksheet.

If you can't find the specified sheet, throw the error.

Sample for copying a worksheet
function main(workbook: ExcelScript.Workbook) {
/** Target sheet name */
const sheetname = 'Sheet1';

/** Target worksheet */
const sheet = workbook.getWorksheet(sheetname);

if (!sheet) {
throw `There is no worksheet named ${sheetname}`;
}

const copied = sheet.copy(ExcelScript.WorksheetPositionType.after, sheet);

console.log(`copied ${sheetname}.`, `Seat name after copy: ${copied.getName()}`);
}