1

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.

1
  • Although I'm not sure whether I could correctly understand your question, I proposed a modified script as an answer. Please confirm it. If I misunderstood your question and that was not useful, I apologize. Commented Jan 12, 2023 at 11:38

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

@ Tanaike it works great... thanks for the wonderful and super fast solution....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.