Join Scott Simpson for an in-depth discussion in this video Using Google data in your web app, part of Learning Google Apps Script (2013).
Let's take a look at using data from your Google environment, in a Web App. Here, I'm using the web App that I made in the last movie. First, I'm going to set up a function to return some information from my Google environment. This function returns the ratio of used storage to available storage, for my drive App. I'm showing this, because it's very basic to set up, and chances are it'll be different for everyone. You could just as easily return the number of unread messages in your Gmail. Some info from an analytics dashboard you have access to, or the contents of a spreadsheet you want to share this example.
So at this point, if I visit the web page. I'll draw a div and some text. And then my script will run. But I won't see the result of that script running unless I ask for something to come back. I'll do this by adding a handler. In this case, adding with success handler and the name of the function that I want to run when I get a success event back from my Apps Script. (SOUND) The success event comes back to me with a value.
The return value from the getDriveUsed function I defined earlier. What I'm doing here is passing that value into the span tag, with the ID used value. With a percent sign appended. I can also add a failure handler, just in case something goes wrong. I'll make sure this file's saved, and before I switch over to my website, and I see any quotes around this percent since it's a string. Save that again, and I'll go over to the slash dev version and reload. So we can see that it shows up here. But to update this on my production site, the slash excerpt version, need to go back to my script editor.
Go to file, manage versions, and save a new version of the site. Then, I can go back to the arrow cloud icon. Or deploy his Web App in the publish menu, and change the published version there. Then it'll hit Update, and now my production site is showing the updated code. I can go back to publish and deploy as Web App.
And choose to execute the App as the user accessing the Web App. I'll change the access to anyone and hit Update. As you can see here, I've logged in as a different user. And I have a different amount of drive space used, now that I'm using my own credentials to log in instead of the script's authors.
- What is Google Apps Script?
- Creating and editing files with Apps Services
- Storing data for your script
- Using events and triggers in containers
- Building custom spreadsheet functions
- Sharing your script as a web app
- Serving JSON or XML from your script
- Publishing your app in the Chrome Web Store