15

In Google Sheets, how do I create a hyperlink to this sheet?

I have a sheet which I'm about to download as PDF for archive. It would be useful to include a link in the sheet, pointing to itself, so that later I could click the PDF link and open the sheet again.

Of course I could copy the URL from the address bar, and insert the hyperlink:

=hyperlink("https://docs.google.com/spreadsheets/d/asdf/edit#gid=0","Link")

but I'm looking for a more general method.

3
  • What do you mean by "a more general method"? May 31, 2016 at 11:50
  • To link to the current sheet, even if the sheet is duplicated and re-used.
    – tomnexus
    May 31, 2016 at 14:14
  • Looks that the answer by @Max_Makhrov fulfils that requirement. Did you already tried the suggested custom function? May 31, 2016 at 14:21

4 Answers 4

19

Open menu Tools → Script Editor and paste this code:

function getSheetUrl() {
  var SS = SpreadsheetApp.getActiveSpreadsheet();
  var ss = SS.getActiveSheet();
  var url = '';
  url += SS.getUrl();
  url += '#gid=';
  url += ss.getSheetId(); 
  return url;
}

Close The Script Editor.

And use this formula:

=hyperlink(getSheetUrl(),"Link")
4
  • Is it possible to do it without using the script editor?
    – pts
    Jan 25, 2019 at 17:26
  • Create the url like this: #gid=215455 where 215455 is your sheet id. Copy it from the browser address bar. Jan 28, 2019 at 7:05
  • 1
    Is it possible to do it within a formula, i.e. is there an equivalent of getSheetId() which I can call from a formula, without writing or using a script, e.g. =hyperlink(concatenate("#gid=",getSheetId()),"Link") ?
    – pts
    Jan 29, 2019 at 13:05
  • 1
    @pts, as far as I know, there's no built-in formula. I'd love to have such formula, too. Hope, Google will implement it soon. Jan 30, 2019 at 16:00
0

Notice that on the link you provided, there is a #gid=0 near the end of your link. Just copy this and put it inside the quotation marks.

Example:

=hyperlink("**#gid=0**","Link")
1
  • This doesn't work for me - I just get Invalid link. Also if I use just "#gid=0" then I get a plausible link to the same sheet, but only when that sheet is open. From a PDF it's a null link. I want to open the sheet in browser, when clicking the link in the PDF one day.
    – tomnexus
    May 19, 2022 at 16:40
0

Proposition: (based on @Max Makhrov's answer)

allowing to both call the function from the script or by precising a sheet name from script like iterating through sheets and building a list of sheet urls.

(That's actually what I'm trying to do and brought me here)

function getSheetUrl(sht) {
  var ss = SpreadsheetApp.getActive(),
      url = '';

  sht = sht ? sht : ss.getActiveSheet();

  url = (ss.getUrl() + '#gid=' + ss.getSheetId());

  return url;
}
1
  • Thank you for the corrections, I'm a bit new to participating on StackExchange. Apr 5, 2020 at 15:22
0

max's answer has a slight typo in it:

function getSheetUrl(sht) {
  var ss = SpreadsheetApp.getActive(),

  var SHT = sht ? sht : ss.getActiveSheet();

  var url = (ss.getUrl() + '#gid=' + SHT.getSheetId());

  return url;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.