【问题标题】:How to (Google Scripts) After dropdown selection, trigger a script when a formula changes a cell value and filter out those rows?如何(谷歌脚本)下拉选择后,当公式更改单元格值时触发脚本并过滤掉这些行?
【发布时间】:2019-09-24 20:00:46
【问题描述】:

一般来说,我有一个谷歌表格,其中我在单元格 C2 中有一个下拉菜单,其中有多个选项。选项有 30 天、60 天、90 天。选择 30 天后,我需要从同一个电子表格中过滤掉某些数据行。 IE:用户在下拉列表中选择“30 天”(单元格 C2)。在同一个电子表格中,第 59 行到第 64 列第 1 列,我有公式 =C2 并在下拉选择时更改为“30 天”,这些应该被过滤掉。如果用户在下拉列表中选择“60 天”,第 59 到 64 行会重新出现。

我已经研究并尝试了这两种方法,但无法让它们完全适用于我的用例。

以下不读取公式中的值: https://yagisanatode.com/2018/05/26/how-to-hide-a-row-based-on-a-cell-value-in-google-sheets-with-filter-or-google-apps-script/

我无法让以下内容适用于我的用例: Trigger a script when a formula changes a cell value

var SHEET = "check in template";
// The value that will cause the row to hide. 
var VALUE = "30 Day Check In";
// The column we will be using 
var COLUMN_NUMBER = 3

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();

//Ensure on correct sheet.
  if(SHEET == activeSheet.getName()){
    var activeCell = ss.getRange("C2");

    //Ensure we are looking at the correct column.
    if(activeCell.getColumn() == COLUMN_NUMBER){
      //If the cell matched the value we require,hide the row. 
      if(activeCell == VALUE){
        activeSheet.hideRows(59,5);
      };
    };
  };
}

我希望过滤掉第 59 到 64 行,但没有触发任何内容。

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    试试这个:

    function onEdit(e) {
      var sh=e.range.getSheet();
      if(sh.getName()=='check in template' && e.range.columnStart==3 && e.range.rowStart==2){
        if(e.value=="30 Day Check In"){
          sh.hideRows(59,5);
        }
      }
    }
    

    无法通过在没有事件对象的情况下运行它来测试它。所以不要试图从脚本编辑器中运行它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多