Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate range ID hyperlink, #rangeID=XXXXXXXXXX, in Google Sheets using Scripts?

So recently, Google added the feature where you can hyperlink to a specific cell, which is great. To do so, you use the "Insert link" feature on a specific cell, and then from the dropdown menu, you can click "Select a range of cells to link" where you then choose the cell or range in which you'd like to link to. In doing so, Google generates a very handy (and dynamic) ten-digit "Range ID".

It looks something like this: =HYPERLINK("#rangeid=1234567890","link")

What I can't figure out, though, is how to create this using a script.

It seems strange to me that this function wouldn't exist, since it's possible to do manually.

I can use the following code to generate a sheet ID, which comes in very handy when creating hyperlinks to jump between pages:

var ss = SpreadsheetApp.getActive(); 
var sheet1 = ss.getSheetByName(name); 
var gid = sheet.getSheetId();
sheet2.getRange("A1").setFormula('=hyperlink("#gid='+gid+'","'name'")');

And I can use the following code to create a link to a specific cell, but it's not dynamic and breaks once rows/columns are inserted into the sheet:

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var range = ss.getRangeByName("A10");
var sheetID = range.getSheet().getSheetId();
var rangeCode = range.getA1Notation();
sheet2.getRange("J10").setFormula('=hyperlink("https://docs.google.com/spreadsheets/d/'+ss.getId()+'/edit#gid='+sheetID+'&range='+rangeCode+'","link")');

There has to be a way to do something like this following code, but I can't find a way, currently:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRangeByName("A10");
var rangeID = range.**getRangeID()**;
sheet2.getRange("J10").setFormula('=hyperlink("#rangeID='+rangeID+'","link")');

I know that getRangeID() doesn't currently exist in the available options for Google Apps Script; however, is there something that does this function? It would be incredibly helpful if this command existed.

Any help out there? Is there another way to accomplish this that I'm missing?

Thanks for any help!

like image 872
Gergazoid Avatar asked Apr 12 '18 20:04

Gergazoid


People also ask

Can you hyperlink to a specific cell in Google Sheets?

Creating Links to Jump to Cell/Range in the Same Sheet Here is a step-by-step process to create a link and jump to specific cells: Select the cell where you want insert the link. Right-click on the cell. Click on Insert Link (you you can use the keyboard shortcut Control + K)


1 Answers

You can use a relative url. The answer in a linked document can be found here https://webapps.stackexchange.com/questions/44473/link-to-a-cell-in-a-google-sheets-via-url.

Example:

=hyperlink("#gid=0&range=A2",Bookmarks!A2)

I think #gid=0 represents the first sheet. Then I can specify the range in the sheet. I was unable to get named sheets working. Besides the first sheet/tab the others all have a #gid which you can find in the url of that sheet.

Ex:

=hyperlink("#gid=1756281106&range=A2",Bookmarks!A2)

like image 123
lastlink Avatar answered Oct 19 '22 17:10

lastlink