Skip to content

Instantly share code, notes, and snippets.

@akrantz
Last active April 25, 2022 08:02
Show Gist options
  • Save akrantz/86a4902b3cd60888b8391c792138634a to your computer and use it in GitHub Desktop.
Save akrantz/86a4902b3cd60888b8391c792138634a to your computer and use it in GitHub Desktop.
Implement the SUM() function in JavaScript.
name: Custom Function SUM()
description: Implement the SUM() function in JavaScript.
host: EXCEL
api_set: {}
script:
content: |
/**
* The sum of all of the numbers.
* @customfunction
* @param number1 A number (such as 1 or 3.1415), a cell address (such as A1 or $E$11), or a range of cell addresses (such as B3:F12).
*/
function sum(number1: number[][][]): number {
return sumOf(number1);
/**
* The sum of a number is simply that number;
* the sum of an array is the sum of the elements;
* the sum of an unsupported type is zero.
*/
function sumOf(value: any): number {
if (typeof value == "number") {
return value;
} else if (Array.isArray(value)) {
return value.reduce<number>((previous, current) => previous + sumOf(current), 0);
} else {
return 0;
}
}
}
/**
* The sum of all of the numbers.
* @customfunction
* @param number1 A number (such as 1 or 3.1415), a cell address (such as A1 or $E$11), or a range of cell addresses (such as B3:F12).
*/
function sum2(number1: number[][][]): number {
let total: number = 0;
number1.forEach(range => {
range.forEach(row => {
row.forEach(num => {
total += num;
});
});
});
return total;
}
language: typescript
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css
[email protected]/client/core.min.js
@types/core-js
@microsoft/[email protected]/dist/office.helpers.min.js
@microsoft/[email protected]/dist/office.helpers.d.ts
[email protected]
@types/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment