Introducción a Google Apps Script – Lección 1

Welcome to our project Google Developers Hackademy, in which experts from our Latin American communities will teach you how to use our tools, developer products, APIs and new tricks. I’m glad to inaugurate this Google Developers Hackademy with a course created and presented by Antonio Guzman, organizer of the GDG Androidtitlan in Mexico. This course consists of five parts. We invite you to watch it and learn with us. Let’s begin. Hello, my name is Antonio Guzman. Welcome to this introduction course to Google Apps Script.

In this course, our goal is to get you closer to the Google Apps Script concept, knowing the necessary elements to start working, knowing the services available, and giving examples with some programming. We’ll work with Spreadsheet, Document and we’ll integrate the Gmail e-mail sending service. In this first lesson, we’ll talk about 3 things: what is Google Apps Script, what is needed to build our own scripts, and which are the services available in the platform. Throughout the lesson we’ll do an exercise, and when we finish, we’ll understand the concept of Google Apps Script and we’ll be able to execute our own scripts. In business and personal environments, Google Apps allow us to store, generate and transform information, but we don’t always persist it.

This is where Google Apps Script gives us the chance to maximize the value of our information because we can exploit it in a simple way. Some of the services we use every day have APIs, which, using the JavaScript syntax, allow us to write programs to do all kinds of things that range from something as simple as extracting the number of unread e-mails, to more elaborated ones, like translating the information of a spreadsheet, sending it to another document, and then attaching it to an e-mail we’re sending through Gmail. There are several services available in Google Apps Script and they’re divided into two categories: the first category is called Google Apps Services, which includes Calendar, Contacts, Dodlist, Document, Domain, Drive, Finance, Forms, Gmail, Groups, Language, Maps, Sites, Spreadsheet…

And well, the other group is the advanced services one. In this group we have: Adsense, Analytics, Bigquery, Prediction, Task and Urlshortener. 頻hich ones have you used? In this case, though we’re writing JavaScript programs, they’re neither run on the client’s side nor executed in the browser. These programs run on the server’s side, but we don’t need to configure a specialized server either, they run on Google’s infrastructure, they run on Google’s servers. So, to start the development of Google Apps Script we need to visit the developers’ site. There, we’ll find a section devoted to App Script and we need to see the site as an initial info source, since there we’ll find the definition for App Script, the basic and intermediate tutorials, the documentation for the services available in the platform, and it also works as an access route to the Code Editor, where we will start writing our programs.

And now it’s time to start programming. LET’S GET TO WORK Click on the center red button, “Start scripting,” to redirect you to scriptgoogle.com. We see a welcome window. The welcome window has different elements which are templates to create scripts, tutorials and the scripts we’ve recently created. We’re going to choose an empty option, Blank Project. When on the Editor screen, the first thing to do is name the project. We will name this one “gas01.” We click on OK and we continue building our script and getting familiarized with our environment. Notice that we have several menu options that facilitate the programming. We can see the Log, access the Help, publish the function list we have, and, well, we now have a function skeleton. Let’s declare our variable, to which a document we’ll create will be associated.

In order to create the document, we will use the “DocumentApp” class. The DocumentApp class is part of the APIs that, as we mentioned before, allow us to use the services available in the platform. DocumentApp has a method called “create”. This method receives a text chain that is the name for the document we want to create. This document is created into our Google Drive unit. We’ll give it a name similar to the one of our script: “gas -01.” Now, what we’re going to do is putting text in our document. For this, we get its body and it’s there where we will paste the text. We use a method called “appendParagraph,” which needs us to give it a text chain, which in this case will be: “Welcome to the world of scripts,” and this way we already have a paragraph in our document; it exists and has a content.

It’d also be good to add a second paragraph to it to keep adding content. Then, how do we do that? Just as we did it before. Document, we get this object’s body and once again, the method is the same: appendParagraph, but now we will change the text, all right? Would you like it if it now says: “Welcome to the world of Google Apps Script?” Remember, the method gets a text chain which is “Welcome to the world of Google Apps Script.” Semicolon, we finish our sentence…

Now we save it and we run our script for the first time. In the upper bar we see it is running and it asks something. Every script we run needs the user’s authorization. So we give it to it. It lets us know which are the permissions this script has. It says it’ll access our unit, so we click on “OK” and we already know what our script needs: accessing our Drive. We create a document in Drive, so we enter: drive.google.com. We search our document. Do you remember its name? “gas -01,” and then it opens it. And there’s the text we had written: “Welcome to the world of scripts.” “Welcome to the world of Google Apps Scripts,” and our script worked, it did what we asked it to do.

The scenarios, problems and opportunities we have are very diverse. That’s why, when we build a script, we have to consider all the services available to reach the best possible solution. Each one of these services, as we saw it, has its own APIs. We’ll have to use them to reach that solution. So we can state that Google Apps Script is not a way or a recipe, but the possibility to choose several items to reach a solution. RECAP Before starting our next lesson, I recommend you keep getting familiarized with the resources available in the developers’ site. Let’s remember we have the APIs there for the different services available. Check them out, why not choose those that correspond to the services you use more. Perhaps Calendar, Gmail, Spreadsheet, Drive… When you start seeing the API’s methods and you’ll certainly come up with things you can do for the next scripts you are trying.

You’ll be able to …. scenarios, solutions, and well, now it’s time to getting familiarized with the editor. We saw it has several menus and each menu, several options. Start clicking here and there. You also have to remember what we did in the beginning: clicking on “Start scripting” in the developers’ site. Next time, when you want to build a script, you don’t need to do it from there, you’ll be able to do it from Google Drive, where an option to create script-like documents has been added, and that’s it, you’ll have started building scripts from Google Drive. This is it for our first lesson.

In the next lesson we’ll see the different types of scripts and we will try two execution modes: the manual and the time-triggered executions. See you next time. Keep on programming. .

Add Comment