在Google表格中隐藏行编辑

时间:2016-11-21 06:46:10

标签: google-apps-script google-sheets

我需要一个由B2中的yes / no字段触发的脚本来隐藏Budget_01表格中的所有行,只要在B列中找到0,例如表格Budget_01:

BUDGET_01带有隐藏的表[B2] ="否"所有行都是未隐藏的

Budget_01
Hide Zero|No
Expense  | Amount | Frequency
Car loan  500.00   1
Gas       0        0
Music     0        1
Supplies  50       20
....
Books     0        0

BUDGET_01带有隐藏的表[B2] ="是"列B = $ 0隐藏的所有行

Budget_01
Hide Zero|Yes
Expense  | Amount | Frequency
Car loan  500.00   1
Supplies  50       20

这是我的尝试不起作用:

    function toggleRows() {

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName("Budget 01");  
    var cellValue = sheet.getRange("B2").getValue();
    var sheets = ss.getSheets();

      if(cellValue == 'No'){
          for(var i = 0, iLen = sheets.length; i < iLen; i++) {
            // get sheet
            var sh = sheets[i];

            // unhide rows
            var rRows = sh.getRange("A1:A");
            sh.unhideRow(rRows);
          }

        }

        if(cellValue == 'Yes'){
            ss.showRows(1, ss.getMaxRows());
            ss.getRange('B1:B')
                .getValues()
            .forEach(function (r, i) {
                if (r[0] !== '' && r[0].toString()
                    .charAt(0) == 0) ss.hideRows(i + 1)
            }); 
        }
    }

3 个答案:

答案 0 :(得分:1)

我没有使用yes / no下拉列表来启动脚本,而是插入了两个标有yes和no的图形按钮(插入&gt;绘图方形按钮)。我从每个按钮启动了这两个功能:

function hide01Rows() {
  // set up spreadsheet and sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet(), sheets = ss.getSheets();

  for(var i = 0, iLen = sheets.length; i < iLen; i++) {
    // get sheet
    var sh = sheets[i];

    sh.showRows(1, sh.getMaxRows());
    sh.getRange('B:B')
        .getValues()
    .forEach(function (r, i) {
        if (r[0] !== '' && r[0].toString()
            .charAt(0) == 0) sh.hideRows(i + 1)
    });
  }
}

function unhide0Rows() {
  // set up spreadsheet and sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet(), sheets = ss.getSheets();

  for(var i = 0, iLen = sheets.length; i < iLen; i++) {
    // get sheet
    var sh = sheets[i];

    // unhide rows
    var rRows = sh.getRange("A:A");
    sh.unhideRow(rRows);
  }
}

这不是最优雅的解决方案,但它能满足我的需求。

答案 1 :(得分:0)

请将toggleRows()的函数名更改为onEdit(),并更改&#34;否&#34;的B1。到&#34;是&#34;。这样,当编辑电子表格时,Budget_01 - Unhidden变为Budget_01 - 隐藏。如果我的解释错了,请告诉我。

答案 2 :(得分:0)

我使用forEach语句循环遍历else if条件,而不是使用Yes

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");         // Change for your sheet

  // Check the condition  
  var cellValue = sheet.getRange("B1").getValue();

  // To hide/unhide, you need a range, not the value. Split into to variables.
  var range = sheet.getDataRange();
  var values = range.getValues();

  // Check the data being returned
  Logger.log(cellValue);
  Logger.log(values);

  // If "No," unhide the entire range.
  if(cellValue == "No" || "no") {
    for(var i = 0; i < values.length; i++) {
      Logger.log("Unhide all rows.");
      sheet.unhideRow(range);
    }

  // If "Yes," loop through the data looking for 0's and hide the range.
  } else if (cellValue == "Yes" || "yes") {
    for(var i=0; i<values.length;i++) {
      if(values[i][1] == 0) {
        Logger.log("Found 0 at row " + i);
        sheet.hideRow(sheet.getRange(i+1,1));
      }
    }
  }
}