List all sheet names in Google Sheets via Google Apps Script

This Google Apps Script lists all sheet names from the Google Sheet that you currently have open. It starts listing them in the currently selected cell, then moves down and lists one sheet per cell. It also links that cell with the corresponding sheet. -SM, 2024-08-15

/**
 * This function prints the names of all sheets.
 * -SpreadsheetsMade, 2024-08-15
 */
function printLinkedSheetNamesInAndBelowSelectedCell() {

  var ss;
  var sheets;

  var activeSheet;
  var printSheetNamesToThisColumn;
  var printSheetNamesToThisRow;
  var selectedCell;

  var currentSheet;
  var currentSheetName;
  var currentSheetURL;
  var richText;

  var i;



  ss = SpreadsheetApp.getActiveSpreadsheet();

  sheets = ss.getSheets();

  activeSheet = ss.getActiveSheet();

  selectedCell = activeSheet.getActiveCell();

  printSheetNamesToThisRow = selectedCell.getRow();

  printSheetNamesToThisColumn = selectedCell.getColumn();



  for (i = 0; i < sheets.length; i++) {

    currentSheet = sheets[i];

    currentSheetName = currentSheet.getSheetName();

    currentSheetURL = "#gid=" + currentSheet.getSheetId();

    // Create link to sheet
    richText = SpreadsheetApp.newRichTextValue()
      .setText(currentSheetName)
      .setLinkUrl(currentSheetURL)
      .build();

    // Print sheet names
    activeSheet.getRange(printSheetNamesToThisRow + i, printSheetNamesToThisColumn).setRichTextValue(richText);
  }
}