如何通过 Google Apps 脚本应用 GoogleSheet Filterview? [英] How to apply a GoogleSheet Filterview via Google Apps Script?
问题描述
我创建了一些用于创建 GSheets FilterViews
的 Google Apps 脚本代码.我看不到如何将特定的 FilterViews
(不是 Filters
)应用于当前工作表.
我的代码设置了 FilterSettings,然后将其应用于基本过滤器,但我在文档中看不到如何将类似的方法应用于 filterview.
if (condition!=0 && hiddenValues.length>0){filterSettings['criteria'][COLUMNS.ASSIGNEE.pos] = {隐藏值":隐藏值,'条件':条件};}否则如果(hiddenValues.length> 0){filterSettings['criteria'][COLUMNS.ASSIGNEE.pos] = {隐藏值":隐藏值,};}否则如果(条件!= 0){filterSettings['criteria'][COLUMNS.ASSIGNEE.pos] = {'条件':条件};}//使用 BASIC FILTER 而不是 FILTER VIEW无功请求 = {设置基本过滤器":{过滤器":过滤器设置}}Sheets.Spreadsheets.batchUpdate({'requests': [request]}, ss.getId());
看过这个文档:-
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/sheets#FilterViewhttps://developers.google.com/sheets/api/reference/rest/v4/FilterCriteriahttps://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#ConditionTypehttps://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#ConditionValue
我可以看到它在历史上不受支持,但我想知道是否仍然如此.
如果我查看请求 API,我可以看到有以下请求类型:-
- addFilterView
- 删除过滤器视图
- updateFilterView
- 重复过滤器视图
没有 SetFilterView.
通过 GAS 激活过滤器视图的正确方法是什么.
问题:
FilterView
无法从应用程序脚本设置
解决方法:
在此解决方法中,主要缺点是,您需要通过 url 打开 Google 表格,并且该 url 选项卡/窗口将在后台运行.您还可以使用简单的 =HYPERLINK()
公式轻松完成此操作,使用相同的逻辑,无需任何应用程序脚本.
- FilterViews 将以下 url 哈希附加到工作表编辑 url:
gid=SHEET_ID&fvid=FILTERVIEW_ID
- 发布一个网络应用(
HookService
),明确用于打开 Google 表格网址. - 使用网络应用程序,可以更改 Google 表格网址.
- 可以使用过滤视图按钮在 Google 表格中打开边栏
- Sidebar 然后将连接到网络应用程序(通过
window.postMessage
)并命令它更改 Google 表格 url.这是必需的,因为侧边栏不能直接更改 Google 表格网址,但网络应用可以.
示例脚本:
WebApp:hookService.html
<html lang="en"><头><meta charset="UTF-8";/><title></title>头部><身体><h1><span>开始</span>Google 工作表上的 HookService....</h1><div id="main"><p>这是设置过滤视图所必需的</p><p>请允许弹出窗口继续......</p><标签>您的 Google 表格 ID:<输入id=工作表"类型=文本"值=<?=id?>"自动完成/></label><br/><label>侧边栏原点:<input id="origin";类型=网址";自动完成/></label><br/><button>开始挂机</button>
<脚本>var win1, win1url;让 [div,输入,原点,按钮] = [...document.querySelectorAll('div,input,button'),];origin.value = window.location.origin;函数 openSheets() {win1 = window.open((win1url ='https://docs.google.com/spreadsheets/d/' + input.value + '/edit'),'_空白的');如果(赢1){document.querySelector('span').textContent = '运行';window.addEventListener('message', receiveMessage, false);div.innerHTML = '关闭此窗口以停止 Hook 服务';} 别的 {alert('必须在此站点上禁用弹出窗口阻止程序才能运行钩子');}}openSheets();button.addEventListener('click', openSheets);函数接收消息(事件){//[安全] 检查事件来源if (event.origin !== origin.value) {console.error('不允许来源');alert('来源不允许');返回;}win1.location.replace(win1url + event.data);}