英文:
Update all filter view ranges
问题
有没有一个可以一次更新工作表上所有筛选视图范围的应用脚本?
我有数百个筛选视图,手动更改将会很繁琐。
筛选视图都在一个名为“Data”的工作表上。我需要将范围从A1:AB3116更改为A1:AB9011。
谢谢任何帮助。
英文:
Is there a app script I can run that will update the ranges of all the filter views on a sheet at once?
I have hundreds of filter views, and it would be laborious to do it manually.
The filter views are all on a sheet called "Data'. I need to change the range from A1:AB3116 TO A1:AB9011
Thanks for any help.
答案1
得分: 1
我相信你的目标如下:
- 你想改变筛选视图的范围。
- 你想将所有数据表中的所有筛选视图的范围从“A1:AB3116”更改为“A1:AB9011”。
在这种情况下,以下示例脚本如何:
示例脚本:
请将以下脚本复制并粘贴到电子表格的脚本编辑器中,并在高级Google服务中启用Sheets API,然后保存脚本。
请确认sheetName
和obj
。在此示例中,使用了您提供的信息。
function myFunction() {
var sheetName = "data"; // 这是来自您的问题。
var obj = [{ before: "A1:AB3116", after: "A1:AB9011" }]; // 这是来自您的问题。
// 获取电子表格和工作表。
var ss = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = ss.getId();
var sheet = ss.getSheetByName(sheetName);
var sheetId = sheet.getSheetId();
// 将a1Notation转换为gridRange。
var o = obj.map(({ before, after }) =>
[before, after].map(r => {
var rng = sheet.getRange(r);
var rowStart = rng.getRow() - 1;
var rowEnd = rowStart + rng.getNumRows();
var colStart = rng.getColumn() - 1;
var colEnd = colStart + rng.getNumColumns();
return { sheetId, startRowIndex: rowStart, endRowIndex: rowEnd, startColumnIndex: colStart, endColumnIndex: colEnd };
})
);
// 为使用Sheets API的batchUpdate创建请求体。
var filterViews = Sheets.Spreadsheets.get(spreadsheetId, { ranges: [sheetName], fields: "sheets(filterViews)" }).sheets[0].filterViews;
var requests = filterViews.reduce((ar, { range, ...e }) => {
var check = o.find(([{ startRowIndex, endRowIndex, startColumnIndex, endColumnIndex }]) => range.startRowIndex == startRowIndex && range.endRowIndex == endRowIndex && range.startColumnIndex == startColumnIndex && range.endColumnIndex == endColumnIndex);
if (check) {
ar.push({ updateFilterView: { filter: { filterViewId: e.filterViewId, range: check[1] }, fields: "*" } });
}
return ar;
}, []);
// 使用创建的请求体请求Sheets API。
if (requests.length == 0) return;
Sheets.Spreadsheets.batchUpdate({ requests }, spreadsheetId);
}
-
运行此脚本时,将从“data”工作表中检索筛选视图。并且从检索到的筛选视图中搜索范围“A1:AB3116”,找到后将范围更改为“A1:AB9011”并更新筛选视图。
-
在此示例中,当您需要更改多个范围的更改时,您可以在“obj”中使用它们。
参考链接:
英文:
I believe your goal is as follows.
- You want to change the range of filter views.
- You want to change from "A1:AB3116" to "A1:AB9011" of all filter views in "data" sheet.
In this case, how about the following sample script?
Sample script:
Please copy and paste the following script to the script editor of Spreadsheet and enable Sheets API at Advanced Google services, and save the script.
Please confirm sheetName
and obj
. In this sample, your provided information is used.
function myFunction() {
var sheetName = "data"; // This is from your question.
var obj = [{ before: "A1:AB3116", after: "A1:AB9011" }]; // This is from your question.
// Retrieve spreadsheet and sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = ss.getId();
var sheet = ss.getSheetByName(sheetName);
var sheetId = sheet.getSheetId();
// Convert a1Notation to gridRange.
var o = obj.map(({ before, after }) =>
[before, after].map(r => {
var rng = sheet.getRange(r);
var rowStart = rng.getRow() - 1;
var rowEnd = rowStart + rng.getNumRows();
var colStart = rng.getColumn() - 1;
var colEnd = colStart + rng.getNumColumns();
return { sheetId, startRowIndex: rowStart, endRowIndex: rowEnd, startColumnIndex: colStart, endColumnIndex: colEnd };
})
);
// Create request body for using the batchUpdate of Sheets API.
var filterViews = Sheets.Spreadsheets.get(spreadsheetId, { ranges: [sheetName], fields: "sheets(filterViews)" }).sheets[0].filterViews;
var requests = filterViews.reduce((ar, { range, ...e }) => {
var check = o.find(([{ startRowIndex, endRowIndex, startColumnIndex, endColumnIndex }]) => range.startRowIndex == startRowIndex && range.endRowIndex == endRowIndex && range.startColumnIndex == startColumnIndex && range.endColumnIndex == endColumnIndex);
if (check) {
ar.push({ updateFilterView: { filter: { filterViewId: e.filterViewId, range: check[1] }, fields: "*" } });
}
return ar;
}, []);
// Reuest Sheets API using the created request body.
if (requests.length == 0) return;
Sheets.Spreadsheets.batchUpdate({ requests }, spreadsheetId);
}
-
When this script is run, the filter views are retrieved from "data" sheet. And, the range of
A1:AB3116
is searched from the retrieved filter views, and when it is found, the range is changed toA1:AB9011
and update the filter views. -
In this sample, when you change multiple changes of ranges, you can use them in
obj
.
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论