Starting out in JS. I tried to be concise in the title, couldnt. I have some data in a sheet:
What I want and have somewhat accomplished, is to do two things
group by id and sum the qty, reformat the pr column by product, just a name. I'll try to make the code reproducible right here, so it's easier to catch the error (I might solve it without asking, :O, I'll leave this)
//var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
//var sheet = spreadsheet.getSheetByName("SortProductos");
var a = [[ 66 ],[ 66 ],[ 66 ],[ 66 ],[ 66 ],[ 24 ],[ 330 ],[ 155 ],[ 127 ],[ 110 ],[ 272 ],[ 272 ],[ 272 ],[ 272 ],[ 272 ],[ 70 ],[ 78 ]];
var b = [ [ 1 ],[ 1 ],[ 1 ],[ 1 ],[ 1 ],[ 2 ],[ 2 ],[ 1 ],[ 1 ],[ 1 ],[ 1 ],[ 1 ],[ 2 ],[ 1 ],[ 1 ], [ 1 ],[ 1 ] ];
var c = [ [ 'PD-1346' ],[ 'PD-1000-B' ],[ 'PD-1028' ],[ 'PD-711' ],[ 'PD-58' ],[ 'PD-1445' ],[ 'PD-860' ],[ 'PD-648' ],[ 'PD-1483' ],[ 'PD-1044' ],[ 'PD-58' ],[ 'PD-65' ],[ 'PD-1499' ],[ 'PD-368' ],[ 'PD-465' ],[ 'PD-73B' ],[ 'PD-1469' ] ];
var pNameF = [];
for (let i=0; i<c.length;i ) {
var temp = c[i][0];
if (temp.startsWith('PD-')) {
pNameF.push('FRASLE')
}
}
const data = a.map(function(a, indice) {
return {
id: a[0],
qty: b[indice][0],
pName: pNameF[indice][0],
}
})
console.log(data);
const inputVal = [
...data.reduce(
(map, item) => {
const { id: key, qty } = item;
const prev = map.get(key);
if(prev) {
prev.qty = qty
} else {
map.set(key, Object.assign({}, item))
}
return map
},
new Map()
).values()
]
console.log(inputVal)
const inputFinal = [];
for (let i=0; i<inputVal.length; i ) {
let id = inputVal[i][0];
let qty = inputVal[i][1];
let prodN = inputVal[i][2];
inputFinal.push([id, qty, prodN])
}
console.log(inputFinal);
So, problems that arise, a couple: First off, IDK why the arr.map just catches the first letter of the whole word instead of the entire string???? talking about the var = pNameF
But let's ignore that, the reduce does it job but I have to reconvert that to a 2-d array for google sheets if not, I can't use .setValues();
Yet the for says undefined which I'm guessing means the const inputVal isn't filled?? but how? if I log it just before and it shows the content.
I'm sure there are better ways to do all of this but I'm trying to do stuff with what I kinda understand reduce being the least understood and map close.
CodePudding user response:
Issues:
First: pNameF is a 1D array so accessing the index then further getting the [0] gets you the F instead of FRASLE.
To fix this, modify:
pNameF.push('FRASLE')
To:
pNameF.push(['FRASLE'])
or just remove the [0] when accessing the value.
Next: You are trying to access inputVal like it is a 2D array, but it is not. It's an array of object. You need to access the keys per element instead.
From:
for (let i = 0; i < inputVal.length; i ) {
let id = inputVal[i][0];
let qty = inputVal[i][1];
let prodN = inputVal[i][2];
inputFinal.push([id, qty, prodN])
}
To:
for (let i = 0; i < inputVal.length; i ) {
let id = inputVal[i].id;
let qty = inputVal[i].qty;
let prodN = inputVal[i].pName;
inputFinal.push([id, qty, prodN])
}
Script Output:
You should now be able to use this to setValues()
sheet.getRange(1, 1, inputFinal.length, inputFinal[0].length).setValues(inputFinal);



