Goal: Click a button/select an option to hide/reveal a number of rows. Example, click/select on cell A4 that hides rows A5-A8 and another A45 that hides rows A46-A59. A button would be preferred but I'll take whatever. Bonus points if I can use a custom image.
I have a function that hides rows but it only works sometimes, could be slow (takes anywhere from 5s or 20s+, although I have 900 rows) or sometimes only hides some of the list regardless of length. I copied the function from somewhere else and I probably didn't implemented it in the best way, which might explain some problems, but here's how it works.
On B8 there's a dropdown list (data validation, list of items). There are two options, one is a blank (technically an inivisible character) and the other is "H". In column C, I copy this code in the specific cells I want to hide:
=IF(B8="H","Hide","Show")
And here is the script:
/** HideRow */
function onEdit()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow();
for( i=1 ; i<=lastRow ; i++) { // i <= lastRow
var status = sheet.getRange("C"+i).getValue(); // C = the column that writes Show/Hide
if (status == "Hide") { // status == "Hide"
sheet.hideRows(i);
}
if (status == "Show"){ // status == "Show"
sheet.showRows(i);
}
}
}
I will be adding more rows over time and I don't know if the script keeps track of the cell when it moves, which is why I wrote a line of code in individual cells (maybe an array would fix slowdown due to redundancy?). Although if it works better in the script than by all means do it. Would a named range be useful in this case?
I've looked at some others and haven't had luck, mostly because other than the variables I'm not sure what else I would need to change.