根据单元格值隐藏工作表
Hide Sheets Based on a Cell Value
我对学习应用程序脚本还很陌生,并查看了 over/tried 来编辑 脚本,但我没有得到我想要的结果。我有一个标题为 "Menu" 的 sheet,我希望用户从单元格 A2 中的三个不同下拉选项(例如蓝色、黄色、绿色)中选择 select。然后我想根据 selection 隐藏不同的 sheets。因此,如果用户 selects "Blue" 我只希望以单词 "Blue" 开头的 sheets 可见 + "Menu" sheet其余的要隐藏起来。黄色和绿色也一样。请注意,每种颜色有 13 个 sheet。
非常感谢任何帮助。
试试这个代码:
function onEdit(e)
{
//filter the range
if (e.range.getA1Notation() == "A2")
{
// get value of cell (yellow||green||...)
onlySheet(e.value)
}
}
function onlySheet(str)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
//get all sheets
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++)
{
//get the sheet name
var name = sheets[i].getName();
// check if the sheet is not the "Menu" sheet
if (name != "Menu")
{
// check if the name of the sheet contains the value of the cell, here str
//if it does then show sheet if it doesn't hide sheet
if (name.match(new RegExp(str, "gi")))
sheets[i].showSheet();
else
sheets[i].hideSheet();
}
}
}
这是 @JSmith's 的另一种实现方式,使用 Sheets REST API 更有效地隐藏和取消隐藏大量 sheet。
要从 Apps 脚本使用 Sheets REST API,您首先需要 enable it, as it is an "advanced service。"
Sheets API 方法使您能够使用数据的 JavaScript 表示,而不需要重复与 Spreadsheet 服务交互(例如检查每个 sheet的名字)。此外,批处理 API 调用作为一个操作处理,因此所有可见性更改都会同时反映,而 Spreadsheet 服务的 showSheet()
和 hideSheet()
方法在每次调用后刷新到浏览器。
var MENUSHEET = "Menu";
function onEdit(e) {
if (!e) return; // No running this from the Script Editor.
const edited = e.range,
sheet = edited.getSheet();
if (sheet.getName() === MENUSHEET && edited.getA1Notation() === "A2")
hideUnselected_(e.source, e.value);
}
function hideUnselected_(wb, choice) {
// Get all the sheets' gridids, titles, and hidden state:
const initial = Sheets.Spreadsheets.get(wb.getId(), {
fields: "sheets(properties(hidden,sheetId,title)),spreadsheetId"
});
// Prefixing the choice with `^` ensures "Red" will match "Reddish Balloons" but not "Sacred Texts"
const pattern = new RegExp("^" + choice, "i");
// Construct the batch request.
const rqs = [];
initial.sheets.forEach(function (s) {
// s is a simple object, not an object of type `Sheet` with class methods
// Create the basic request for this sheet, e.g. what to modify and which sheet we are referencing.
var rq = { fields: "hidden", properties: {sheetId: s.properties.sheetId} };
// The menu sheet and any sheet name that matches the pattern should be visible
if (s.properties.title === MENUSHEET || pattern.test(s.properties.title))
rq.properties.hidden = false;
else
rq.properties.hidden = true;
// Only send the request if it would do something.
if ((!!s.properties.hidden) !== (!!rq.properties.hidden))
rqs.push( { updateSheetProperties: rq } );
});
if (rqs.length) {
// Visibility changes will fail if they would hide the last visible sheet, even if a later request in the batch
// would make one visible. Thus, sort the requests such that unhiding comes first.
rqs.sort(function (a, b) { return a.updateSheetProperties.properties.hidden - b.updateSheetProperties.properties.hidden; });
Sheets.Spreadsheets.batchUpdate({requests: rqs}, initial.spreadsheetId);
}
}
在使用 Google 的各种 REST APIs 时,有相当多的资源需要熟悉:
- Google APIs Explorer(交互式请求测试)
- Google Sheets REST API Reference
- Partial Responses(又名 "fields" 参数)
- Determining method signatures
- google-sheets-api
在 54 sheet 的工作簿中进行了一些测试,其中我使用工作表 API 应用了一些更改并使用@JSmith 的代码来恢复更改,显示了 API 方法大约快 15 倍,根据 console.time
和 console.timeEnd
测量。 API 更改耗时 0.4 到 1.1 秒(平均 1 秒),而 Spreadsheet 服务方法耗时 15 到 42 秒(平均 20 秒)。
我对学习应用程序脚本还很陌生,并查看了 over/tried 来编辑
非常感谢任何帮助。
试试这个代码:
function onEdit(e)
{
//filter the range
if (e.range.getA1Notation() == "A2")
{
// get value of cell (yellow||green||...)
onlySheet(e.value)
}
}
function onlySheet(str)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
//get all sheets
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++)
{
//get the sheet name
var name = sheets[i].getName();
// check if the sheet is not the "Menu" sheet
if (name != "Menu")
{
// check if the name of the sheet contains the value of the cell, here str
//if it does then show sheet if it doesn't hide sheet
if (name.match(new RegExp(str, "gi")))
sheets[i].showSheet();
else
sheets[i].hideSheet();
}
}
}
这是 @JSmith's
要从 Apps 脚本使用 Sheets REST API,您首先需要 enable it, as it is an "advanced service。"
Sheets API 方法使您能够使用数据的 JavaScript 表示,而不需要重复与 Spreadsheet 服务交互(例如检查每个 sheet的名字)。此外,批处理 API 调用作为一个操作处理,因此所有可见性更改都会同时反映,而 Spreadsheet 服务的 showSheet()
和 hideSheet()
方法在每次调用后刷新到浏览器。
var MENUSHEET = "Menu";
function onEdit(e) {
if (!e) return; // No running this from the Script Editor.
const edited = e.range,
sheet = edited.getSheet();
if (sheet.getName() === MENUSHEET && edited.getA1Notation() === "A2")
hideUnselected_(e.source, e.value);
}
function hideUnselected_(wb, choice) {
// Get all the sheets' gridids, titles, and hidden state:
const initial = Sheets.Spreadsheets.get(wb.getId(), {
fields: "sheets(properties(hidden,sheetId,title)),spreadsheetId"
});
// Prefixing the choice with `^` ensures "Red" will match "Reddish Balloons" but not "Sacred Texts"
const pattern = new RegExp("^" + choice, "i");
// Construct the batch request.
const rqs = [];
initial.sheets.forEach(function (s) {
// s is a simple object, not an object of type `Sheet` with class methods
// Create the basic request for this sheet, e.g. what to modify and which sheet we are referencing.
var rq = { fields: "hidden", properties: {sheetId: s.properties.sheetId} };
// The menu sheet and any sheet name that matches the pattern should be visible
if (s.properties.title === MENUSHEET || pattern.test(s.properties.title))
rq.properties.hidden = false;
else
rq.properties.hidden = true;
// Only send the request if it would do something.
if ((!!s.properties.hidden) !== (!!rq.properties.hidden))
rqs.push( { updateSheetProperties: rq } );
});
if (rqs.length) {
// Visibility changes will fail if they would hide the last visible sheet, even if a later request in the batch
// would make one visible. Thus, sort the requests such that unhiding comes first.
rqs.sort(function (a, b) { return a.updateSheetProperties.properties.hidden - b.updateSheetProperties.properties.hidden; });
Sheets.Spreadsheets.batchUpdate({requests: rqs}, initial.spreadsheetId);
}
}
在使用 Google 的各种 REST APIs 时,有相当多的资源需要熟悉:
- Google APIs Explorer(交互式请求测试)
- Google Sheets REST API Reference
- Partial Responses(又名 "fields" 参数)
- Determining method signatures
- google-sheets-api
在 54 sheet 的工作簿中进行了一些测试,其中我使用工作表 API 应用了一些更改并使用@JSmith 的代码来恢复更改,显示了 API 方法大约快 15 倍,根据 console.time
和 console.timeEnd
测量。 API 更改耗时 0.4 到 1.1 秒(平均 1 秒),而 Spreadsheet 服务方法耗时 15 到 42 秒(平均 20 秒)。