Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 基于单元格值隐藏图纸_Google Apps Script_Google Sheets_Visibility_Google Sheets Api - Fatal编程技术网

Google apps script 基于单元格值隐藏图纸

Google apps script 基于单元格值隐藏图纸,google-apps-script,google-sheets,visibility,google-sheets-api,Google Apps Script,Google Sheets,Visibility,Google Sheets Api,我对学习应用程序脚本非常陌生,并查看/尝试编辑脚本,但我没有得到想要的结果。我有一个标题为“菜单”的表格,我希望用户从单元格A2中的三个不同下拉选项中进行选择(例如蓝色、黄色、绿色)。然后,我想根据选择隐藏不同的图纸。因此,如果用户选择“蓝色”,我只希望以“蓝色”开头的工作表可见+菜单工作表,其余的隐藏。黄色和绿色也一样。请注意,每种颜色有13张纸 非常感谢您的帮助。请尝试以下代码: function onEdit(e) { //filter the range if (e.range.

我对学习应用程序脚本非常陌生,并查看/尝试编辑脚本,但我没有得到想要的结果。我有一个标题为“菜单”的表格,我希望用户从单元格A2中的三个不同下拉选项中进行选择(例如蓝色、黄色、绿色)。然后,我想根据选择隐藏不同的图纸。因此,如果用户选择“蓝色”,我只希望以“蓝色”开头的工作表可见+菜单工作表,其余的隐藏。黄色和绿色也一样。请注意,每种颜色有13张纸

非常感谢您的帮助。

请尝试以下代码:

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();
    }
  }
}
函数onEdit(e)
{
//过滤范围
如果(例如,range.geta1表示法()=“A2”)
{
//获取单元格的值(黄色| |绿色| |……)
仅限表(e.值)
}
}
仅功能表(str)
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
//把所有的床单都拿来
var sheets=ss.getSheets();
对于(变量i=0;i
这是的替代实现,使用Sheets REST API可以更有效地隐藏和取消隐藏大量工作表

要使用应用程序脚本中的Sheets REST API,首先需要,因为它是一个“.”

Sheets API方法使您能够使用数据的JavaScript表示,而无需重复与电子表格服务交互(例如检查每个工作表的名称)。此外,批处理API调用作为一个操作处理,因此所有可见性更改都会同时反映,而电子表格服务的
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);
  }
}
在使用谷歌的各种REST API时,有很多资源需要熟悉:

  • (交互式请求测试)
  • (又名“字段”参数)

在一个有54张工作表的工作簿中进行了一些测试,其中我使用了工作表API来应用一些更改,并使用@JSmith的代码来恢复这些更改,结果表明,用
console.time
&
console.timeEnd
衡量,API方法大约快了15倍。API的更改时间从0.4秒到1.1秒(平均1秒),而电子表格服务方法的更改时间从15秒到42秒(平均20秒)。

欢迎使用堆栈溢出。提供链接对于上下文非常有用。但请在你的问题中包含源代码。谢谢@Shawn你能分享一下你的电子表格吗?我很高兴看到你在上面。@Jsmith-这是sheet Sheets API是Sheets REST API。REST描述API类型。与SOAP等相比,如果您想查看,我可能输入了错误的脚本,因为它似乎没有为我过滤(我的经验不足可能是罪魁祸首)。执行简单触发器函数时,只能在Stackdriver日志/执行记录中查看其错误。