自动化快速入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
构建并运行一个简单的自动化流程,该流程会创建一个 Google 文档,并通过电子邮件向您发送该文档的链接。
目标
前提条件
如需使用此示例,您需要满足以下前提条件:
- Google 账号(Google Workspace 账号可能需要管理员批准)。
- 可访问互联网的网络浏览器。
设置脚本
如需构建自动化,请按以下步骤操作:
- 如需打开 Apps 脚本编辑器,请前往
script.google.com
。如果您是首次访问 script.google.com
,请点击查看信息中心。
- 点击 New project。
删除脚本编辑器中的所有代码,然后粘贴以下代码。
点击“保存”图标
。
点击无标题项目。
输入脚本的名称,然后点击重命名。
运行脚本
如需运行该脚本,请按以下步骤操作:
- 点击运行。
根据提示为脚本授权。
如果 OAuth 权限请求页面显示警告此应用未经过验证,请继续操作,依次选择高级 >
前往“{项目名称}”(不安全)。
脚本执行完毕后,请检查您的 Gmail 收件箱是否收到了电子邮件。
打开电子邮件,然后点击链接以打开您创建的文档。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis script automates the creation of a Google Doc and emails you a link for access.\u003c/p\u003e\n"],["\u003cp\u003eYou'll need a Google Account and web browser to use this automation script.\u003c/p\u003e\n"],["\u003cp\u003eThe script involves pasting provided code into the Apps Script editor and running it after authorization.\u003c/p\u003e\n"],["\u003cp\u003eUpon successful execution, you'll receive an email containing the link to your newly created Google Doc.\u003c/p\u003e\n"],["\u003cp\u003eExplore further options to extend functionalities with Google Docs, Sheets, and Slides or learn basic JavaScript.\u003c/p\u003e\n"]]],[],null,["# Automation quickstart\n\nBuild and run a simple automation that creates a Google Docs document and\nemails you a link to the document.\n\nObjectives\n----------\n\n- Set up the script.\n- Run the script.\n\nPrerequisites\n-------------\n\nTo use this sample, you need the following prerequisites:\n\n- A Google Account (Google Workspace accounts might require administrator approval).\n- A web browser with access to the internet.\n\nSet up the script\n-----------------\n\nTo build the automation, take the following steps:\n\n1. To open the Apps Script editor, go to [`script.google.com`](https://script.google.com). If this is the first time you've been to `script.google.com`, click **View Dashboard**.\n2. Click **New project**.\n3. Delete any code in the script editor and paste in the code below.\n\n templates/standalone/helloWorld.gs \n [View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/templates/standalone/helloWorld.gs) \n\n ```javascript\n /**\n * Creates a Google Doc and sends an email to the current user with a link to the doc.\n */\n function createAndSendDocument() {\n try {\n // Create a new Google Doc named 'Hello, world!'\n const doc = DocumentApp.create('Hello, world!');\n\n // Access the body of the document, then add a paragraph.\n doc.getBody().appendParagraph('This document was created by Google Apps Script.');\n\n // Get the URL of the document.\n const url = doc.getUrl();\n\n // Get the email address of the active user - that's you.\n const email = Session.getActiveUser().getEmail();\n\n // Get the name of the document to use as an email subject line.\n const subject = doc.getName();\n\n // Append a new string to the \"url\" variable to use as an email body.\n const body = 'Link to your doc: ' + url;\n\n // Send yourself an email with a link to the document.\n GmailApp.sendEmail(email, subject, body);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed with error %s', err.message);\n }\n }\n ```\n4. Click Save .\n\n5. Click **Untitled project**.\n\n6. Enter a name for your script and click **Rename**.\n\nRun the script\n--------------\n\nTo run the script, take the following steps:\n\n1. Click **Run**.\n2. When prompted, authorize the script.\n If the OAuth consent screen displays the warning, **This app isn't verified** ,\n continue by selecting **Advanced** \\\u003e\n **Go to {Project Name} (unsafe)**.\n\n3. When the script execution completes, check your [Gmail inbox](https://mail.google.com)\n for the email.\n\n4. Open the email and click the link to open the document that you created.\n\nNext steps\n----------\n\n- [Extend Google Docs](/apps-script/guides/docs)\n- [Extend Google Sheets](/apps-script/guides/sheets)\n- [Extend Google Slides](/apps-script/guides/slides)\n- [Basic JavaScript features](/apps-script/guides/services#basic_javascript_features)"]]