使用 excel office 脚本创建循环以重命名工作簿中的所有工作表

Creating a loop to rename all sheets in workbook using excel office script

这是代码:

/**
 * This sample renames a worksheet from "Sheet1" to "SALES".
 */
function main(workbook: ExcelScript.Workbook) {
  // Get a worksheet named "Sheet1".
  const sheet = workbook.getWorksheet('Sheet1');

  // Set its name to SALES.
  sheet.setName('SALES');

  // Get a worksheet named "SALES".
  //const sheet = workbook.getWorksheet('SALES');
  // Position the worksheet at the beginning of the workbook.
  sheet.setPosition(0);
}

您可以使用此代码使用 sales01、sales02 等约定重命名文件中的所有工作表

  function main(workbook: ExcelScript.Workbook) {
let worksheets : ExcelScript.Worksheet[] = workbook.getWorksheets()
let sheetsCount: number = worksheets.length
for (let i = 0; i < sheetsCount; i++) {
  worksheets[i].setName(`SALES0${i + 1}`)
}

}