I'm trying to use TypeScript in Script Lab for Excel to copy the active worksheet to a new blank workbook. I've found a sample script below that copies the worksheet to the same workbook, but not sure how to have it copy to a new blank workbook instead. Any ideas?
$("#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);
}
}
It is supported, you can check here https://learn.microsoft.com/en-us/javascript/api/excel/excel.worksheet?view=excel-js-preview#copy_positionType__relativeTo_