try {
await Excel.run(async (context) => {
var array = [
[0, 1, 1, 1],
[0, 0, 1, 0],
[0, 0, 1, 0],
];
// Method 1 (write formula and read back)
// const sheet = context.workbook.worksheets.getActiveWorksheet();
// const range = sheet.getRange("A1").getResizedRange(array.length - 1, array[0].length - 1)
// range.values = array;
// const sumRange = sheet.getRange("F2");
// sumRange.formulas = [["=SUM(A1:D5)"]]
// sumRange.load("values")
// await context.sync();
// OfficeHelpers.UI.notify("Sum", sumRange.values[0][0].toString())
// Method 2 (use Excel as calculation engine)
// const sheet = context.workbook.worksheets.getActiveWorksheet();
// const range = sheet.getRange("A1").getResizedRange(array.length - 1, array[0].length - 1)
// range.values = array;
// const sum = context.workbook.functions.sum(range).load("value")
// await context.sync();
// OfficeHelpers.UI.notify("Sum", sum.value.toString());
// Method 3: (calculate in-place via plain JS)
// let sum = 0;
// array.forEach(row => {
// row.forEach(cell => {
// sum = sum + cell;
// });
// })
// console.log(sum)
// Method 3B:
// let sum = 0;
// array.forEach(row => row.forEach(cell => sum = sum + cell))
// console.log(sum)
});
}
catch (error) {
OfficeHelpers.UI.notify(error);
OfficeHelpers.Utilities.log(error);
}