Tag Archives: JavaScript

Run Apps Script code from anywhere using the Execution API

Posted by Edward Jones, Software Engineer, Google Apps Script and Wesley Chun, Developer Advocate, Google Apps

Have you ever wanted a server API that modifies cells in a Google Sheet, to execute a Google Apps Script app from outside of Google Apps, or a way to use Apps Script as an API platform? Today, we’re excited to announce you can do all that and more with the Google Apps Script Execution API.

The Execution API allows developers to execute scripts from any client (browser, server, mobile, or any device). You provide the authorization, and the Execution API will run your script. If you’re new to Apps Script, it’s simply JavaScript code hosted in the cloud that can access authorized Google Apps data using the same technology that powers add-ons. The Execution API extends the ability to execute Apps Script code and unlocks the power of Docs, Sheets, Forms, and other supported services for developers.

One of our launch partners, Pear Deck, used the new API to create an interactive presentation tool that connects students to teachers by converting slide decks into interactive experiences. Their app calls the Execution API to automatically generate a Google Doc customized for each student, so everyone gets a personalized set of notes from the presentation. Without the use of Apps Script, their app would be limited to using PDFs and other static file types. Check out the video below to see how it works.



Bruce McPherson, a Google Developer Expert (GDE) for Google Apps, says: “The Execution API is a great tool for enabling what I call ‘incremental transition’ from Microsoft Office (and VBA) to Apps (and Apps Script). A mature Office workflow may involve a number of processes currently orchestrated by VBA, with data in various formats and locations. It can be a challenge to move an entire workload in one step, especially an automated process with many moving parts. This new capability enables the migration of data and process in manageable chunks.” You can find some of Bruce’s sample migration code using the Execution API here.

The Google Apps Script Execution API is live and ready for you to use today. To get started, check out the developer documentation and quickstarts. We invite you to show us what you build with the Execution API!

Streamlining code in the JavaScript Maps API

Since the Google Maps JS API v3 launched in 2009, we have added lots of new features. And we’ve also made occasional modifications to the API to minimize and simplify common code pathways for developers.


Today we want to announce two small, but useful, changes we have recently made. The first lessens the amount of code required to create a map. The second evolves the API towards a more standard pattern of using plain JavaScript objects.


The first change was introduced quietly a few versions ago, and made the Map Type an optional parameter (defaulting to the standard ROADMAP type) when instantiating a new map.


The second change is available in versions 3.16 of the API and above, and introduces a new way of creating a location: instead of google.maps.LatLng(37.5, -122.5), you can now simply pass in a plain JavaScript object like {lat: 37.5, lng: -122.5}.


Let’s take a look at how these changes affect the original code from our launch announcement above:


Original:
var myOptions = {
     zoom: 8,
     center: new google.maps.LatLng(-34.397, 150.644),
     mapTypeId: google.maps.MapTypeId.ROADMAP
   };


New
   var myOptions = {
     zoom: 8,
     center: {lat: -34.397, lng: 150.644}
   };


Note that since the order of the values no longer matters, the following is equally valid:
     center: {lng: 150.644, lat: -34.397}
The end result is less code needed to instantiate a basic map, less opportunity for confusion regarding coordinate ordering, and a more flexible way to create locations.


Note that google.maps.LatLng will continue to work, and there’s no need to modify any of your existing code. These new features are simply designed to help those getting started, and help you create more streamlined and readable code.


As always, we love to get feedback on how even minor changes like this affect your workflow. You can comment on this post or on our Google+ Page. And as always, you can get help on StackOverflow. Check out our support page for the right tags to use.

Posted by Josh Livni, Maps API Team

Building on YouTube APIs in the cloud with Google Apps Script

There’s nothing better in this world than a great pairing, like coffee and donuts or bees and honey. Today there’s a new one to add to the list: Google Apps Script now has built in support for the YouTube Data v3 and YouTube Analytics APIs. If you haven’t yet heard of Google Apps Script, you’re missing out on an easy-to-use tool for integrating and automating common tasks across many of Google’s services. With less than half a screen’s worth of code in Google Apps Script’s cloud-based editor, you can:

  • Dynamically update a spreadsheet containing watch-time statistics for all of your channel's videos, with all the flexibility and power of Google Sheets to sort and slice that data
  • Create a live dashboard or scheduled email report about your channel's performance
  • Handle channel management tasks such as scheduling automatic bulletins or changing the visibility of a large number of videos from private to public
  • Automate playlist rotation without having to maintain a server or keep a computer for the sole purpose of running a script
Google Apps Script's cloud-based environment and autocomplete functionality make it easy to just open an editor, enable the YouTube APIs, and start writing code:



For functions that require OAuth 2.0 authorization, there’s no authorization code to write and no token management to deal with. Once your script is ready, just click “Run” and Google Apps Script will present you with an authorization dialog. Once you select the channel you want to authorize, the script will have all of the permissions it needs to operate on your behalf, running in the background at scheduled intervals if you so desire.

To get started, browse to Google Drive. Click “Create” and then choose “Script”. This will open a new browser tab to the Google Apps Script editor. Name your project and click on “Resources” and select “Advanced Google Services”:


Toggle the YouTube Data API and/or YouTube Analytics API on:


Note the message that these services must be enabled in the API console. Click the link to be taken to the Google Developer Console. The link in the message will take you to a specific API project created for this specific Apps Script. Scroll down and toggle on the YouTube APIs the script will use:


Switch back to the tab containing Google Apps Script and click “OK”. You’re ready to start writing code. Type “YouTube” and hit the period key (“.”). If the APIs have been turned on correctly, you will be able to start writing code and calling functions available in the Data API or Analytics API. For instance, a short script that searches for videos about “dogs” and prints the video IDs to the Google Apps Script log would look like this:


function searchByKeyword() {
var results = YouTube.Search.list("id,snippet",
{q : "google apps script", maxResults: 25});

for(var i in results.items) {
var item = results.items[i];
Logger.log("[%s] Title: %s", item.id.videoId, item.snippet.title);
}
}

For more information about this update to Google Apps Script, check out the post on their official blog. To learn more about how to integrate scheduled jobs, write to spreadsheets or any of the many things Google Apps Script can do, check out the tutorials at their home at developers.google.com/apps-script. If you’re the type that prefers to learn on the go, get started with your own copy of our sample code in your Google Drive. You will still need to enable the APIs, so don’t forget to go to “Resources > Advanced Google services” for the link to the Developer Console project to turn on the APIs.


If you have any questions, feel free to find us on StackOverflow under the youtube-api and google-apps-script tags. Happy coding!


- Ikai Lan
YouTube Developer Relations

Map of the Week: Hill Mapper San Francisco

Map of the Week: Hill Mapper San Francisco


Hill Mapper San Francisco uses the Elevation API to show how steep streets are in San Francisco.


Streets that go uphill, relative to the current location of the marker, are red, and downhill streets are blue. The opacity of the color represents how steep the hill is.


If you drag around the marker, the Polylines change color, as the marker’s elevation changes the relative elevation of the streets.


It’s particularly powerful if you view it with satellite imagery.


If you want to search for a particular location, the search box uses Places Autocomplete to help you find it.


All around, this is a nice, innovative use of our APIs to show off useful information.

Posted by Mano Marks, Maps Developer Relations Team