Reputation: 71
question is, how to change this part of the code, to get all columns from the array not only [0,1,2,3]
this is the codeline:
const new_table = [0,1,2,3].map(x => make_row_from_col(this_table, x)).join('\n');
and no, object.keys(this_table) does NOT work, because it does NOT give me all columns back only 4 rows are getting displayed smh.
whole script:
function Test() {
const sSheet = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = sSheet.getSheetByName("Refinery");
const table = srcSheet.getRange("A:H").getValues(); // use "D2:M" if you have a header
const values_C = table.filter(t => t[7] == "READY TO SELL").map(t => t[0]);
const values_D = table.filter(t => t[7] == "READY TO SELL").map(t => t[1]);
const values_E = table.filter(t => t[7] == "READY TO SELL").map(t => t[2]);
const values_F = table.filter(t => t[7] == "READY TO SELL").map(t => t[3]);
Logger.log(values_C)
Logger.log(values_D)
Logger.log(values_E)
Logger.log(values_F)
Logger.log(values_C.length)
const this_table = [values_C,values_D,values_E,values_F];
const make_row_from_col = (this_table, col) => this_table.map(x => x[col]).join("\t"+"\t"+" | "+"\t"+"\t");
const new_table = Object.keys(this_table).map(x => make_row_from_col(this_table, x)).join('\n');
Logger.log(new_table);
}
this is LOG of this script and output that i want:
Upvotes: 0
Views: 43
Reputation: 19309
Object.keys
, when applied to an array, will return an array with the indexes of this array. If you're using it on this_table
, you'll only when an array with 4 items, since that's the length of this_table
.
Because of this, you're only getting 4 rows for your "table".
A easier and more efficient way to do this is to retrieve the values from your 4 desired columns at once, and use the different join
on that:
const values = table.filter(t => t[7] == "READY TO SELL").map(t => t.slice(0,4));
const new_table = values.map(this_row => this_row.join("\t"+"\t"+" | "+"\t"+"\t")).join("\n");
If you wanted to retrieve the columns separately, though, I'd suggest transposing the resulting 2D array so that inner arrays are rows and not columns. See this answer for an example on how to transpose an array (I don't think it makes much sense to do that, since getValues()
by itself gives you the data organized in your desired way; also, even if the desired columns were not contiguous, you could easily filter your desired ones in a single 2D array).
Upvotes: 1