简体   繁体   English

Google Apps Scripts / Google Sheet - 禁用所有活动过滤器,显示特定单元格并根据条件再次过滤

[英]Google Apps Scripts / Google Sheet - Disable all the active filters, Show a specific cell and filter again based on criteria

I'm trying to create a custom menu in my spreadsheet that will have 2 items:我正在尝试在我的电子表格中创建一个自定义菜单,其中包含 2 个项目:

1st item:第一项:

  • Disable all the active filters in a specific sheet.禁用特定工作表中的所有活动过滤器。
  • Show the last empty row in column B (in my example sheet: cell B15)在 B 列中显示最后一个空行(在我的示例表中:单元格 B15)
  • Add the value "> Add Content here <" in the last empty cell from column B在 B 列的最后一个空单元格中添加值 "> 在此处添加内容 <"

2nd item: Filtering the data using filter from column B & C and keep only the data for the last 60 days (It would be nice if the code is dynamic).第 2 项:使用 B 列和 C 列的过滤器过滤数据,并仅保留最近 60 天的数据(如果代码是动态的,那就太好了)。

I've started to work on the first item, but I'm stuck at the second part.我已经开始研究第一个项目,但我被困在第二部分。 Here is a sample of my spreadsheet with GAS to help you understand what I want to achieve.这是我的 GAS 电子表格示例,可帮助您了解我想要实现的目标。

https://script.google.com/u/0/home/projects/1rVwI5QMA35PksB9csKvMhxWpCHeLEbdKAo5UWb8y9GFGtdHpHsOwkva6/edit https://script.google.com/u/0/home/projects/1rVwI5QMA35PksB9csKvMhxWpCHeLEbdKAo5UWb8y9GFGtdHpHsOwkva6/edit

https://docs.google.com/spreadsheets/d/11ptkjp5UySKc0iwZTu3QUrJVKds6t4W5eMD39XVUbME/edit#gid=0 https://docs.google.com/spreadsheets/d/11ptkjp5UySKc0iwZTu3QUrJVKds6t4W5eMD39XVUbME/edit#gid=0

Any help on this would be appreciated.对此的任何帮助将不胜感激。

I believe your goal is as follows.我相信你的目标如下。

  • You want to put a value of > Add Content here < to the 1st empty row of column "B".您想将> Add Content here <的值放入“B”列的第一个空行。
  • You want to show the rows that the year and month values of columns "B" and "C" are the last 60 days.您想要显示“B”和“C”列的年份和月份值是过去 60 天的行。
  • You want to achieve this filter using the basic filter.您想使用基本过滤器来实现此过滤器。

When I saw your sample script, I found the following sample script.当我看到您的示例脚本时,我发现了以下示例脚本。

// first item - working
function prepareForContentAddition() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('Dashboard');
  var filter = sheet.getFilter();
  if (filter !== null) {  
    var range = filter.getRange(); 
    var firstColumn = range.getColumn();
    var lastColumn = range.getLastColumn();
    for (var i = firstColumn; i < lastColumn; i++) {
      filter.removeColumnFilterCriteria(i);
    }
  }
// Show the last empty row / blank cell in Column B and add "> Add Content here <" in it
  var lastRow = sheet.getRange('Dashboard!B3:B').getLastRow();
  sheet.getRange(lastRow, +1,1).activate();
}

// Apply 2 months filtering starting from today
function backToFiltering() {
}

When this script is modified, how about the following modification?当这个脚本被修改了,下面的修改呢?

Modified script:修改后的脚本:

function prepareForContentAddition() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('Dashboard');
  var filter = sheet.getFilter();
  if (filter !== null) {
    var range = filter.getRange();
    var firstColumn = range.getColumn();
    var lastColumn = range.getLastColumn();
    for (var i = firstColumn; i < lastColumn; i++) {
      filter.removeColumnFilterCriteria(i);
    }
  }

  // I added below script.
  Object.prototype.get1stNonEmptyRowFromBottom = function (columnNumber, offsetRow = 1) { // Ref: https://stackoverflow.com/a/44563639
    const search = this.getRange(offsetRow, columnNumber, this.getMaxRows()).createTextFinder(".").useRegularExpression(true).findPrevious();
    return search ? search.getRow() : offsetRow;
  };
  var row = sheet.get1stNonEmptyRowFromBottom(2);
  sheet.getRange(row + 1, 2).setValue("> Add Content here <");
}

And

function backToFiltering() {
  const date = new Date();
  date.setDate(date.getDate() - 60);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('Dashboard');
  var filter = sheet.getFilter();
  if (filter !== null) {
    filter.setColumnFilterCriteria(2, SpreadsheetApp.newFilterCriteria().whenNumberGreaterThanOrEqualTo(year).build()).setColumnFilterCriteria(3, SpreadsheetApp.newFilterCriteria().whenNumberGreaterThan(month).build());
  }
}
  • In this modification, I think that your expected situation can be obtained.在这个修改中,我认为可以得到你预期的情况。 2022, 8 and 2022, 9 are shown.显示2022, 82022, 9 But, when you want to show 2022, 7 , 2022, 8 and 2022, 9 , please modify them as follows.但是,当您要显示2022, 7 , 2022, 82022, 9时,请进行如下修改。

    • From

       filter.setColumnFilterCriteria(2, SpreadsheetApp.newFilterCriteria().whenNumberGreaterThanOrEqualTo(year).build()).setColumnFilterCriteria(3, SpreadsheetApp.newFilterCriteria().whenNumberGreaterThan(month).build());
    • To

       filter.setColumnFilterCriteria(2, SpreadsheetApp.newFilterCriteria().whenNumberGreaterThanOrEqualTo(year).build()).setColumnFilterCriteria(3, SpreadsheetApp.newFilterCriteria().whenNumberGreaterThanOrEqualTo(month).build());

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Apps 脚本 - 自动过滤活动工作表 - Google apps script - automatically filter the active sheet 有没有办法通过应用程序脚本在谷歌表中的表格上找到现有的过滤器? - Is there a way to find existing filters on a table in google sheet via apps scripts? 使用 Google 脚本从 Google 表格上的特定单元格命名新表格 - Naming a New Sheet with Google Scripts from a specific cell on a Google Sheet 如何根据谷歌应用程序脚本 GAS 中另一张工作表中列出的单元格值过滤出一张工作表上的行 - How to filter out rows on one sheet based on cell values listed in another sheet in google app scripts GAS 将 Google Apps 脚本从活动单元格中的打印数组更改为特定工作表和范围 - Change Google Apps Script from printing array from active cell to specific sheet and range 使用Google Apps脚本获取工作表中包含数据的特定行为空的所有行 - get all rows in a sheet with data where a specific column is empty using Google Apps Scripts 使用 Google Apps 脚本代码根据条件查找和替换 - Find and replace based on criteria with Google Apps Scripts Code 谷歌应用脚本:基于两个标准的正则表达式匹配,包括一个传递的变量 - Google apps scripts: regexmatch based on two criteria including a passed variable 在 Google Apps Scripts 中创建所有工作表名称的列表 - Create list of all sheet names in Google Apps Scripts 根据条件和单元格值将行复制到 Google 表格上的另一张表格 - Copy row to another sheet on Google Sheets based on criteria and cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM