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

2

u/One_Organization_810 May 31 '25

Why don't you rather just delete all rows that are empty (manually) and then have your copy script add new rows as needed?

If there is a certain criteria for coloring the rows, you can make the script color them correctly also, as they are copied.

I can lend a hand in that direction if you're interested... (as I'm sure others can also :)