79698156

Date: 2025-07-11 10:10:35
Score: 2
Natty:
Report link

The answer to the above question is here...

https://support.google.com/docs/thread/356832818?hl=en&sjid=10003399125056233914-EU

The solution uses @Cooper code as follows:

function allsheetslist() {
  var ss=SpreadsheetApp.getActive();
  var shts=ss.getSheets();
  var html='<table>';
  shts.forEach(function(sh,i){
    html+=Utilities.formatString('<tr><td><input type="button" value="%s" onClick="gotoSheet(%s);" /></td></tr>',sh.getName(),sh.getIndex());
  });
  html+='</table>';
  html+='<script>function gotoSheet(index){google.script.run.gotoSheetIndex(index);}</script>';
  var userInterface=HtmlService.createHtmlOutput(html)
  SpreadsheetApp.getUi().showSidebar(userInterface);
}

function gotoSheetIndex(index) {
  var ss=SpreadsheetApp.getActive();
  var shts=ss.getSheets();
  shts[index-1].activate();
}

@--Hyde then provides his solution to replace a section of the code with the following:

shts.forEach((sheet, i) => {
    const sheetName = sheet.getName();
    if (sheetName.match(/^(Sheet1|Sheet2|Another sheet|Fourth)$/i))
      html += Utilities.formatString('<tr><td><input type="button" value="%s" onClick="gotoSheet(%s);" /></td></tr>', sheetName, sheet.getIndex());
  });

This does exactly what I hoped it would. It enables me to specify the exact sheets I want in my sidebar.

I hope that this proves useful to others.

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Cooper
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: melrin