I am using this script to hide all rows on my Example sheet that contain dates in Column A which are older than the two most recent dates in Column A of the sheet. It is ignoring any empty checkboxes on the sheet and is quite efficient.
My goal is to understand how to update this script so that it continues to function correctly if the number of header rows change, or the number of cells containing data in the A cells of the header change. For example, if the header rows increase to 10, and the number of A cells in the header change from 3 to 2, I change the two number 4's to 3 instead and update s.hideRows(9,row-7) to s.hideRows(11,row-10), but that does not hide enough data.
Here is the current script, which only works with 8 rows of headers and if 3 of those rows contain data in the A cells:
function hideRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Example");
var v = s.getRange("A:A").getValues(); //Modified the range to start directly from where the data starts
var rowCount = 0 //replacement for .getLastRow
for(var i = s.getLastRow(); i > -1; i--) { //going through all rows and finding the ones with no empty value
if (v[i] != "")
rowCount = rowCount + 1
}
rowCount = rowCount +4
var lastDate = s.getRange("Example!A"+ rowCount).getValue(); //Getting the value of the latest row on column A, getLastRow is replaced by rowcount
var previous = lastDate
var counter = 0
for(var i = rowCount; i > -1; i--) {
if ((v[i][0]).toString() !== previous.toString()){
counter = counter + 1
previous = (v[i][0])
var row = i
if (counter == 4){
break
}
}
}
s.hideRows(9,row-7)
}
How do I correctly update this script when the header information changes?