Home > Mobile >  Automatically hide rows with empty cells in predefined column - only hides non-empty cells
Automatically hide rows with empty cells in predefined column - only hides non-empty cells

Time:02-15

I currently have the following script but in contrast to my expectations it only hides the rows with non-empty cells in column F and keeps the one with empty cells unhidden...what is wrong? ==="" is correct right?

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange(6,6,274,1);
function onOpen() {
{

    //get the values to those rows
    var values = range.getValues();

    //go through every row
    for (var i=0; i<values.length; i  ){

        //if row value is equal to empty  
        if(values[i][0] === ""){

        //hide that row
        sheet.hideRows(i 1);   
        }   
    }
}

Also, is there a way to do this whole thing in multiple sheets but with different ranges without having to completely multiplying the whole script with different variables? I am quite new to AppScript and the "for every i" is still something I do not understand how it works...Thank you so much for your help!

Edit: Can I also add another if(values[i][0] > "") { sheet.showRows(i 1)}; to check for changes in the rows and unhide them again?

CodePudding user response:

function onOpen() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  const vs = sh.getRange(6, 6, 274, 1).getValues().flat();
  vs.forEach((e,i) => {if(!e)sh.hideRows(i   6);});
}

CodePudding user response:

Just like @TheWizEd has mentioned, you will have to use the same index when hiding the rows as well, otherwise you will just hide the rows which correspond to the i 1 index; so instead of sheet.hideRows(i 1) you will have to use sheet.hideRows(i 6).

  • Related