1

I'm able to make an addFilterView request using the batchUpdate. My question is that how can I retrieve the created filterViewId?

Here is the code I'm using:

 var filterSettings = {
  "title":criteria,
  "range": {
    "sheetId": sheetId,
    "startRowIndex": startRowIndex,
    "endRowIndex": 99999,
    "startColumnIndex": 0,
    "endColumnIndex": lastColumn
  },
    "criteria": {

    }
  };

  filterSettings.criteria[colNo] = {
         "condition": {
           "type": "TEXT_CONTAINS",
           "values": [
             {
               "userEnteredValue": criteria
             }
           ]
         }
       }

   var requests = [{
  "addFilterView": {
    "filter": filterSettings
  }
  }];

Sheets.Spreadsheets.batchUpdate(
        {
          requests:  requests 
        },
        ssId
        );
Rubén
  • 34,714
  • 9
  • 70
  • 166
Husain Alhamali
  • 823
  • 4
  • 17
  • 32

1 Answers1

4
  • You want to retrieve filterViewId from the created filter view.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Pattern 1:

In this pattern, when the method of batchUpdate is run, filterViewId is retrieved from the returned value.

Please modify as follows.

Modified script:

From:
Sheets.Spreadsheets.batchUpdate(
        {
          requests:  requests 
        },
        ssId
        );
To:
var res = Sheets.Spreadsheets.batchUpdate({requests:  requests}, ssId);
var filterViewId = res.replies[0].addFilterView.filter.filterViewId;
Logger.log(filterViewId)

Pattern 2:

In this pattern, filterViewId is retrieved from using the method of spreadsheets.get in Sheets API. In this case, the result is retrieved by the sheet name and filter view title.

Sample script:

function myFunction() {
  var sheetName = "Sheet1";  // Please set the sheet name.
  var filterViewTitle = "sampleName";  // Please set the filter view title.

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssId = ss.getId();
  var res = Sheets.Spreadsheets.get(ssId);
  var sheet = res.sheets.filter(function(s) {return s.properties.title == sheetName});
  if (sheet.length == 1) {
    sheet[0].filterViews.forEach(function(f) {
      if (f.title == filterViewTitle) {
        Logger.log(f.filterViewId)
      }
    });
  }
}

Note:

  • In above scripts, it supposes that Sheets API has already been enabled at Advanced Google services.

Reference:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks for your answer @Tanaike, actually yes this is what I'm looking for but it's my bad I that I didn't make it more clear, actually I've already tried these solutions and they are working fine but the issue with them is that they are taking too long time to execute. I'm applying this based on values from around 500 rows and imagine that for each row it's taking 8 seconds. So Besides your answer, is there any way to make it faster? – Husain Alhamali Feb 12 '20 at 10:08
  • @Ali Alhamaly Thank you for replying. I'm glad your issue was resolved. I would like to also support your additional question. But I think that your additional question is new issue, and that is different from your question. So can you post it as new question by including the detail information? Because when your initial question is changed by comment, other users who see your question are confused. By posting it as new question, users including me can think of your new question. At that time, please close this question. If you can cooperate to resolve your new issue, I'm glad. – Tanaike Feb 12 '20 at 12:11
  • It seems that the property of `criteria` of `addFilterView` is deprecated. So in this case, please use `filterSpecs[]`. https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/sheets#FilterView The sample script can be seen at https://stackoverflow.com/a/70630015/7108653 – Tanaike Jan 08 '22 at 08:53