Hide Sheets Based on a Cell Value

时间:2018-09-18 19:47:38

标签: google-apps-script google-sheets visibility

I'm pretty new to learning app script and looked over/tried to edit this script, but I'm not getting my desired result. I have a sheet titled "Menu" where I'm wanting a user to select from three different drop down options in Cell A2 (e.g. Blue, Yellow, Green). I then want to hide the different sheets based on the selection. So if a user selects "Blue" I want only the sheets that start with the word "Blue" to be visible + the "Menu" sheet and the rest to be hidden. Same for Yellow and Green. As a note there are 13 sheets for each color.

Any help with this is much appreciated.

2 个答案:

答案 0 :(得分:2)

这是@JSmith's answer的另一种实现,它使用Sheets REST API更有效地隐藏和取消隐藏大量工作表。

要使用Apps脚本中的Sheets REST API,您首先需要enable it,因为它是“ advanced service”。

Sheets API方法使您能够使用数据的JavaScript表示,而无需与Spreadsheet Service反复进行交互(例如,检查每个工作表的名称)。此外,批处理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);
  }
}

使用Google的各种REST API时,有很多资源需要熟悉:

在包含54张纸的工作簿中进行了一些测试,其中我使用了Sheets API进行了一些更改,而@JSmith的代码恢复了这些更改,结果显示,以{{1} }和console.time。 API更改从0.4更改为1.1s(平均1s),而Spreadsheet Service方法的更改则在15到42s(平均20s)之间。

答案 1 :(得分:0)

尝试此代码:

SELECT id,fn_remove_accents(name)  name
FROM main_creditperson 
WHERE name="Irene Olga López";

| id     | name             |
| ------ | ---------------- |
| 366354 | Irene Olga Lopez |