I have an input tab with 13 columns of data. I used the filter from Google Sheets to restrain the data and I want only the filtered data to be put in an output tab.
I tried using this Google Apps Script code for that, however it does not work as intended.
function showResults() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = [];
for (var i = 1; i < sheet.getLastRow(); i++) {
if(!sheet.isRowHiddenByFilter(i)) {
var row_data = sheet.getRange("A" + i + ":M" + i).getValues();
console.log(row_data);
data.push(row_data);
}
}
sheet.getRange('Results!A1:M' + (data.length + 1)).setValues(data);
}
The problem I noticed in the logs is that it only goes through the first row of the filtered data before exceeding the maximum execution time.
Any help is appreciated, especially if it's a Google Sheets feature I didn't know about and not necessarily the code.
Hi you must clone your sheet and use cloned object for fix your problem like this :
function showResults() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = [];
for (var i = 1; i < sheet.getLastRow(); i++) {
let cache = JSON.parse(JSON.stringify(sheet ))
if(!sheet.isRowHiddenByFilter(i)) {
var row_data = cache.getRange("A" + i + ":M" + i).getValues();
console.log(row_data);
data.push(row_data);
}
}
sheet.getRange('Results!A1:M' + (data.length + 1)).setValues(data);
}