2

I got an array from a single column from google sheet like [[23],[25],[26],[89],[""],[45],[78],[62],[""],[123],[45],[68]] How can I split it with empty values [] and get sums for each sub-array like (163/185/236) or more, cuz array length may be more then 1000 including 20-50 empty values?

I modified the script from the answer. But it doesn't work in my case. I don't understand why?

function getSums(){
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sh = ss.getSheetByName('TemplateTest');
 var r = sh.getRange(1, 1, sh.getLastRow());
 var arr = r.getValues(); //log: [[23],[25],[26],[89],[""],[45],[78],[62],[""],[123],[45],[68]]
 var sums = [];
 var sum = 0;
 // iterate through the array
 for (var i = 0; i < arr.length; i ++) {
 if (arr[i].length == 0 || i == arr.length - 1) {
 // empty array, push sum to array and reset
 sums.push(sum);
 sum = 0;
 } else {
 // array has values, sum them up and add to sum
 sum += arr[i].reduce((a, b) => a + b);
 }
 }
 Logger.log(sums) //log: [16345786212345]
}

Where I'm wrong and how can I fix it?

DEMO:

function getSums(){
 //var ss = SpreadsheetApp.getActiveSpreadsheet();
 //var sh = ss.getSheetByName('TemplateTest');
 //var r = sh.getRange(1, 1, sh.getLastRow());
 var arr = [[23],[25],[26],[89],[""],[45],[78],[62],[""],[123],[45],[68]]
 //var arr = r.getValues();
 var sums = [];
 var sum = 0;
 // iterate through the array
 for (var i = 0; i < arr.length; i ++) {
 if (arr[i].length == 0 || i == arr.length - 1) {
 // empty array, push sum to array and reset
 sums.push(sum);
 sum = 0;
 } else {
 // array has values, sum them up and add to sum
 sum += arr[i].reduce((a, b) => a + b, 0);
 }
 }
 console.log(sums) //log: [16345786212345]
}
getSums();

asked Mar 28, 2020 at 16:03
5
  • Your code seems to work fine and it is logging [516] instead of [16345786212345]. I did add default value 0 in the reduce() method at last. Commented Mar 28, 2020 at 16:09
  • It works with snippet but doesn't when I'm getting an array from a column in Google Sheets. I don't understand why Commented Mar 28, 2020 at 16:31
  • The log is wrong. Try console.log(JSON.stringify(arr)) Commented Mar 28, 2020 at 16:32
  • You are right! It was wrong! [[23],[25],[26],[89],[""],[45],[78],[62],[""],[123],[45],[68]]. But I don't see that with Logger.log(arr) Commented Mar 28, 2020 at 16:40
  • Now provide a demo snippet with your problem. Commented Mar 28, 2020 at 16:48

2 Answers 2

3

You could take a sinple loop and add the values until you reach undefined as value, then push the sum to the result set and reset sum.

At the end push the final sum to the result set, too.

var data = [[23.0], [25.0], [26.0], [89.0], [""], [45.0], [78.0], [62.0], [""], [123.0], [45.0], [68.0]],
 result = [],
 sum = 0,
 value,
 i;
for (i = 0; i < data.length; i++) {
 value = data[i][0];
 if (value === "") {
 result.push(sum);
 sum = 0;
 continue;
 }
 sum += value;
}
result.push(sum);
console.log(result);

Timogavk
9091 gold badge9 silver badges27 bronze badges
answered Mar 28, 2020 at 16:13
Sign up to request clarification or add additional context in comments.

1 Comment

@Timo simple. Your demo is wrong and your simulating data is not representative. Provide a proper demo
1

Using array.reduce, It could be done very easily.

const data = [
 [23.0],
 [25.0],
 [26.0],
 [89.0],
 [],
 [45.0],
 [78.0],
 [62.0],
 [],
 [123.0],
 [45.0],
 [68.0]
];
let [result, sum] = data.reduce(
 ([arr, sum], curr) => {
 if (curr.length) sum += curr[0];
 else arr.push(sum);
 return [arr, sum];
 },
 [[], 0]
);
result.push(sum); //push last data
console.log(result);
console.log(result.join("."));

answered Mar 28, 2020 at 16:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.