I've got an array question for processing in google sheets. I'm trying to map a value from one sheet into another. Thanks for any help. I've got explanation and code below...
PROBLEM: this gives me blank rows (policies2[i]
).
DESIRED OUTCOME: new values from entry_top
in the last row of policies2
.
I'm working with two sheets. policies.values
is the array from a normal google sheet of tabular values. policies2
is a copy of policies
. entry_top
is the array of values from a sheet with unstructured data, except that headers and values are beside each other.
So if "Line" is a header in policies
, it would find "Line" in a cell/array node in entry_top
and then get the next cell/array value (next column/index/to the right), take that value and put it in the last row in the same column in policies
(match by header).
So the code below loops through policies.values[0]
, the header values. Then loops through every cell in entry_top
, by rows (e
) and columns (ee
). Then if the headers match (h
and ee
), it puts the next value ([ei][eei+1]
) in the matching policies2
column in the last row ([policies2.length-1][hi]
).
policies.values[0].map((h, hi) => {
entry_top.map((e, ei) => {
e.map((ee, eei) => {
if (ee == h) policies2[policies2.length - 1][hi] = entry_top[ei][eei + 1];
});
});
});
MRE: oddly this example works. so not sure what above is causing an issue...
function testdp() {
policies = {
values:[[1,2,3],[4,5,6],[7,8,9]]
}
policies2=[[1,2,3],[4,5,6],[7,8,9],[,,,]];
entry_top = [[,,1,'add',,],[,'a','b',2,'add2',,'c'],['c',,,3,'add3',,]]
Logger.log(policies.values);
Logger.log(policies2);
Logger.log(entry_top);
policies.values[0].forEach((h,hi)=>{
entry_top.forEach((e,ei)=>{
e.forEach((ee,eei)=>{
if (ee == h) policies2[policies2.length - 1][hi] = entry_top[ei][eei + 1];
})
})
});
Logger.log(policies2);
// [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [add, add2, add3]]
}