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.

7
  • 1
    See the NamedRange class documentation where you will find getId() Commented Mar 1, 2017 at 16:27
  • Sadly, that document is for Google Docs, not Google Sheets. I've hit it a few times this afternoon! Commented Mar 1, 2017 at 16:31
  • Sorry, wrong one. Try this one. - with no id... Commented Mar 1, 2017 at 16:47
  • I found named ranges in the external Google Sheets REST API - and tried it in the API explorer but sadly the namedRangeIds returned don't seem to work in this context. Commented Mar 1, 2017 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: docs.google.com/spreadsheets/d/xxxx/… where zzzz = the Sheet ID. Obtain it with var key = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getSheetId(); Commented Mar 1, 2017 at 17:10

3 Answers 3

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); 
}
Sign up to request clarification or add additional context in comments.

3 Comments

If you change something on a spreadsheet (add/remove rows) this link (with direct address) will break unfortunately
Yes, it's unfortunate. In my situation it didn't matter, because I was regenerating all the links on each run.
If you need not to navigate, but to submit a value to a given cell you can use this answer stackoverflow.com/a/77453492/3525368
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

1 Comment

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.
-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\")");

2 Comments

Code only answers with no commentary are not useful. Explain your code, your process, your solution, especially since this is a late answer.
Range.getDataSourceUrl() returns a full link with &range=<a1notation> at the end. It's not a named range id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.