Programmatically build a hyperlink to a named range in Sheets
Asked Answered
H

3

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.

Houston answered 1/3, 2017 at 16:18 Comment(7)
See the NamedRange class documentation where you will find getId()Thorpe
Sadly, that document is for Google Docs, not Google Sheets. I've hit it a few times this afternoon!Houston
Sorry, wrong one. Try this one. - with no id...Thorpe
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.Houston
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();Thorpe
That's a decent workaround. Thanks.Houston
Let us continue this discussion in chat.Houston
H
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); 
}
Houston answered 2/3, 2017 at 17:8 Comment(3)
If you change something on a spreadsheet (add/remove rows) this link (with direct address) will break unfortunatelyPhraseology
Yes, it's unfortunate. In my situation it didn't matter, because I was regenerating all the links on each run.Houston
If you need not to navigate, but to submit a value to a given cell you can use this answer https://mcmap.net/q/672783/-passing-url-parameter-s-to-google-sheetsPhraseology
P
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

Prehistoric answered 9/4, 2020 at 12:18 Comment(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.Houston
M
-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\")");
Mayramays answered 5/4, 2018 at 22:46 Comment(2)
Code only answers with no commentary are not useful. Explain your code, your process, your solution, especially since this is a late answer.Medullated
Range.getDataSourceUrl() returns a full link with &range=<a1notation> at the end. It's not a named range id.Caresse

© 2022 - 2024 — McMap. All rights reserved.