简体   繁体   English

如何使用谷歌应用脚本应用电子表格过滤器?

[英]How to apply filters of spreadsheet using google apps scripts?

I need to apply a filter on a spreadsheet and then apply the filter on the active spreadsheet.我需要在电子表格上应用过滤器,然后在活动电子表格上应用过滤器。

Tried using the Filter Class but not sure what is incorrect尝试使用过滤器 Class 但不确定什么不正确

'''
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("main sheet");
var dataMatrix1 = activeSheet.getRange(1, 1, activeSheet.getLastRow(), activeSheet.getLastColumn());

function applyFilter(){
  Logger.log("mark1");

  var filteredData = dataMatrix1.createFilter(); //filter created
  var a = 'a';
  filteredData.sort(1, false);
  filteredData.setColumnFilterCriteria(1 , a);

  Logger.log("Mark2");
}
'''

The spreadsheet has 2 rows with value = 'a' in the first column.电子表格有 2 行,第一列中的 value = 'a'。 Need to apply a filter to the sheet and filter out rows with value = 'a'.需要对工作表应用过滤器并过滤掉 value = 'a' 的行。

You are very close to accomplish your request;您非常接近完成您的要求; you only need to create a filter criteria instead of using the a variable.您只需要创建一个过滤条件而不是使用a变量。 You can see exactly which methods to use on the following code.您可以在以下代码中准确看到要使用的方法。 Also, the filtered string must be inside of an array, so I sightly modified your a variable.此外,过滤后的字符串必须在数组内,所以我修改了你a变量。

function applyFilter() {
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(
    "main sheet");
  var dataMatrix1 = activeSheet.getRange(1, 1, activeSheet.getLastRow(),
    activeSheet.getLastColumn());

  var filteredData = dataMatrix1.createFilter(); //filter created
  var a = ['a'];
  filteredData.sort(1, false);
  var filterCriteria = SpreadsheetApp.newFilterCriteria().setHiddenValues(a)
    .build();
  filteredData.setColumnFilterCriteria(1, filterCriteria);
}

Please, do not hesitate to ask for more help if you keep having problems.如果您仍然遇到问题,请不要犹豫,寻求更多帮助。

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

相关问题 如何使用 Google Apps 脚本引用外部电子表格 - How to reference an external spreadsheet with Google Apps Scripts 在Google Apps脚本中使用BigQuery连接到Google Spreadsheet - Using BigQuery in Google Apps Scripts to connect to Google Spreadsheet 使用Google Apps脚本自动从电子表格中删除数据或将数据插入到电子表格中 - Using Google Apps Scripts to automate deleting from and inserting data into a spreadsheet 如何在Google Apps脚本中获取发布到网络电子表格的ID - How to get the id for a published to the web spreadsheet in google apps scripts 如何在 Google Apps Script 电子表格嵌入脚本中使用确认按钮? - How to use a confirmation button in Google Apps Script spreadsheet embedded scripts? 如何将Google Apps脚本应用于整个Google表格电子表格? - How to apply a Google Apps Script to an entire Google Sheets spreadsheet? 通过 Google Apps Scripts #2 发布 Google 电子表格 - Publish a Google Spreadsheet through Google Apps Scripts #2 Google Scripts API:如何将onEdit函数应用于每个电子表格? - Google Scripts API: How to apply an onEdit function to every spreadsheet? 通过 Google Apps Scripts 发布 Google 电子表格 - publish a Google Spreadsheet through Google Apps Scripts Google Apps电子表格脚本会随机停止? - Google Apps Spreadsheet Scripts Randomly Stopping?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM