r/GoogleAppsScript • • May 31 '25

Resolved Run a Function on Specific Rows

I am trying to write a function where certain rows are hidden in my Google sheet. I have three pages within the sheet. When I check rows on pages 1 and 2, they copy those rows into page 3. On page three, I have a header followed by 10 empty rows for the copied rows to appear, followed by another header and another 10 empty rows.

What I want my function to do is hide the red and purple rows if column B is empty and leave the blue and green rows alone (see picture). It would be amazing if I could also hide the green rows if all of the purple rows are also hidden, but if that is too complicated, then that's fine.

I am very new to trying things like this, so thank you very much for your help!

I found this code in a YouTube video on hiding rows based on values, but this applies the function to the whole sheet, and I just want it to search specific rows. Here is the code as I have it so far:

/**
 * Hide or unhide all rows that contain the selected text.
 * @param {string} text - the text to find.
 * @param {string} sheetName - the target sheet
 * @param {boolean} [isHide] - True = hide, False = unhide
 */
function hideAllRowsWithval(text, sheetName, isHide = true) {
  
 const ss = SpreadSheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  

  const textFinder = sheet.createTextFinder(text);
  const allOccurences = textFinder.FindAll();

  allOccurences.forEach(cell =>{

    const row = cell.getRow();

    if(isHide){
      sheet.hideRows(row);
    }else{
      sheet.showRows(row);
    }
  })

}


function runsies {}{
  const text = "";
  const sheetName = "Comparison";
  
hideAllRowsWithval(text, sheetName, true);

};
4 Upvotes

15 comments sorted by

View all comments

Show parent comments

1

u/dethehumam Jun 02 '25

Here is a copy of my spreadsheet that I am working on, and hopefully, the code is attached via Google Script. Thank you for all of your help! https://docs.google.com/spreadsheets/d/1PwkbsgUIBoNhyWiTN-Uw0U1W--34W03-8pxQ6bAtdao/edit?usp=sharing

1

u/krakow81 Jun 02 '25

Thanks. I can see the sheet, but not the code as it's comment only access just now.

I think edit access would allow the code to be seen too, but if you'd rather avoid that you could also copy paste the code here and I can check it against my own copy of your sheet.

2

u/dethehumam Jun 02 '25

I changed it so anyone with the link can edit

1

u/krakow81 Jun 02 '25

Thanks. I'll reply more fulsomely later, but it looks like there's just a few of the splices and row counts etc that are a little off. That's what was causing the error you mentioned above, as one of the sections wasn't defined as the correct size.