删除所有文件程序条件,但保留过滤器 [英] Delete all filer criterias but preserve filter
问题描述
我想清除所有过滤器规则,但保留过滤器本身.
I want to clear all filter rules, but leave the filter itself.
有直接,快速的方法吗?
Is there a direct and fast way to do it?
我找到的代码是:
var filter = sheet.getFilter();
It gets the filter object, and the number of options I have with filter# is limited.
注意:filter.remove()
删除了此过滤器,但我需要保留它.
Note: filter.remove()
removes this filter, but I need to preserve it.
推荐答案
您只想删除基本过滤器的标准,而不删除基本过滤器.如果我的理解是正确的,那么如何使用Sheets API?示例脚本如下.使用此脚本时,请在高级Google服务和API控制台中启用Sheets API.
You want to remove only the criteria of basic filters while the basic filter is not removed . If my understanding is correct, how about using Sheets API? The sample script is as follows. When you use this script, please enable Sheets API at Advanced Google Services and API console.
- 在脚本编辑器上
- 资源->高级Google服务
- 打开Google Sheets API v4
- On script editor
- Resources -> Advanced Google Services
- Turn on Google Sheets API v4
- 在脚本编辑器上
- 资源-> Cloud Platform项目
- View API控制台
- 在入门"中,单击启用API"并获取凭据(例如密钥).
- 在左侧,单击库.
- 在搜索API和服务,输入表格".然后点击Google Sheets API.
- 单击启用"按钮.
- 如果已启用API,请不要关闭.
- On script editor
- Resources -> Cloud Platform project
- View API console
- At Getting started, click Enable APIs and get credentials like keys.
- At left side, click Library.
- At Search for APIs & services, input "sheets". And click Google Sheets API.
- Click Enable button.
- If API has already been enabled, please don't turn off.
如果现在要使用脚本来打开脚本编辑器以使用Sheets API,则可以通过访问此URL https://console.cloud.google.com/apis/library/sheets.googleapis.com/
If now you are opening the script editor with the script for using Sheets API, you can enable Sheets API for the project by accessing this URL https://console.cloud.google.com/apis/library/sheets.googleapis.com/
var id = SpreadsheetApp.getActiveSpreadsheet().getId(); var filters = Sheets.Spreadsheets.get(id, {fields: "sheets/basicFilter"}); var resource = {requests: filters.sheets.filter(function(e){return Object.keys(e).length}).map(function(e){return {setBasicFilter: {filter: {range: e.basicFilter.range}}}})}; Sheets.Spreadsheets.batchUpdate(resource, id);
注意:
- 此示例通过不提供标准的参数而仅删除标准,而没有删除基本过滤器.
- 在此示例中,电子表格中所有工作表的条件均被删除.因此,如果您要删除特定工作表的条件,请修改此示例.
- This sample removes only the criteria by not giving the parameters for the criteria, while the basic filter is not removed.
- In this sample, the criteria of all sheets in the spreadsheet are removed. So if you want to remove the criteria for the specific sheet, please modify this sample.
- 高级Google服务: https://developers.google.com/apps -script/guides/services/advanced
- Sheets API v4: https://developers.google.com/sheets/api/
- Advanced Google Services : https://developers.google.com/apps-script/guides/services/advanced
- Sheets API v4: https://developers.google.com/sheets/api/
Note :
如果这不是您想要的,对不起.
If this was not what you want, I'm sorry.
这篇关于删除所有文件程序条件,但保留过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!