- Home
- Google Workspace
- Apps Script
- Samples
Build and run a simple automation that creates a Google Docs document andemails you a link to the document.
Objectives
- Set up the script.
- Run the script.
Prerequisites
To use this sample, you need the following prerequisites:
- A Google Account (Google Workspace accounts mightrequire administrator approval).
- A web browser with access to the internet.
Set up the script
To build the automation, take the following steps:
- To open the Apps Script editor, go to script.google.com. If this is the first time you've been to
script.google.com
, click View Dashboard. - Click New project.
Delete any code in the script editor and paste in the code below.
templates/standalone/helloWorld.gs
/** * Creates a Google Doc and sends an email to the current user with a link to the doc. */function createAndSendDocument() { try { // Create a new Google Doc named 'Hello, world!' const doc = DocumentApp.create('Hello, world!'); // Access the body of the document, then add a paragraph. doc.getBody().appendParagraph('This document was created by Google Apps Script.'); // Get the URL of the document. const url = doc.getUrl(); // Get the email address of the active user - that's you. const email = Session.getActiveUser().getEmail(); // Get the name of the document to use as an email subject line. const subject = doc.getName(); // Append a new string to the "url" variable to use as an email body. const body = 'Link to your doc: ' + url; // Send yourself an email with a link to the document. GmailApp.sendEmail(email, subject, body); } catch (err) { // TODO (developer) - Handle exception console.log('Failed with error %s', err.message); }}
Click Save .
Click Untitled project.
Enter a name for your script and click Rename.
Run the script
To run the script, take the following steps:
- Click Run.
When prompted, authorize the script.If the OAuth consent screen displays the warning, This app isn't verified,continue by selecting Advanced >Go to {Project Name} (unsafe).
When the script execution completes, check your Gmail inboxfor the email.
Open the email and click the link to open the document that you created.
Next steps
- Extend Google Docs
- Extend Google Sheets
- Extend Google Slides
- Basic JavaScript features
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-06 UTC.
[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]