[英]Google Apps Script - Creating and Saving Filters for Google Sheets
I feel like a bit of a chump, but I cannot work this out...我觉得有点笨,但我无法解决这个问题......
I have been given the job of producing a new master analysis sheet each month from a supplied XML file that combines with various columns of our (multiple) sheets.我的工作是每个月根据提供的 XML 文件生成一个新的主分析表,该文件与我们(多个)表的各个列相结合。 No problems, so far.
没有问题,到目前为止。 I have got all of that working the way I want.
我已经按照我想要的方式完成了所有这些工作。 :-)
:-)
My issue is that we also have about 6-8 filters saved with a specific sheet that allow our auditors to focus on specific areas (and as you can understand, our auditors want these to work EXACTLY as they specify).我的问题是,我们还有大约 6-8 个过滤器保存在一个特定的表中,使我们的审计员可以专注于特定领域(正如您所理解的,我们的审计员希望这些过滤器完全按照他们指定的方式工作)。
I have tried using createFilter() but there doesn't appear any way to save multiple filters to that sheet (maybe I am missing something).我曾尝试使用createFilter()但似乎没有任何方法可以将多个过滤器保存到该工作表中(也许我遗漏了一些东西)。 No joy!
没有快乐! :-(
:-(
I have tried recording a macro which I could then run to create the filters.我试过录制一个宏,然后我可以运行它来创建过滤器。 No joy here either :-(
这里也没有快乐:-(
Do I have to tell these pesky auditors to create there own filters each month (they do know how, but it's beneath them), or is there a way I can script them up and get them off my back?我是否必须告诉这些讨厌的审计员每个月创建自己的过滤器(他们确实知道如何,但它在它们之下),或者有什么方法可以编写它们并让它们摆脱我的背影?
Unfortunately (as much as I would like to) I cannot share our sheets or scripts as we have significant IP embedded there.不幸的是(尽管我很想)我不能分享我们的工作表或脚本,因为我们在那里嵌入了重要的 IP。
I would really appreciate some guidance as to how you might approach this (if it is possible).我真的很感激一些关于你如何处理这个问题的指导(如果可能的话)。
Kind regards亲切的问候
Ian伊恩
If you're indeed talking about the 'Create new filter view', I suggest making an template sheet.如果您确实在谈论“创建新过滤器视图”,我建议制作一个模板表。 So instead of creating a new sheet every month, make one template spreadsheet and add all the filter views your auditors desire.
因此,与其每个月都创建一张新表,不如制作一个模板电子表格并添加您的审计员想要的所有过滤器视图。 Then copy that spreadsheet, and paste the new data in it.
然后复制该电子表格,并将新数据粘贴到其中。
The correct way to create a filter using Apps Script and the createFilter()
is this one:使用 Apps 脚本和
createFilter()
创建过滤器的正确方法是这样的:
function setFilters() {
var ss = SpreadsheetApp.getActiveSheet();
var rangeFilter = ss.getRange("INPUT_YOUR_RANGE_HERE");
var filter = rangeFilter1.createFilter();
var filterCriteria = SpreadsheetApp.newFilterCriteria();
filterCriteria.ADD_YOUR_CRITERIA_HERE;
filter.setColumnFilterCriteria(columnPosition, filterCriteria.build());
}
As you can see, you must use build()
in order to build the criteria for the filter you have created.如您所见,您必须使用
build()
来为您创建的过滤器构建条件。
You can also use the Sheets advanced services and create the filters using the Sheets API, something similar to this:您还可以使用 Sheets 高级服务并使用 Sheets API 创建过滤器,类似于以下内容:
var filterSettings = {
//YOUR FILTER SETTINGS
};
var request = [{
"setBasicFilter": {
"filter": filterSettings
}
}];
And as for calling the Sheets service and applying the above filter, you can use this:至于调用 Sheets 服务并应用上述过滤器,您可以使用:
Sheets.Spreadsheets.batchUpdate({'requests': request}, SPREADSHEET_ID);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.