Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To save a file in an Excel add-in using TypeScript, you can use the Excel.run() method to run a batch operation that includes creating a Workbook object and calling the saveAs() method. Here's an example code snippet:

Excel.run(function (context) {
  const range = context.workbook.getSelectedRange();
  const values = range.values;
  const sheetName = context.workbook.worksheets.getActiveWorksheet().name;

  const workbook = context.workbook;
  workbook.worksheets.add(sheetName).activate();
  const sheet = workbook.getActiveWorksheet();
  sheet.getRange("A1").values = values;
  workbook.saveAs("NewWorkbook.xlsx");
}, function (error) {
  console.error(error);
});

In this example, we first get the selected range of cells and its values. We then create a new worksheet in the workbook, activate it, and copy the values to the new sheet. Finally, we call the saveAs() method on the workbook object to save the changes to a new Excel file.