Anthony Madle
Anthony Madle

Reputation: 31

Hiding Rows using Google Apps Script

I'm looking to create almost a drop-down feature in Google Sheets. I was hoping to have arrows set that could potentially collapse rows when clicked.

It would be amazing if I could get it so each main "title" row has an arrow, that would cause the rows to either hide/unhide.

Since only the titles would be written in column A, I was thinking it might be possibly for a script to recognize the blank cells in between two filled ones?

So far I only have this, which is basically useless:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first three rows
sheet.hideRows(1, 3);

In a perfect world, the spreadsheet would look something like this, with the functionality to collapse the rows with the click of the arrow.

https://docs.google.com/spreadsheets/d/1-oGawFoPXjdHvadxXsSFxuZWWdOxN9ZgbvS10jelWZY/edit?usp=sharing

Not sure if this is possible, but if there is any hope/ideas I'd love to hear them.

Upvotes: 1

Views: 5128

Answers (2)

Cooper
Cooper

Reputation: 64032

function onEdit(e) {
  const sh=e.range.getSheet();
  const shts=['Sheet13'];
  if(shts.indexOf(sh.getName())!=-1 && e.range.columnStart==1 && e.value=="TRUE") {
    e.range.setValue("FALSE");
    sh.hideRows(Number(e.range.rowStart));
    e.source.toast('Row ' + e.range.rowStart + ' has been hidden.');
  }
}

Animation:

enter image description here

Upvotes: 3

Anees Hameed
Anees Hameed

Reputation: 6544

Instead, have you considered using the group row facility? You can just group and ungroup the rows to show/hide them.

  • Just select all the rows that you want to group,
  • right click select Group rows.

I have added the same in the spreadsheet that you have shared.

enter image description here

Upvotes: 1

Related Questions