批量删除Google表格中的过滤器视图

时间:2019-02-11 14:26:15

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

我找到了this old thread,但似乎无法使用任何方法。

问题:我有一个电子表格,有数十个人使用,并且随着时间的推移,已经创建了200多个过滤器视图。我想从工作簿的一张工作表中批量删除所有过滤器视图

启用Google Sheets API后,我尝试了此方法。它可以运行,但是,所有过滤器视图仍然保留。

mergeObject() { 
        let merged = {};
            this.needMergedArray.map(item => {
          merged = {...merged, [item.key]: {key: [item.key], server: (merged[item.key] && merged[item.key].server || []).concat(item.server)}};
        });
        return Object.values(merged);
      }
    }

谢谢所有

过滤器视图的图像:

Image of Filter Views

1 个答案:

答案 0 :(得分:0)

问题:

  • 您正在发出清除过滤器请求,该请求仅清除当前过滤器

解决方案:

  • 您必须改为发出deleteFilterView请求

示例代码:

function delFilterViews() {
  var ssId = SpreadsheetApp.getActive().getId();
  Sheets.Spreadsheets.batchUpdate(
    {
      requests: Sheets.Spreadsheets.get(ssId, {
        ranges: 'Sheet1', //Sheet in which filterviews are present
        fields: 'sheets/filterViews/filterViewId',
      }).sheets[0].filterViews.map(function(e) {
        return { deleteFilterView: { filterId: e['filterViewId'] } }; //create a new delete filter view request for each filter view present in sheet1
      }),
    },
    ssId
  );
}

参考文献: