Home > database >  Hide/Unhide Rows Excluding Specific Row
Hide/Unhide Rows Excluding Specific Row

Time:01-14

I am on basics of appscript and learning it progressively with the help of this community. Any help on below will be appreciated.

I am trying to design a script which hides and unhides rows on change of selection and for that I got a solution from question posted at below link.

Google Sheet Hide/Unhide Rows Using Appscrit Unhide

Below is the code given in above link

function onEdit(e) {
const sh = e.range.getSheet();
const rg = e.source.getRangeByName("NamedRange1");
const sr = rg.getRow();
const sc = rg.getColumn();
const er = sr   rg.getHeight() - 1;
const ec = sc   rg.getWidth() - 1;
if (sh.getName() == "Sheet3" && e.range.columnStart >= sc && e.range.columnStart <= ec 
&& e.range.rowStart >= sr && e.range.rowStart <= er && e.value) {
//e.source.toast("Flag1")
const sh2 = e.source.getSheetByName("Sheet2");
const vs = sh2.getDataRange().getValues();
vs.forEach((r, i) => {
if (r.every(e => e == '')) {
if (e.value == "A") {
sh2.hideRows(i   1);
} else {
sh2.showRows(i   1)
}
}
});
}
}

The code is given proper result but I want a bit modification in the same. The unhide command of the code unhides all the rows of the sheet, however I want the code to unhide all the rows except first row of the sheet.

Any help on above will really be appreciated.

CodePudding user response:

From The unhide command of the code unhides all the rows of the sheet, however I want the code to unhide all the rows except first row of the sheet., how about the following modification?

From:

vs.forEach((r, i) => {
if (r.every(e => e == '')) {
if (e.value == "A") {
sh2.hideRows(i   1);
} else {
sh2.showRows(i   1)
}
}
});

To:

vs.forEach((r, i) => {
  if (r.every(f => f == '')) {
    if (e.value == "A") {
      sh2.hideRows(i   1);
    } else if (i > 1) {
      sh2.showRows(i   1);
    }
  }
});
  • When this is reflected in your script, sh2.showRows(i 1) is run when e.value != "A" and i > 1. By this, the 1st row is skipped.
  • And also, in the case of r.every(e => e == ''), e has already been used with the event object. So, I changed it to f.
  • Related