首页 > 解决方案 > 取消隐藏 google 表格行的脚本无法始终如一地工作

问题描述

我只是在学习使用脚本,无法让脚本隐藏和显示要在我想应用脚本的工作表上工作的特定行。我有一个作为沙盒创建的谷歌表,它确实可以工作,但是一旦我复制脚本并将脚本分配给一个按钮,“显示”功能就不起作用,而隐藏功能继续。

我要做的就是隐藏第 3 行,然后能够取消隐藏此工作表上的同一行。

/** @OnlyCurrentDoc */

function hide() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('3:3').activate();
  spreadsheet.getActiveSheet().hideRows(spreadsheet.getActiveRange().getRow(), spreadsheet.getActiveRange().getNumRows());
  spreadsheet.getRange('4:4').activate();
};

function show() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('4:4').activate();
  spreadsheet.getActiveSheet().showRows(3, 1);
};

没有错误,脚本运行完成,工作表没有任何可见的变化(该行仍然隐藏)

标签: google-apps-scriptgoogle-sheetsgoogle-sheets-macros

解决方案


尝试这个

/**
 * This hides the row 3 of the active sheet.
 *
 */ 
function hide(){
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.hideRow(3);
}

上面的代码是一个简化版本。由于您只想隐藏一行,因此请使用hideRow(rowNum).

参考


推荐阅读