自动化快速入门

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

构建并运行简单的自动化作业,以创建 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. 打开电子邮件,然后点击链接以打开您创建的文档。

后续步骤