Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Returns the sum of each row in a multiple column dataset.
- *
- * @param {A2:D10} data The input data
- * @return {array} sums.
- * @customfunction
- */
- function SUM_EACH_ROW(data) {
- const results = []
- data.forEach(row => {
- let n = 0
- row.forEach(col => {
- if (typeof col == 'number') {
- n += col
- }
- })
- if (n == 0) {
- results.push([""])
- } else {
- results.push([n])
- }
- })
- return results
- }
- /**
- * Returns the sum of each column with column headers.
- *
- * @param {A1:D10} data The input data (including the headers)
- * @return {array} sums.
- * @customfunction
- */
- function SUM_EACH_COL(data){
- const resultsObj = {}
- data[0].forEach((header, i) => {
- resultsObj[i] = {header, sum: 0}
- })
- data.unshift()
- data.forEach((row) => {
- row.forEach((col, i) => {
- if (typeof col == 'number') {
- resultsObj[i].sum += col
- }
- })
- })
- return Object.values(resultsObj).map(res => {
- const { header, sum } = res
- return [header, sum]
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment