0

If I've created a web app in Google Apps Script using the HtmlService any hyperlinks I include in my template pages during development use the URL structure: https://script.google.com/a/macros/[domain]/s/[app dev id]/dev?page=form

Whereas once the app is deployed the links need to be:

https://script.google.com/a/macros/[domain]/s/[app exec id]/exec?page=form

To save me having to change every hyperlink when deploying the web app, what would be the most manageable solution?

Relative hyperlinks don't seem to work and using ScriptApp.getService().getUrl() will only return null if it isn't deployed.

pppery
  • 3,731
  • 22
  • 33
  • 46
h0dges
  • 663
  • 7
  • 15
  • Are you sure relative links don't work? Are you using them as `href="?page=form"` ? I remember it used to work fine... – azawaza Jun 15 '16 at 16:27
  • Related: http://stackoverflow.com/questions/11280978/href-in-htmlservice – Rubén Jun 16 '16 at 00:24
  • Related: http://stackoverflow.com/questions/15668119/linking-to-another-html-page-in-google-apps-script – Rubén Jun 16 '16 at 00:26
  • Create and add [mcve] – Rubén Jun 16 '16 at 00:29
  • I tested it now: `ScriptApp.getService().getUrl()` returns either /dev or /exec URL, depending on whether the deployed or development version is called. –  Jun 16 '16 at 05:10

1 Answers1

1

Thank you Rubén and soup for your help. This is the solution I was looking for:

Code.gs

function getScriptUrl() {
 var url = ScriptApp.getService().getUrl();
 return url;
}

Home.html

<?var url = getScriptUrl();?><a href='<?=url?>?page=form'>Go to Form</a>
h0dges
  • 663
  • 7
  • 15
  • I've realised that if any template pages include other pages i.e. js, css via: function include(filename) { return HtmlService.createHtmlOutputFromFile(filename) .getContent(); } Then you need to get the parent page to store the url then use innerHTML to retrieve it. – h0dges Jun 16 '16 at 11:43