Requirements for a web application
Standalone and embedded scripts can be web applications if the following requirements are met:
- The script contains a doGet(e) or doPost(e) function
- the function returns an HTML service object HtmlOutput or Content service TextOutput
- Deploying a script as a Web application
To publish a script as a web application, you must:
- Write a new version of the script by selecting File > Manage Versions and clicking Save New Version.
- Publish the script by choosing Publish > Deploy as web app.
- In the Project version field select the version you have recorded.
Under Execute the app as select the type of authorization: the developer’s account or the user running the script.
Under Who has access to the app specify who has the right to run the application. The options depend on the type of your account, but usually contain the following fields: “Only myself” – only me; any member of your domain – any member of your domain, “Anyone” (with a Google account) – anyone with a Google account; “Anyone, even anonymous” – anyone, even anonymous.
Click on Deploy.
This will open a dialog with a message saying that the project has been deployed as a web application. This message contains two important URLs for your application:
The first is called the Current web app URL and ends in /exec. This is the URL of the published version of the deployed application, based on the latest entry.
The second is called latest code and ends in /dev. This link is only available to users with permission to edit the script. This instance of the application always runs the last recorded version of the script regardless of the formal version – and is intended for quick testing during development.
You can share a link to your app with anyone who wants to run it. You can also publish the app to the Chrome Web Store for public use.
Including a web app in the Google Site
Web apps can be included in the Google Site (sometimes called site gadgets).
To create a script linked to the Google Site you need to:
- Go to the site
- Click on the gear icon (right top corner of the screen).
- Select Manage site
- Click on Google Apps Scripts, and then on Add new script
After this, the process is similar to the one presented in the “Deploying the script as a Web application” section.
To connect a web app to the Google Site you need to:
- Enter the site
- Go to the page to which the application will be connected
- Click the pencil icon (editing mode)
- Select Insert > Google Apps Script in the menu
- Select the application from the list.
- Click on Select and then Save.
- Save the changes on the page and exit edit mode
URL options
When the user launches the app or the app receives an HTTP GET request, Apps Script runs the doGet(e) function. When the application receives an HTTP POST request Apps Script runs the doPost(e) function. In both cases, the argument e is an event parameter that contains information about the URL parameters.
For example you can pass parameters along with the URL, in this case the user’s name and age:
You can then display the parameters as follows:
function doGet(e) {
var params = JSON.stringify(e);
return HtmlService.createHtmlOutput(params);
}
In the previous example, doGet(e) returns the following object:
{
“queryString”: “username=jsmith&age=21”,
“parameter”: {
“username”: “jsmith”,
“age”: “21”
},
“contextPath”: “”,
“parameters”: {
“username”: [
“jsmith”
],
“age”: [
“21”
]
},
“contentLength”: -1
}
Credentials
The credentials of a web application depend on who runs it, the owner of the application or the user who visited the page on the site where it is located.