EDIT: For an even more effective solution, see this post.
I have a Google Sheet (example here) with thousands of rows of data where dates are entered into Column A in order from top to bottom, with varying numbers of rows for each date, starting on Row 9. I have created a button to run a script (thanks to @Rene Olivo for the help given here) which hides all rows except for today and yesterday's dates.
What I'm trying to accomplish is to hide all entries except for the last two entered dates on the sheet, as opposed to today and yesterday's dates, but I am unsure how to specify that. This is my current script:
function hideRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Example");
var v = s.getRange("A:A").getValues();
var today = new Date();
var helper = new Date();
var yesterday = new Date(helper.setDate(helper.getDate()-1));
today.setHours(0, 0, 0, 0);
yesterday.setHours(0, 0, 0, 0);
for (var i = s.getLastRow(); i > 8; i--) {
var t = v[i - 1];
if (t != "") {
var u = new Date(t);
if (u < yesterday) {
s.hideRows(i);
}
}
}
}
What would the best way to handle this be? I'm also curious to know whether there is a more efficient way to do what this script is doing, as this one is taking a lot of time to complete.