I want to restrict the map to only those columns of the range that are in the list headers
which is a subset of v[0]
so that only the values in the columns of the range listed in headers
are changed.
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sht = ss.getSheetByName('Elements');
var rng = sht.getDataRange();
var v = rng.getValues();
var headers = ['Id', 'Label'];
//headers is a subset of v[0]
//This will work for `all` values of the values
//values = v.map(x => x.map(y => y==("Networking - 1") ? 1: y));
values = v.map(x => x.map(y => y== cols.includes(y)==true && y=="Networking - 1" ? 1: y))
var sht2 = ss.getSheetByName('AAA');
sht2.getRange(1,1, v.length, v[0].length).setValues(v);
}
The first step is to get the column indexes where headers
appears. To do that, you can apply indexOf to the first row of your data v
which is the headers row:
v[0].indexOf(headers[0])
v[0].indexOf(headers[1])
The next step is to use map to get only these columns:
v.map(d=>[d[v[0].indexOf(headers[0])],d[v[0].indexOf(headers[1])]]);
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sht = ss.getSheetByName('Elements');
var rng = sht.getDataRange();
var v = rng.getValues();
var headers = ['Id', 'Label'];
//headers is a subset of v[0]
v = v.map(d=>[d[v[0].indexOf(headers[0])],d[v[0].indexOf(headers[1])]]); // new code
//This will work for `all` values of the values
//values = v.map(x => x.map(y => y==("Networking - 1") ? 1: y));
values = v.map(x => x.map(y => y== cols.includes(y)==true && y=="Networking - 1" ? 1: y))
var sht2 = ss.getSheetByName('AAA');
sht2.getRange(1,1, v.length, v[0].length).setValues(v);
}