使用Google Apps脚本向筛选器视图添加条件(高级工作表API)

人气:999 发布:2022-10-16 标签: criteria filter google-apps-script google-sheets-api

问题描述

我正在尝试使用高级工作表API创建筛选器视图。不过,我在上面找到的所有东西都是用Python或其他语言写的,而且我也没有那么高级,我只能勉强通过谷歌搜索问题!我的其余代码搜索每天添加的新选项卡,这些代码的目的是在该选项卡上自动创建带有一些条件的筛选器视图。

我使用的是";Sheets.Spreadsheets.BatchUpdate()";方法,除了";filterSpes[]";属性之外,此代码中的所有内容都适用于我。我首先尝试了Criteria属性,但后来发现它不再使用了?请帮帮忙!

              const resource = {
                requests: {
                  addFilterView: {
                    
                    filter: {
                      filterViewId: '0000000',
                      title: 'Chris Johnson',
                      range: {
                        sheetId: st1.toString(),
                        startRowIndex: 0,
                        endRowIndex: 500,
                        startColumnIndex: 0,
                        endColumnIndex: 8
                      }
                      filterSpecs: [{
                          3: {condition: {
                                  type: 'TEXT_CONTAINS',
                              values: {
                                    userEnteredValue: 'Chris Johnson'
                                    }}}
                                   }],
            }
            }}}

推荐答案

修改点:

}filterSpecs: [{出错。,需要添加。 filterSpecs的元素不正确。 即使不包括filterViewId,也会添加筛选器视图。但当使用filterViewId时,您可以提供原始ID。 requests的属性是数组。

当这些要点反映在您的请求正文中时,它将如下所示。

修改后的脚本:

function myFunction() {
  const spreadsheetId = "###"; // Please set your Spreadsheet ID.
  const resource = {
    "requests": [
      {
        "addFilterView": {
          "filter": {
            "filterViewId": 12345,
            "title": "Chris Johnson",
            "range": {
              "sheetId": 0,
              "startRowIndex": 0,
              "endRowIndex": 500,
              "startColumnIndex": 0,
              "endColumnIndex": 8
            },
            "filterSpecs": [
              {
                "filterCriteria": {
                  "condition": {
                    "type": "TEXT_CONTAINS",
                    "values": [
                      {
                        "userEnteredValue": "Chris Johnson"
                      }
                    ]
                  }
                },
                // "columnIndex": 0 // If you want to use the column, please use this.
              }
            ]
          }
        }
      }
    ]
  };
  Sheets.Spreadsheets.batchUpdate(resource, spreadsheetId);
}

参考:

AddFilterViewRequest

119