Deploying container-bound Google Apps Script as Web App

橙三吉。 提交于 2020-01-01 19:38:10

问题


I'm creating a spellchecker using the Google Docs API in an Apps Script (just a script that extends the functionality of a Google Doc), and I wan't to make this service available to users whom download it as a Web App. Problem is that when I've made my (Container-bound) script in the script editor, it is only available in the Google Document through which I created it - that is, if i open a new document, I cannot use the script.

If I "Deploy as Web App", make it available to everyone and paste the given URL, I get an error message saying that the script needs a function called doGet(), which is not in my script.

How do I go about to publish my script as a regular web app?


回答1:


I would proceed by creating two scripts: the core functionality would be deployed as a Web App and a simple container-bound script would offer an interface to call the Web App.

Since the Web App is not bound to a document you may want to follow this scheme:

function doGet(e){
    if(e.parameter.docId){
        doStuff(DocumentApp.openById(e.parameter.docId));
    }
}

Now when you deploy the app you will get a link that gives you access to the functionality.

From the container-bound script you can add some UI (e.g. an Anchor element in a side-panel) that links to the web app with the appropriate parameters

ScriptApp.getService().getUrl() + "?docId=" + DocumentApp.getActiveDocument().getId()

or use UrlFetchApp to get the results and display them in the UI.

Unfortunately this is not the same as adding the functionality across all your documents automatically, but rather a way to install only a relatively lightweight hook in each document where you want to add the functionality, instead of the full script. I am not aware of any method that can achieve that. Note that when you make a copy of a document, the copy will contain all its scripts so you can create a template for documents that need the additional functionality. This can get ackward though if you wish to mix and match multiple scripts.

The advantage of my method is that if you modify the core functionality, the change is immediately available to all your documents making use of it, with no need to update their scripts. On the other hand if the container-script needs to interact heavily with the UI it may get complicate and reduce the usefulness of separating it in two scripts.




回答2:


The answer is in your question : the main function of your script (the one that builds the UI) must be called doGet() (this is the conventional entry point of any GAS webapp, the function that you implicitly call when opening the webapp url)... but I'm afraid this will not solve your problem...

Even if I don't know what is in your script and how you wrote it I guess that it refers to the doc in which you bound it as the "active document" and that will probably be the most important issue since a webapp has no document attached to it.

Beside that, how would a spell checker work as a standalone app ? could you explain more clearly what you want to do ?



来源:https://stackoverflow.com/questions/18374708/deploying-container-bound-google-apps-script-as-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!