ScriptApp.getService().getUrl() is broken?

560 Views Asked by At

why is ScriptApp.getService().getUrl() returning another URL while the web app is on another URL? after deploying the web app, the link to it is "https://script.google.com/macros/s/AKfycbyLfBVBICZES9w7N5sYzBYHv8oujoNG1FPnmYxGckZHNWSJh-nDWoFaWw5oZs7Cz3M6QA/exec" but on logger.log when i check the getUrl() it is "https://script.google.com/macros/s/AKfycbwFNB8lsKqyaECOo5Nzdj7SmYB26uXarHZD9WSYTLzmxIDLsoEN/exec"

i have a simple function in this web app to test:

function myFunction() {
 var url = ScriptApp.getService().getUrl();
 Logger.log(url)
}

when i try the link it says "Sorry, unable to open the file at this time.

Please check the address and try again."

what is happening?

*edit: can someone highlight to me what is the purpose of having ScriptApp.getService().getUrl() when it is not doing what it was intended to do?

What i'm trying to do: I cannot have more than 1 html page in the web app or to be specific, i cannot link to another html page within the web app

2

There are 2 best solutions below

1
Henrique Luna On

I'm having the same problem. If I work in development mode, ie with the url ending in /dev, ScriptApp.getService().getUrl() returns the wrong URL. However, if you deploy and use the published version link ending in /exec, it will work. I believe this is a bug. As suggested by this post ScriptApp.getService().getUrl() points to dev URL. How can I get it to point to exec production URL?, I have disabled Chrome V8 from running in general settings and it works.

0
ballatom On

I would like to revive this questions as, IMO, ScriptApp.getService().getUrl() is still behaving so that it makes it useless if you are not aware of its flaw and causes a waste of time for developers trying to use it.

To reproduce, do the following.

  1. Create a blank spreadsheet and edit the script (Extensions>App Script).
  2. Paste the following code :
function doGet(e) {
  lParameters = e.parameter;

  if (e.parameter.target == undefined) {
    textOutput = ContentService.createTextOutput("Welcome to the web app.");
    return textOutput;
  }
}

function myFunction() {
  Logger.log(ScriptApp.getScriptId());
  Logger.log(ScriptApp.getService().getUrl());
}
  1. Execute myFunction() from the code editor. The output should look like :
1gG__HE81QjvNBH6OvuyRdZO0qiJkbXM0ljwsJqXv0F6DYB9aVhp82F64
null
  1. Deploy your script for the first time (Deploy>New Deployment of type WebApp). I chose 'Anyone within myOrganisation' for 'Who has acess' but I am not sure that it plays a role. Here is the URL that I can copy from the deployment dialog : https://script.google.com/a/macros/ballat.be/s/AKfycbx62F71bp90wWlSTFPook8iPfupdaJYuigfk7td0Wes9yo5kAokEBelbFR-L3QOJ2tY/exec.
  2. Run myFunction()again from the editor. Here is the output :
1gG__HE81QjvNBH6OvuyRdZO0qiJkbXM0ljwsJqXv0F6DYB9aVhp82F64
https://script.google.com/a/ballat.be/macros/s/AKfycbxgbI9m7FDX8vOFtJ3_NIAETGsfEzkEXZJ83bkYRjI/dev
  1. Define a time-driven trigger that runs myFunction() every minute. Here is the output :
  • for the Head version :
1gG__HE81QjvNBH6OvuyRdZO0qiJkbXM0ljwsJqXv0F6DYB9aVhp82F64
https://script.google.com/a/ballat.be/macros/s/AKfycbx13dkx_Jd7f81iDhhToK5QBlz3zjCwcgPJNLINTTT3GrMbayk/exec
  • for the deployed version (Version 1)
1gG__HE81QjvNBH6OvuyRdZO0qiJkbXM0ljwsJqXv0F6DYB9aVhp82F64
https://script.google.com/a/ballat.be/macros/s/AKfycbx62F71bp90wWlSTFPook8iPfupdaJYuigfk7td0Wes9yo5kAokEBelbFR-L3QOJ2tY/exec

Let's compare them now (I have removed the first part of the URLs for the sake of readability) :

  • Code editor AKfycbxgbI9m7FDX8vOFtJ3_NIAETGsfEzkEXZJ83bkYRjI/dev

  • Trigger head AKfycbx13dkx_Jd7f81iDhhToK5QBlz3zjCwcgPJNLINTTT3GrMbayk/exec

  • Trigger v1 AKfycbx62F71bp90wWlSTFPook8iPfupdaJYuigfk7td0Wes9yo5kAokEBelbFR-L3QOJ2tY/exec

  • The first URL (when run from the code editor) outputs 'Welcome to the web app." or a more recent text if the code has been updated.

  • The second URL does not work at all.

  • The last URL ALWAYS outputs 'Welcome to the web app.".

Of course, unaware of this behaviour, I have been trying for several days to get my application working with the second URL... What a waste of time. I hope my answer saves time to others and motivates them to move away from Google Workspace-G-Cloud clutter before it becomes worse.