4

I have a spreadsheet with lots of named ranges, and I'd like to have a sort of table of contents which provides hyperlinks to jump to them.

In the UI I can create a hyperlink to a named range, which ends up with the format:

https://docs.google.com/spreadsheets/d/xxxxx/edit#rangeid=yyyyy

Where xxxx is a long spreadsheet id, and yyyy is a series of digits.

Since I have an awful lot of these, I'd like to use Google Apps Script to generate all of these links programatically. I can find the named range objects using Spreadsheet.getRangeByName, but I can't find a way to get a rangeid from this.

  • 1
    See the [NamedRange class documentation](https://developers.google.com/apps-script/reference/document/named-range) where you will find getId() – Karl_S Mar 01 '17 at 16:27
  • Sadly, that document is for Google Docs, not Google Sheets. I've hit it a few times this afternoon! – Peter Russell Mar 01 '17 at 16:31
  • Sorry, wrong one. [Try this one.](https://developers.google.com/apps-script/reference/spreadsheet/named-range) - with no id... – Karl_S Mar 01 '17 at 16:47
  • I found named ranges in [the external Google Sheets REST API](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#NamedRange) - and tried it in the [API explorer](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get) but sadly the namedRangeIds returned don't seem to work in this context. – Peter Russell Mar 01 '17 at 17:04
  • I ran a couple scripts looking to see if something was not documented and could not find anything. I show sections without using named ranges, but the actual range, so get the R1 notation of the range and generate the link as: https://docs.google.com/spreadsheets/d/xxxx/edit#gid=zzzz&range=A1:D6 where zzzz = the Sheet ID. Obtain it with `var key = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getSheetId();` – Karl_S Mar 01 '17 at 17:10
  • That's a decent workaround. Thanks. – Peter Russell Mar 01 '17 at 17:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137048/discussion-between-peter-russell-and-karl-s). – Peter Russell Mar 02 '17 at 10:48

3 Answers3

2

It doesn't appear that this is possible, but as a workaround, Karl_S suggested using a range link which does work:

function createNamedRangeUrl(name) {
  var root = SpreadsheetApp.getActiveSpreadsheet(); 
  var range = root.getRangeByName(name); 
  var sheetId = range.getSheet().getSheetId(); 
  var rangeCode = range.getA1Notation(); 
  return ("https://docs.google.com/spreadsheets/d/" + 
  root.getId() + "/edit#gid=" + sheetId + "&range=" + rangeCode); 
}
Community
  • 1
  • 1
2

You can get id of named range using Advanced Google service: Google Sheets API. Turn it on at Resources - Advanced Google services...

Then use spreadsheets.get to get your Google Sheet data including named ranges info:

var spreadsheetId = '...';

var ssData = Sheets.Spreadsheets.get(spreadsheetId);
var namedRanges = ssData.namedRanges;

Logger.log(namedRanges);

Result:

enter image description here


Note: namedRangeId returned by API is obfuscated (?) and you cannot use it directly to create link programmatically. For some reason it different from what seen in UI:

enter image description here

Kos
  • 4,890
  • 9
  • 38
  • 42
  • 1
    I presume these are the same ids returned by the REST API. As we both noticed, these sadly don't seem to work in links. – Peter Russell Apr 15 '20 at 15:27
-3
var fullSpreadsheetLink = "full spreadsheet link goes here"
var spreadsheetTabName = "tab name goes here"

var spreadsheet = SpreadsheetApp.openByUrl(fullSpreadsheetLink);
var sheet = spreadsheet.getSheetByName(spreadsheetTabName);

var myCell = sheet.getRange("A1");
var linkCell = sheet.getRange("A2")

var dataSourceUrl = myCell.getDataSourceUrl();
var urlArray = dataSourceUrl.split("&");

linkCell.setValue("=HYPERLINK(\"#"+urlArray[3]+"&"+urlArray[4]+"\",\"link to A1\")");
bpgriner
  • 381
  • 6
  • 14
  • 3
    Code only answers with no commentary are not useful. Explain your code, your process, your solution, especially since this is a late answer. – tehhowch Apr 06 '18 at 11:25
  • 1
    Range.getDataSourceUrl() returns a full link with &range= at the end. It's not a named range id. – lcrespilho Jul 23 '21 at 21:56