Ask Your Question
2

How can TypeScript be used to save a file in an Excel add-in?

asked 2023-01-28 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-01-05 08:00:00 +0000

qstack gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-28 11:00:00 +0000

Seen: 23 times

Last updated: Jan 05 '22