'Google sheets: get a list of the sheet names giving a GS file url

I have been trying for many hours to get a script that returns the list of names of the sheets of a GS document after giving it the url of said document or the cell that contains said url in the document I am working on. I previously found this proposal that I modified slightly but it doesn't work for me: Create list of all sheet names in Google Apps Scripts

function getSheetNamesFromOtherGSfile(otherGSfileURL) {
  var spreadSheetAURL = otherGSfileURL;
  var sheetNameArray = [];

  var spreadSheetsInA = SpreadsheetApp.openByUrl(spreadSheetAURL).getSheets();

  sheetNameArray = spreadSheetsInA.map(function(sheet) {
    return [sheet.getName()];
  });

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = sheet.getRange(1, 1, sheetNameArray.length, 1);
  range.setValues(sheetNameArray);
}

Thanks.



Solution 1:[1]

According to the documentation you cannot call Spreadsheet.openById() within a custom function in a cell.

The approach I recommend is to create a container-bound function, see docs here. With these function, which can be called from a custom menu, there are none of those limitations.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neven Subotic