I am using the code below to create a table. It works fine.
My final goal is to add a sub-total for "de-prioritized". Since I am already looping the table, I wanted to leverage the existing loop. (without looping every value in the array which I am afraid will slow down the code)
For Example If my table is:
[A,de-prioritized,2,1,0,0,1],
[B,de-prioritized,0,2,1,1,0],
[C,other,0,5,2,1,1]
I want to get: [2,3,1,1,1] (as sum of [2,1,0,0,1]+[0,2,1,1,0]).
In other words: Let's say I have a table A1:F10. In A there is a flag (either "de-prioritized" or not), while in B1:F10 there are values. I want to replicate the formula in each column: SUMIF(A1:A10,"de-prioritized",B1:B10),SUMIF(A1:A10,"de-prioritized",C1:C10),SUMIF(A1:A10,"de-prioritized",D1:D10) and so on. I cannot set the formulas because the range in the example above is dynamic. I tried to set the formulaR1C1 with sumif in the script, but it did not work.
I found a similar problem, but I may have n arrays to sum and I am not able to fit the solution in mine: Javascript - Sum two arrays in single iteration
Existing code I have:
var l = sheet2.getRange('A1').getValue();
for (var i = 0; i<l ;i++) {
if (sheet3.getRange(i+3,2).getValue() == 'de-prioritized') {
sheet3.getRange(i+3,1,1,sheet3.getLastColumn()).setBackgroundRGB(250, 240, 230);
} else sheet3.getRange(i+3,1,1,sheet3.getLastColumn()).setBackgroundRGB(225, 247, 232)
}
[2,3,1,1,1]
from [["A","de-prioritized",2,1,0,0,1],["B","de-prioritized",0,2,1,1,0],["C","other",0,5,2,1,1]]
using Google Apps Script.const arr = [["A","de-prioritized",2,1,0,0,1],["B","de-prioritized",0,2,1,1,0],["C","other",0,5,2,1,1]];
const res = arr.reduce((ar, [,b,...cdefg]) => {
if (b == "de-prioritized") ar = ar.length > 0 ? ar.map((e, i) => e + cdefg[i]) : cdefg;
return ar;
}, []);
console.log(res) // <--- [2,3,1,1,1]
const arr = [["A","de-prioritized",2,1,0,0,1],["B","de-prioritized",0,2,1,1,0],["C","other",0,5,2,1,1]]; const res = arr.reduce((ar, [,b,...cdefg]) => { if (b == "de-prioritized") ar = ar.length > 0? ar.map((e, i) => e + cdefg[i]): cdefg; return ar; }, []); console.log(res)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.