Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

Copy values of filter criteria google script

Is it possible to copy values after applying a filter? I want to ignore the hidden values. I need to filter a sheet with more than 2000 rows and if I use a loop it takes a long time. Then, I use this:

var filteredRangefec = range.createFilter()
.setColumnFilterCriteria(6,filterCriteria)
.setColumnFilterCriteria(9, filterCriteriafecha)
.getRange();//range.getFilter().remove();
}

But when i use GetValues take all values, filter and not filter


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to retrieve the data and manipulate it in Google Apps Script, you could create temporary sheet, copy filtered data to temporary sheet using method:copyTo() with copyPasteType PASTE_NORMAL and use method:getDataRange() & method:getValues() to retrieve the data.

Example Data:

Sample Data

I copied TheMaster answer here and added some features:

function getFilteredValues(){
  var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var newSheet = activeSpreadsheet.getSheetByName("Temporary");
  //check if existing, delete if yes
  if (newSheet != null) {
    activeSpreadsheet.deleteSheet(newSheet);
  }
  //create new sheet with name Temporary
  newSheet = activeSpreadsheet.insertSheet();
  newSheet.setName("Temporary");

  var dataSheet = activeSpreadsheet.getSheetByName("Sheet1");
  var toFilter = dataSheet.getDataRange();
  var filter = toFilter.createFilter();

  //create criteria
  var criteria = SpreadsheetApp.newFilterCriteria();
  criteria.whenNumberGreaterThan(1200);

  //filter first column using the criteria above
  filter.setColumnFilterCriteria(1, criteria.build());

  //copy filtered data to temporary sheet
  var sourceRange = dataSheet.getFilter().getRange();
  sourceRange.copyTo(
    newSheet.getRange('A1'),
    SpreadsheetApp.CopyPasteType.PASTE_NORMAL,
    false); 

  Logger.log(newSheet.getDataRange().getValues());
  activeSpreadsheet.deleteSheet(newSheet);   
}

Output:

Google Apps Script Logs

Note: You can also use this to paste the data to sheet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...