Search code examples
google-apps-scriptgoogle-sheetsrowhide

How to avoid an infinite loop (hide rows) with onSelectionChange(e) please?


I made this script to hide the row of the cell I clicked. But obviously, once the row is hidden, my selection stays on a new cell and my script continues to hide the new row, without stopping. Makes sense but is there a way to only hide one row? I guess not since onSelectionChange(e) does its job here.

Thanks for your advice.

function onSelectionChange(e) {  
var range = e.range;  
var aa = SpreadsheetApp.getActiveSpreadsheet();
var bb = aa.getActiveSheet();
var activerow = range.getRow();
bb.hideRows(activerow)
}

Solution

  • Alright, here is my answer, maybe it will help someone :

    Here is the code to hide a row using a checkbox + the checkbox turns to false just after so you can click it again later :

    function onEdit(e) {
        if (e.value != "TRUE") return;
        SpreadsheetApp.getActiveSheet().hideRows(e.range.rowStart);
        e.range.setValue(false)
    }
    

    Here is the code to unhide all hidden rows/columns :

    function unhideallrowscolumns() {
        const ss = SpreadsheetApp.getActiveSpreadsheet();
        const sheet = ss.getSheetByName('cc')
        const allrows = sheet.getRange("A:A");
        const allcolumns = sheet.getRange("1:1");
        sheet.unhideRow(allrows);
        sheet.unhideColumn(allcolumns);
    }
    

    Thanks everyone.