Google App Script / Spreadsheet - How to get filter criteria?

后端 未结 6 530
自闭症患者
自闭症患者 2020-12-10 18:18

Is it possible to get the filter criteria of my data set. For example, if one of my column is \"Department\" and I filtered the data to display only \"IT\". How do we get th

6条回答
  •  青春惊慌失措
    2020-12-10 18:51

    Based on mhawksey answer, I wrote the following function:

    //
    function celdasOcultas(ssId, rangeA1) {  
      // limit what's returned from the API
      var fields = "sheets(data(rowMetadata(hiddenByFilter)),properties/sheetId)";
      var sheets = Sheets.Spreadsheets.get(ssId, {ranges: rangeA1, fields: fields}).sheets;
      if (sheets) {
        return sheets[0].data[0].rowMetadata;
      }
    }

    then called it this way:

        // i.e. : ss = spreadsheet, ranges = "Hoja 2!A2:A16"
        Logger.log(range.getA1Notation());
        var ocultas = celdasOcultas(ss.getId(), sheetName + "!" + range.getA1Notation());
        // Logger.log(ocultas);
        var values = range.getValues();
        for (var r = 0; r < values.length; r++) {
          if (!ocultas[r].hiddenByFilter) {
            values[r].forEach( function col(c){
              Logger.log(c);
            });
          }
          //range.setBackground("lightcoral");
        }

    Than prevent the log of the hidden rows. You can see it in action at: Prueba script, note project has to get Google Sheets API enabled on Google API Console.

    Hope it helps. Thank you!

提交回复
热议问题