自动隐藏/取消隐藏Google Spreadsheet行

时间:2019-01-22 08:31:48

标签: google-sheets spreadsheet

我正在尝试制作Google电子表格脚本。

当单元格的值更改为指定值时,我希望它触发隐藏/取消隐藏行。

到目前为止,这是我的脚本:

function autoHide() {

  var ss = SpreadsheetApp.getActive()
  var sheet = SpreadsheetApp.getActiveSheet()
  var cell = ss.getActiveCell()
  var cell1 = ("C12");

  if (cell1 == "Others" ) {
    ss.getActiveSheet().showRows(14, 3);
  }

  else if (cell1 == "Additional Discount/s (over and above 25%)" ) {
    ss.getActiveSheet().showRows(17, 4);
  }

  else {
    ss.getActiveSheet().hideRows(14, 7)
  }
}

Image of sheet

1 个答案:

答案 0 :(得分:1)

您的问题在某些方面尚不清楚,因此我假设您想在编辑 C12 时跟踪单元格 C12 的值。

SQL> with test (col) as
  2      (select 'a98c100e' from dual)
  3  select
  4    replace(replace(col, regexp_substr(col, '\d+', 1, 1), chr(regexp_substr(col, '\d+', 1, 1))),
  5                         regexp_substr(col, '\d+', 1, 2), chr(regexp_substr(col, '\d+', 1, 2))) result
  6  from test;

RESULT
--------------------
abcde

SQL>

如果要在任何单元格被编辑时随时运行该功能,请使用此代码。

如果 C12 是公式且未手动更新,则很有用。

function autoHide() {

  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  var cell = ss.getActiveCell();
  var value = cell.getValue();

  var cellRef = cell.getA1Notation();

  if (cellRef !== "C12") return;
  // This isn't the cell we are looking for, stop executing the function.

  if (value === "Others" ) {

    sheet.showRows(14, 3);

  } else if (value === "Additional Discount/s (over and above 25%)" ) {

    sheet.showRows(17, 4);

  } else {
    sheet.hideRows(14, 7);
  }
}