I'm still very novice at app scripts and javascript, but the more I learn, the easier my job gets.
I have a one dimensional array (var shippingID) and a two dimensional array (var shipDetailsArr). ShippingID acts as a filter for the first column of shipDetailsArr. I want to return all the columns shipDetailsArr where the first column ONLY equals any of the cells from ShippingID. How can I do this on the script editor in app scripts?
Here's an example of the two arrays. The first array is a Shipment ID with a few shipments . The 2nd is a larger set of data that has Shipment ID as the first column. I want to create a new array that returns the 2nd set of data with only the Shipping IDs from the first set. Hope this makes sense, and thanks for your help
Thanks for any suggestions!
Eric
Ciao, supposing that you have shippingID
as an arry of strings and shipDetailsArr
is a 2D array, with javascript you could use filter
function in this way:
let shippingID = ["20-05-01-SHEN-EU", "20-05-01-SHEN-CAN"]; let shipDetailsArr = [["20-05-01-SHEN-EU", "PJ0201", "CE-12-SLOT-WOOD-WATCH-DARK"], ["20-05-01-SHEN-CAN", "PJ0201", "CE-12-SLOT-WOOD-WATCH-DARK"], ["20-05-01-SHEN-US", "PJ0201", "CE-12-SLOT-WOOD-WATCH-DARK"]]; let result = shipDetailsArr.filter(det => shippingID.includes(det[0])); console.log(result);
As you can see from result, I filtered shipDetailsArr
using shippingID.includes(det[0])
. This returns only shipDetailsArr
with a shipmentID
present in shippingID
array.
Use the following code:
function filter2D() {
const ss = SpreadsheetApp.getActive();
const dsh = ss.getSheetByName('All Data');
const data = dsh.getRange('A1:G20').getValues();
const fsh = ss.getSheetByName('Filter Data');
const filterList = fsh.getRange('A2:A13').getValues().flat([1]);
const osh = ss.getSheetByName('Output Data');
const f_data = data.filter(function (row) {
return filterList.includes(row[0]);
});
osh.getRange(1,1,f_data.length,f_data[0].length).setValues(f_data);
}
Note that you can modify the ranges for both Filter Data
and All Data
according to your needs. I also use a third sheet with the name Output Data
to store the filtered data.
Here is an example on how you can filter a 2D array based on values of a 1D array:
function filter2D() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const data = sh.getRange('A1:D18').getValues();
const filterList = sh.getRange('F1:F2').getValues().flat([1]);
const f_data = data.filter(function (row) {
return filterList.includes(row[0]);
});
sh.getRange(24,1,f_data.length,f_data[0].length).setValues(f_data);
}
You will use the filter() function to find the rows in the 2D array data
that meet a particular condition. That is, the first column ( row[0]
) should contain values from filterList
. In order to check whether this happens you can use includes() . Then, I store the filtered data f_data
back to the sheet. Of course this is just for illustration. Feel free to adjust this code to your needs.
function getDetailsForAnId(id='1') {//default used for testing
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('All Data');
const vs=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).getValues();
var oA=[];
oA.push([id].concat(new Array(sh.getLastColumn()-1)));//display id only on first line of each section
vs.forEach(function(r){
if(r[0]==id) {
let t=r.slice();
t[0]='';//remove unnecessary id in output only for clarity
oA.push(t);
}
});
if(oA) {
//console.log(oA);
return oA;
};
}
function loopThruIdsAndDisplayOnOutputSheet() {
const ss=SpreadsheetApp.getActive();
const osh=ss.getSheetByName('OutputSheet');
osh.clearContents();//clear output sheet when starting
const sh=ss.getSheetByName('Filter Data');
const ids=sh.getRange(2,1,sh.getLastRow()-1,1).getValues();
ids.forEach(function(id){
let rows=getDetailsForAnId(id);
if(rows) {
osh.getRange(osh.getLastRow()+1,1,rows.length,rows[0].length).setValues(rows);
}
});
}
All Data(csv):
COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9,COL10,COL11,COL12,COL13,COL14,COL15,COL16,COL17,COL18,COL19,COL20,COL21,COL22,COL23,COL24,COL25,COL26
1,0,14,6,0,19,6,24,22,6,4,17,7,15,4,3,16,5,9,8,20,2,9,15,24,12
2,22,15,3,19,22,23,12,9,14,15,7,0,13,8,20,12,20,4,19,18,9,3,3,2,13
3,23,8,20,9,13,19,10,18,24,23,0,17,16,9,9,12,1,24,1,23,6,11,13,3,10
1,11,6,17,0,17,12,12,4,5,6,10,24,22,21,17,11,17,20,18,1,20,7,18,21,9
2,17,12,5,8,24,10,14,13,15,23,21,20,17,9,20,3,16,11,21,23,1,6,23,18,24
3,22,8,24,2,12,22,6,1,5,1,6,2,20,22,1,7,24,23,6,15,19,15,16,19,21
1,3,21,18,24,9,19,3,23,3,10,23,7,14,16,10,7,0,9,14,20,20,15,16,11,11
2,15,13,23,2,11,14,2,6,24,3,16,18,10,10,8,4,1,4,11,7,10,18,17,7,11
3,22,17,19,8,2,13,3,3,21,19,19,3,14,2,11,5,8,22,6,5,1,13,3,4,18
1,13,13,8,22,6,18,14,8,10,1,24,23,12,21,8,19,11,0,4,23,3,22,20,5,5
2,16,16,18,13,9,15,18,12,23,20,17,13,17,8,1,5,5,11,23,12,12,11,21,21,17
3,2,4,17,10,5,18,4,11,7,4,15,1,0,2,11,15,13,20,10,14,22,12,16,12,9
1,12,0,21,2,17,5,1,17,1,12,9,5,9,8,5,18,20,19,14,22,23,11,6,23,7
2,16,2,16,1,9,15,4,20,22,2,19,9,14,10,2,8,6,12,6,15,7,17,21,20,20
3,22,9,16,9,22,10,1,18,16,19,21,13,16,16,18,20,15,5,2,9,16,21,14,7,6
1,20,19,23,18,13,10,13,16,9,23,11,15,20,0,8,18,1,19,2,1,7,7,16,16,11
2,3,12,7,5,6,5,24,19,12,5,20,5,11,10,13,8,1,22,12,16,16,10,8,1,6
3,6,18,18,23,13,22,8,22,4,8,24,2,2,20,13,12,21,12,19,0,1,21,0,24,11
1,15,23,9,1,15,14,5,2,7,0,8,5,6,15,15,10,20,17,9,17,23,17,10,10,11
2,18,22,17,8,9,23,24,0,23,10,10,9,18,10,10,24,16,22,11,11,3,14,9,3,9
3,15,15,17,20,13,12,5,4,21,2,10,10,16,13,19,12,7,2,6,14,13,8,10,16,19
1,12,2,8,11,1,8,24,4,11,19,4,20,20,1,10,22,17,15,7,15,18,24,14,1,21
2,9,9,23,9,11,9,12,15,13,17,14,15,16,13,3,3,15,15,6,2,2,1,18,13,24
3,24,4,23,8,15,1,10,8,7,13,4,19,15,13,23,23,4,20,3,5,11,5,10,7,8
1,3,20,12,4,19,0,20,12,18,1,1,1,10,8,21,18,6,20,15,0,2,4,23,5,22
Filter Data(csv):
HDR1
1
2
3
OutputSheet(csv):
1,,,,,,,,,,,,,,,,,,,,,,,,,
,0,14,6,0,19,6,24,22,6,4,17,7,15,4,3,16,5,9,8,20,2,9,15,24,12
,11,6,17,0,17,12,12,4,5,6,10,24,22,21,17,11,17,20,18,1,20,7,18,21,9
,3,21,18,24,9,19,3,23,3,10,23,7,14,16,10,7,0,9,14,20,20,15,16,11,11
,13,13,8,22,6,18,14,8,10,1,24,23,12,21,8,19,11,0,4,23,3,22,20,5,5
,12,0,21,2,17,5,1,17,1,12,9,5,9,8,5,18,20,19,14,22,23,11,6,23,7
,20,19,23,18,13,10,13,16,9,23,11,15,20,0,8,18,1,19,2,1,7,7,16,16,11
,15,23,9,1,15,14,5,2,7,0,8,5,6,15,15,10,20,17,9,17,23,17,10,10,11
,12,2,8,11,1,8,24,4,11,19,4,20,20,1,10,22,17,15,7,15,18,24,14,1,21
,3,20,12,4,19,0,20,12,18,1,1,1,10,8,21,18,6,20,15,0,2,4,23,5,22
2,,,,,,,,,,,,,,,,,,,,,,,,,
,22,15,3,19,22,23,12,9,14,15,7,0,13,8,20,12,20,4,19,18,9,3,3,2,13
,17,12,5,8,24,10,14,13,15,23,21,20,17,9,20,3,16,11,21,23,1,6,23,18,24
,15,13,23,2,11,14,2,6,24,3,16,18,10,10,8,4,1,4,11,7,10,18,17,7,11
,16,16,18,13,9,15,18,12,23,20,17,13,17,8,1,5,5,11,23,12,12,11,21,21,17
,16,2,16,1,9,15,4,20,22,2,19,9,14,10,2,8,6,12,6,15,7,17,21,20,20
,3,12,7,5,6,5,24,19,12,5,20,5,11,10,13,8,1,22,12,16,16,10,8,1,6
,18,22,17,8,9,23,24,0,23,10,10,9,18,10,10,24,16,22,11,11,3,14,9,3,9
,9,9,23,9,11,9,12,15,13,17,14,15,16,13,3,3,15,15,6,2,2,1,18,13,24
3,,,,,,,,,,,,,,,,,,,,,,,,,
,23,8,20,9,13,19,10,18,24,23,0,17,16,9,9,12,1,24,1,23,6,11,13,3,10
,22,8,24,2,12,22,6,1,5,1,6,2,20,22,1,7,24,23,6,15,19,15,16,19,21
,22,17,19,8,2,13,3,3,21,19,19,3,14,2,11,5,8,22,6,5,1,13,3,4,18
,2,4,17,10,5,18,4,11,7,4,15,1,0,2,11,15,13,20,10,14,22,12,16,12,9
,22,9,16,9,22,10,1,18,16,19,21,13,16,16,18,20,15,5,2,9,16,21,14,7,6
,6,18,18,23,13,22,8,22,4,8,24,2,2,20,13,12,21,12,19,0,1,21,0,24,11
,15,15,17,20,13,12,5,4,21,2,10,10,16,13,19,12,7,2,6,14,13,8,10,16,19
,24,4,23,8,15,1,10,8,7,13,4,19,15,13,23,23,4,20,3,5,11,5,10,7,8|
OutputSheet Image:
BTW Your question asks how to filter a 2D array with a 1D array. You should be aware that they are both 2D Arrays. A Single Column is still a 2D Array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.