Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable data filters on a range with Google Apps Script

Is there a way to enable data filters by script in a Google Sheet for a certain range? Currently this can be doen manually, but I do not want to select a range, then click 'Data', and then turn on filter.

I know that the filter will remain in an existing sheet. However, I try to apply a filter for a new spreadsheet that is generated via scripting.

My idea is:

function foo() {
  var spreadsheet = SpreadsheetApp.getActive();
  var infoSheet = spreadsheet.insertSheet('sheetName', spreadsheet.getNumSheets());

  infoSheet.getRange(1, 1, 5, 5). -> enable filter?
  ...
}

How can I achieve my goal programmatically?

like image 823
Lung Pang Avatar asked Aug 27 '13 04:08

Lung Pang


People also ask

How do I filter data in Google App Script?

Get the range that the filter applies to The below sample gets the filter on the active sheet, then uses the getRange() method from this class to log the range that the filter applies to. let ss = SpreadsheetApp. getActiveSheet(); // Gets the existing filter on the active sheet.

How do you filter with a range of values Google Sheets?

Google Sheets Filter Function Syntax the Google Sheet filter formula goes as follows: FILTER(range, condition1, [condition2, …]): range: This is the range of cells that you want to filter. condition1: This is the columns/row (corresponding to the column/row of the dataset), that returns an array of TRUEs/FALSES.

Does filtering a Google sheet filter for everyone?

Filter data for everyone Important: When you add a filter, anyone with access to your spreadsheet will see the filter too. Anyone with permission to edit your spreadsheet will be able to change the filter. On your computer, open a spreadsheet in Google Sheets.


3 Answers

Filters are now also available in the native Spreadsheet Service, without needing to activate the Sheets REST API via "Advanced Services".

The above Sheets REST API method, adapted for native Apps Script:

function applyFilter() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var dataSheet = ss.getActiveSheet();

  var toFilter = dataSheet.getDataRange();
  var filter = toFilter.createFilter();

  // Make some criteria to filter with.
  var fcb = SpreadsheetApp.newFilterCriteria();
  /* use FilterCriteria methods */
  fcb.whenCellNotEmpty();

  // Filter the range based on the 1st column:
  filter.setColumnFilterCriteria(1, fcb.build());
}

Further reading:

  • Filter Reference
  • FilterCriteria Reference
like image 103
tehhowch Avatar answered Nov 13 '22 16:11

tehhowch


Now you can apply filters using google sheets advanced service. First you need to turn on the Google Sheets API. For that follow the steps as mentioned in the below link:

https://developers.google.com/sheets/api/quickstart/apps-script

After that you can use the following function to apply filters.

function applyFilter() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssId = ss.getId();
  var dataSheet = ss.getActiveSheet();
  var lastRow = dataSheet.getLastRow();
  var lastColumn = dataSheet.getLastColumn();
  var sheetId = dataSheet.getSheetId();

  var filterSettings = {
    "range": {
      "sheetId": sheetId,
      "startRowIndex": 0,
      "endRowIndex": lastRow,
      "startColumnIndex": 0,
      "endColumnIndex": lastColumn
    }
  };
  var requests = [{
    "setBasicFilter": {
      "filter": filterSettings
    }
  }];
  Sheets.Spreadsheets.batchUpdate({'requests': requests}, ssId);
}
like image 27
Akash Garg Avatar answered Nov 13 '22 17:11

Akash Garg


A short answer would be "No". It seems that GAS supports neither getting, nor setting Filter criteria for columns, at least according to this open issue.

An alternative solution would be to do the filtering and sorting on GAS side, and push the distinct results into different columns or sheets.

like image 42
eksperts Avatar answered Nov 13 '22 15:11

eksperts