0

我正在尝试在 Script Lab for Excel 中使用 TypeScript 将活动工作表复制到新的空白工作簿。我在下面找到了一个示例脚本,该脚本将工作表复制到同一个工作簿,但不确定如何将其复制到新的空白工作簿。有任何想法吗?

$("#setup").click(() => tryCatch(setup));
$("#copy-worksheet").click(() => tryCatch(run));

async function run() {
  await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy("End");

    sampleSheet.load("name");
    copiedSheet.load("name");

    await context.sync();

    console.log("'" + sampleSheet.name + "' was copied to '" + copiedSheet.name + "'");
  });
}

async function setup() {
  await Excel.run(async (context) => {
    context.workbook.worksheets.getItemOrNullObject("Sample").delete();
    const sheet = context.workbook.worksheets.add("Sample");

    let expensesTable = sheet.tables.add("A1:E1", true);
    expensesTable.name = "SalesTable";

    expensesTable.getHeaderRowRange().values = [["Product", "Qtr1", "Qtr2", "Qtr3", "Qtr4"]];

    expensesTable.rows.add(null, [
      ["Frames", 5000, 7000, 6544, 4377],
      ["Saddles", 400, 323, 276, 651],
      ["Brake levers", 12000, 8766, 8456, 9812],
      ["Chains", 1550, 1088, 692, 853],
      ["Mirrors", 225, 600, 923, 544],
      ["Spokes", 6005, 7634, 4589, 8765]
    ]);

    sheet.getUsedRange().format.autofitColumns();
    sheet.getUsedRange().format.autofitRows();

    sheet.activate();
    await context.sync();
  });
}

/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
  try {
    await callback();
  } catch (error) {
    // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
    console.error(error);
  }
}
4

1 回答 1

0

它受支持,您可以在此处查看https://docs.microsoft.com/en-us/javascript/api/excel/excel.worksheet?view=excel-js-preview#copy_positionType__relativeTo _

于 2021-05-24T03:28:32.757 回答