Google Sheets – Find URL of the Current Sheet

google sheetslinks

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.

Best Answer

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")