3
votes

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.

3
See the NamedRange class documentation where you will find getId() - Karl_S
Sadly, that document is for Google Docs, not Google Sheets. I've hit it a few times this afternoon! - Peter Russell
Sorry, wrong one. Try this one. - with no id... - Karl_S
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. - Peter Russell
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(); - Karl_S

3 Answers

1
votes

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); 
}
1
votes

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

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