【问题标题】:Google Sheets app script to hide rows in batch based on the las column of each tab谷歌表格应用脚本根据每个选项卡的 las 列批量隐藏行
【发布时间】:2019-03-18 06:26:24
【问题描述】:

我有一个有很多标签的谷歌表格,其中一些有不同的布局

我需要一个脚本来在所有选项卡上运行(并且能够排除一些选项卡)以隐藏每个选项卡上每一行的最后一列中包含 1 的行

这应该是批量隐藏,因为我有很多行,如果逐行隐藏它会超过执行时间

任何帮助将不胜感激

谢谢!

【问题讨论】:

标签: google-apps-script google-sheets hide batch-processing


【解决方案1】:

您必须阅读每张纸,跟踪最后一列中包含 1 的行的范围,并逐个隐藏这些范围。下面的代码试图做到这一点。看看它是否适用于你的情况。在 excludes 数组中输入要排除的工作表的确切名称。

function test() {
  // get spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  // get all sheets
  var sheets = ss.getSheets();

  // put exact sheet names in array to exclude, sample ['Sheet1', 'Sheet2']
  var excludes = [];

  for (var i = 0; i < sheets.length; i++) {
    // if sheet is to exclude,do nothing
    if (excludes.indexOf(sheets[i].getName()) != -1) continue;
    // hide rows of this sheet
    hideRows(sheets[i]);
  }
}

function hideRows(sheet) {
  // variables
  var start = null,
    range = 0,
    flag = false;
  // read all values
  var values = sheet.getDataRange().getValues();

  // for each row
  values.forEach(function(row, idx) {
    // if row last col is 1, set up start and range
    if (row[row.length - 1] == 1) {
      if (flag) {
        range++;
      } else if (!flag) {
        start = idx + 1;
        range++;
        flag = true;
      }
      return;
    }
    // row last col is not 1, hide last found range of rows
    // reset variables
    if (flag) {
      sheet.hideRows(start, range);
      flag = false;
      start = null;
      range = 0;
    }
  });
}

【讨论】:

  • 有没有办法只在一个函数中实现?
  • hideRows(sheets[i]); 函数中的test() 行可以替换为hideRows() 函数的所有内容。但是读起来会很乱。 2个功能有什么问题?更小鬼,它有用吗?你能测试吗?
猜你喜欢
  • 2018-04-13
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多