Google表格专用脚本:设置多个过滤器

时间:2018-07-20 18:35:01

标签: google-apps-script filtering google-sheets-api

我对将过滤器应用于多个值以使用Google Script隐藏它们有疑问。我有一个具有多个状态的项目跟踪程序,例如“按目标”,“已完成”,“已取消”,“已延迟”等。我想编写一个脚本来设置“状态”列的过滤器值,以使标记为“已完成”或“已取消”被隐藏。我已经在下面编写了用于隐藏“已完成”状态的代码段,如何在其中添加“已取消”?感谢您的帮助。

//Below code has been written to hide rows with status = Completed. 
//I'd like to modify it to hide rows with status = Completed AND rows with status = Canceled.

function SetFilters() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ssId = spreadsheet.getId();
var sheet = spreadsheet.getSheetByName("Project Tracker");

var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var sheetId = sheet.getSheetId();

var filterSettings = {
  "range": {
    "sheetId": sheetId,
    "startRowIndex": 0,
    "endRowIndex": lastRow,
    "startColumnIndex": 0,
    "endColumnIndex": lastColumn
  }
};

filterSettings.criteria = {};
var columnIndex = 2;
filterSettings['criteria'][columnIndex] = {
  'hiddenValues': ["Completed"]
};

var requests = [{
  "setBasicFilter": {
    "filter": filterSettings
  }
}];
Sheets.Spreadsheets.batchUpdate({'requests': requests}, ssId);

}

1 个答案:

答案 0 :(得分:2)

无需使用高级表格服务。我们可以使用Filter class可用。

MyObject