GAS: Programando tu primer script

This is a tutorial that will walk you through the process of building your first simple script. After completing this tutorial, you should be comfortable with the basics of how to create and execute a script from the Script Editor.

  1. Requirements
  2. What is a Script?
  3. Writing a Script
  4. Executing a Script
  5. Learn More

Requirements

Before you begin, you need a Google Account or a Google Apps Account (see this FAQ to understand the difference), a supported browser, and a basic understanding of JavaScript. If you’re new to JavaScript, MDN’s JavaScript wiki has a lot of information, including a Reference and a Guide. Note that these materials were neither developed by nor associated with Google.

What is a Script?

A script is a series of instructions you write in a programming language or scripting language to accomplish a particular task. You type in the instructions and save them as a script. The script runs only under circumstances you define.

Google Apps Script is based upon JavaScript. In addition to providing much of what’s built into JavaScript, Google Apps Script also provides a set of classes that make up the Google Apps Script API. You can use these classes and their associated methods to access Google products, make requests to third-party services and APIs, and access helpful utilities. You can learn more about these topics in the sections on Using Built-in Services and Using External APIs.

Writing a Script

In this tutorial, we will create a standalone script, which is a script that is accessible from Google Drive.

To create a standalone script, go to https://script.google.com. If you see a window that asks what type of script you’d like to create, either select Blank Project or click Close.

Your newly created script will look something like this:

A new script

The script you will create is a very simple one. It will create a new Google Document and email you a link to the newly created document. Delete the placeholder code in your new script, and then copy and paste the code below into the Script Editor.

function createAndSendDocument() {
  // Create a new document with the title 'Hello World'
  var doc = DocumentApp.create('Hello World');

  // Add a paragraph to the document
  doc.appendParagraph('This document was created by my first Google Apps Script.');

  // Save and close the document
  doc.saveAndClose();

  // Get the URL of the document
  var url = doc.getUrl();

  // Get the email address of the active user - that's you
  var emailAddress = Session.getActiveUser().getEmail();

  // Send yourself an email with a link to the document
  GmailApp.sendEmail(emailAddress,
                     'Hello from my first Google Apps Script!',
                     'Here is a link to a document created by my ' +
                     'first Google Apps Script: ' + url);
}

After you paste the code into the Script Editor, click the Save icon. You’ll be prompted to rename your project. Enter the name My First Script and then click OK. Now that you’ve created the script, move on to the next section to learn how to execute the script.

Executing a Script

For the purposes of this tutorial, we’ll run the script directly from the Script Editor. To learn about the other ways that scripts can be executed, see the section on Execution Methods for Scripts.

To execute the script, either click the Run icon or choose Run > createAndSendDocument from the menu. You will see an authorization dialog appear.

authorization dialog

This dialog tells you which services the script needs to access. Click Authorize. Next, you’ll see another dialog, prompting you to grant access to your Gmail account. This is needed so that the script can send email from your Gmail account. Click Grant access.

OAuth dialogNow you’ll see an Authorization Status page, which tells you that you can now run the script. Click Close. Note that you will only have to go through this process the first time you run a script or when a script has been modified to require more permissions than you’d previously granted.

Authorization status

Now that the script has been authorized, run it once more. This time, the script will execute. Next go to your Gmail inbox and you should see an email from your script that looks similar to this.

Email from the script

If you click the link in the email, it will open a Google Document that looks like the one below.

Document

Learn More

Now that you understand the basics of using the Script Editor and creating and running a script, it’s time to learn more. The Tutorials are a good place to find step-by-step examples of different types of scripts. For the API reference for the default services in Google Apps Script, see the Default Services section of the documentation.

If you find that you need additional help, see the support page for information about how to ask questions or raise issues with the Google Apps Script team.