Got a sheet containing dates in the first row.
I would like to hide on open all columns (some are already hidden manually) left of the column which date is closest to the current date
google sheets
Got a sheet containing dates in the first row.
I would like to hide on open all columns (some are already hidden manually) left of the column which date is closest to the current date
Best Answer
This requires a script with a function
onOpen()
. Since you are asking about dates and not times, I'm going to break my own advice "do not use.getValue
on date/time cells".Here is a script that does what you described: grabs the dates in the first row, compares them to the present time (ignoring the potential timezone issues: UTC in Google Script vs local time in Sheets), and picks the closest. Then hides the columns to the left of the closest.
Whether some of the columns were previously hidden doesn't matter.
You may want to play it safer by hiding one column less, with
sheet.hideColumns(1, imin-1);
to provide a cushion against timezone discrepancy. Also, nearest date could be before or after today's; this is also worth considering.