【问题标题】:Add Criteria to Filter View using Google Apps Script (Advanced Sheets API)使用 Google Apps 脚本(高级表格 API)向过滤视图添加条件
【发布时间】:2022-01-19 23:25:49
【问题描述】:

我正在尝试使用高级工作表 API 创建过滤器视图。我能在上面找到的所有东西都是用 Python 或其他语言写的,而且我不是那么先进,我几乎无法通过谷歌搜索问题完成它!我的其余代码会搜索每天添加的新选项卡,此代码的目的是在具有某些条件的选项卡上自动创建过滤器视图。

我正在使用“Sheets.Spreadsheets.batchUpdate()”方法,除了“filterSpecs[]”属性之外,此代码中的所有内容都适用于我。我先尝试了条件属性,但后来发现它不再使用了?请帮忙!

              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'
                                    }}}
                                   }],
            }
            }}}

【问题讨论】:

  • filterSpec 中的 3 的用途是什么?那是列索引吗?
  • @AaronDuniganAtLee 是指定过滤器所在的列,但我认为我的代码是多种语言的混合,哈哈。

标签: google-apps-script filter google-sheets-api criteria


【解决方案1】:

修改点:

  • }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);
}

参考:

【讨论】:

  • 感谢您的编辑!那么如何调整代码使第 3 列必须具有条件“Chris Johnson”?
  • @Tyler Henderson 感谢您的回复。关于您对So how do I adjust the code so the 3rd column must have the criteria "Chris Johnson"?的附加问题,在这种情况下,请在上述请求正文中使用"columnIndex": 2而不是"columnIndex": 0。这样,使用列“C”。如果我误解了您的附加问题,我深表歉意。
  • @Tyler Henderson 感谢您的回复。很高兴您的问题得到解决。
  • 点赞,很有用!
  • @Mike Steelson 谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
相关资源
最近更新 更多