Programmatically publishing GAS scripts as web apps

99 views
Skip to first unread message

Robert Woodley

unread,
Jul 21, 2014, 10:09:25 AM7/21/14
to excel-r...@googlegroups.com
Hi - 

I am looking for a way to publish to the web via script.

I have a spreadsheet with script that others want to publish to their own audience. Think teachers publishing an assignment to students. I have written an install script as an HTML Service that they can run to get their own copies of the spread-sheets. These spread-sheets have the script needed. But how to have the users publish the spread-sheets? Of course they can go to Publish/Deploy as a web app, but many of my users will find this confusing and in fact it requires a bunch of instructions I'd sooner avoid. It seems my installer should be able to publish the web app after having copied the files to the users Google Drive.

Thoughts?

Thank you,
Bob Woodley

Bruce Mcpherson

unread,
Jul 21, 2014, 12:05:10 PM7/21/14
to excel-r...@googlegroups.com
would you share your sheet with me so I can take a look. br...@mcpher.com

Bruce


--
You received this message because you are subscribed to the Google Groups "Excel Liberation" group.
To unsubscribe from this group and stop receiving emails from it, send an email to excel-ramblin...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Woodley

unread,
Jul 22, 2014, 12:13:27 AM7/22/14
to excel-r...@googlegroups.com
I have done so. Basically, I just have a script with 2 functions:

function setup() {
  var url = "";
  try {
    ScriptApp.getService()
             .enable(ScriptApp.getService().Restriction.MYSELF);
    url = ScriptApp.getService().getUrl();
    Logger.log("Your web app is now accessible at the following URL:\n"
                   + url);
  } catch (e) {
    Logger.log("Script authorization expired.\nPlease run it again.");
    ScriptApp.invalidateAuth();
  }
}
function doGet() {
    return ContentService.createTextOutput("I'm alive!").setMimeType(ContentService.MimeType.JSON);
}


You run setup() once, it asks for authorization. Normal. You run it again, it gives you a URL, but it is the wrong URL. If you manually publish the sheet, then run it - it gives you the right URL (It says "I'm alive" if you hit the URL in a browser).

In fact this complies with the documentation which states: " Also note that at least one version of the script must be saved before this method will work."


So, I wonder how people are dealing with this? I would like to give script-enabled spread-sheets that teachers can publish to their students as web apps. But asking them to go thru the 'Publish as Web App' workflow is asking a lot.

Thanks.

Bruce Mcpherson

unread,
Jul 22, 2014, 4:37:33 AM7/22/14
to excel-r...@googlegroups.com
yes looks like a lot of people are having troubles with this issue - https://code.google.com/p/google-apps-script-issues/issues/detail?id=1703

I've just starred it, and you should too. 

I'm trying to figure out a workaround.. will get back to you

Bruce Mcpherson

unread,
Jul 22, 2014, 6:57:44 AM7/22/14
to excel-r...@googlegroups.com
Haven't got an answer yet I'm afraid , but what seems to be happening is that publishing is now version related - seems to be a new feature. So you get a different link for a new version. If you have a previous version published and enabled, it still works on the old published link, but when you disable it, it stops working. .getUrl() is returning the link of the previously published version.

here's where i pared it down to so far when I realized it was never going to work like this. Will continue to think on it.

function setup() {
  var url;
  
  // clean up last published
 
  try {
    ScriptApp.invalidateAuth();
    
  }
  catch(e) {
   Logger.log("failed to invalidate."+JSON.stringify(e));
  }

  // enable new one
  try {
  
    ScriptApp.getService()
      .enable(ScriptApp.getService().Restriction.ALL);
   // try to force anauthorization
    doGet();
  } 
  catch (e) {
    Logger.log("failed to authorize."+JSON.stringify(e));
        
  }
    
}
function doGet() {
    Logger.log("Your app is accessible at "+ScriptApp.getService().getUrl());
    return ContentService.createTextOutput("Your app is accessible at "+ScriptApp.getService().getUrl()).setMimeType(ContentService.MimeType.TEXT);
}

Robert Woodley

unread,
Jul 25, 2014, 12:02:13 AM7/25/14
to excel-r...@googlegroups.com
Interesting. I can't think of any reason one would disable a link.

I did star the issue - thanks.

Is there a REST api for ScriptApp?  Or Java API? Doesn't seem to be. Also is there somewhere I can see all my published web apps?

Thanks,
Bob
Reply all
Reply to author
Forward
0 new messages