Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-api

How can I build a filter that contains a certain text by gas


I have a script that works fine

function setFilter() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var filterSettings = {};
  filterSettings.range = {sheetId: ss.getSheetByName("mySheet").getSheetId()};
  filterSettings.criteria = {};
  filterSettings['criteria'][4] = {
    'hiddenValues': ["FALSE"]
  };
  var request = {
    "setBasicFilter": {
      "filter": filterSettings
    }
  };
  Sheets.Spreadsheets.batchUpdate({'requests': [request]}, ss.getId());
}

with in column E

={"test";arrayformula(if(B2:B="",,not(iserror(find("mike",B2:B)))))}
  • I would like to replace hiddenValues FLASE on column E (4) by when text contains "something" in column B (1), I tried 'textContains': ['mike'] but that doesn't work
  • Is it possible to create a filterview instead of a basic filter

Solution

  • Issue:

    You have to define TEXT_CONTAINS under field condition.

    Solution:

    Replace this:

    filterSettings['criteria'][4] = {
      'hiddenValues': ["FALSE"]
    };
    

    With this:

    filterSettings['criteria'][1] = {
      "condition": {
        "type": "TEXT_CONTAINS",
        "values": [
          {
            "userEnteredValue": "mike"
          }
        ]
      }
    };
    

    Notes:

    • criteria is deprecated, consider using filterSpecs instead.
    • I didn't notice your second question before. Take a look at Managing filter views for information on creating filter views.

    Reference: