自动化快速入门

构建并运行简单的自动化操作,用于创建 Google 文档 通过电子邮件向您发送指向该文档的链接。

目标

  • 设置脚本。
  • 运行脚本。

前提条件

如需使用此示例,您需要满足以下前提条件:

  • Google 账号(Google Workspace 账号可能 需要管理员批准)。
  • 可以访问互联网的网络浏览器。

设置脚本

如需构建自动化操作,请按以下步骤操作:

  1. 要打开 Apps 脚本编辑器,请转到 script.google.com。如果这是您首次去过 script.google.com,点击查看信息中心
  2. 点击 New project
  3. 删除脚本编辑器中的所有代码,并粘贴以下代码。

    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);
      }
    }
  4. 点击“保存”“保存”图标

  5. 点击未命名项目

  6. 输入脚本的名称,然后点击重命名

运行脚本

如需运行该脚本,请按以下步骤操作:

  1. 点击运行
  2. 出现提示时,为脚本授权。 如果 OAuth 同意屏幕显示以下警告:“此应用未经验证”, 选择高级 > 以继续 前往“{Project Name}”(不安全)

  3. 脚本执行完成后,请查看您的 Gmail 收件箱

  4. 打开该电子邮件,然后点击链接打开您创建的文档。

后续步骤