Filter data by column K in Google Script Editor (Google Sheets)

后端 未结 3 2139
旧巷少年郎
旧巷少年郎 2021-01-01 01:21

In this example, I have a set of data from a Google Sheet (4Cat) feeding into another sheet (ImportFeeder) where I am running my Google Script.

At the end of the scr

3条回答
  •  余生分开走
    2021-01-01 01:34

    Why not using a custom filter function? Added a .toLowerCase() to match "ipad" case insensitive.

    function myFunction() {
    var sss = SpreadsheetApp.openById('1kL96dRm3Z8XBtMXnSzUARxR1b34-njlkZQ1sU0c3g1s'); //replace with source ID
    var ss = sss.getSheetByName('4cat');   //replace with source Sheet tab name
    var range = ss.getRange('A:V');        //assign the range you want to copy
    var rawData = range.getValues()        // get value from spreadsheet 1
    var data = rawData.filter(isColKiPad); // Filtered Data will be stored in this array
    var tss = SpreadsheetApp.openById('1u7547KaniKHLUJn2v-ezN4l8ZcxE4viCFcoxsA904MI'); //replace with destination ID
    var ts = tss.getSheetByName('Sheet1'); //replace with destination Sheet tab name
    ts.getRange(2,1,ts.getLastRow() - 1,ts.getLastColumn()).clear(); // Assuming header is in the first row, clears sheet but header
    ts.getRange(2, 1, data.length, data[0].length).setValues(data);
    };
    // Change both to ts.getRange(1,1,[...] if there's no header row
    function isColKiPad(arr) {
      return arr[10].toLowerCase() == "ipad";
    };
    

    Did you consider using a Spreadsheet formula? You could try a combination of =IMPORTRANGE(spreadsheet_key, string_range) and QUERY(data, query, [header]) to import your range already filtered :

    =QUERY(IMPORTRANGE("1kL96dRm3Z8XBtMXnSzUARxR1b34-njlkZQ1sU0c3g1s";"4cat!A:V");"SELECT * WHERE Col11 = 'iPad'")
    

    You won't even need to clear your sheet this way.

提交回复
热议问题