I'm new in google scripts, please help me to understand. my row is for example [11, 22, 33, 44] i want get them individually 11 22 33 44 and maybe add to each of them 1 or 5 and put them in array like this `
[[11], [22], [33], [44]]
or if 1
[[12], [23], [34], [45]]
` How can i achieve this and what type of methods or functions i can use Explain with examples please
CodePudding user response:
Descripton
Here is a good reference to learning javascript Javascript Tutorial Using Array.map() , perform a function for each element of the array.
Script
function unFlat() {
try {
let a = [11,22,33,44];
let b = a.map( x => [x] );
let c = 5;
let d = a.map( x => [x c])
console.log(a);
console.log(b);
console.log(d);
}
catch(err) {
console.log(err);
}
}
6:10:04 AM Notice Execution started
6:10:04 AM Info [ 11, 22, 33, 44 ]
6:10:04 AM Info [ [ 11 ], [ 22 ], [ 33 ], [ 44 ] ]
6:10:04 AM Info [ [ 16 ], [ 27 ], [ 38 ], [ 49 ] ]
6:10:04 AM Notice Execution completed
Reference
CodePudding user response:
Put all rows into columns
function putallrowsintocolums() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const vs = sh.getDataRange().getDisplayValues();
let nr = sh.getLastRow() 1;
vs.forEach((r,i) => {
sh.getRange(nr,i 1,r.length).setValues(r.map(e => [e]));
});
}