自动化快速入门

构建并运行简单的自动化操作,以创建一个 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 同意屏幕显示 This app is not verify 警告,请依次选择 Advanced > Go to {Project Name} (unsafe),以继续操作。

  3. 脚本执行完成后,请在 Gmail 收件箱中查收电子邮件。

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

后续步骤