自動化快速入門導覽課程

建構並執行簡單的自動化作業,建立 Google 文件文件,並以電子郵件將文件連結傳送給您。

目標

  • 設定指令碼。
  • 執行指令碼。

必要條件

如要使用這個範例,您必須具備以下必要條件:

  • Google 帳戶 (Google Workspace 帳戶可能需要取得管理員核准)。
  • 可連上網際網路的網路瀏覽器。

設定指令碼

如要建構自動化作業,請按照下列步驟操作:

  1. 如要開啟 Apps Script 編輯器,請前往 script.google.com。如果這是您第一次進入 script.google.com,請按一下「View Dashboard」(查看資訊主頁)
  2. 按一下 [新專案]
  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. 按一下「Untitled project」

  6. 輸入指令碼名稱,然後按一下「重新命名」

執行指令碼

如要執行指令碼,請按照下列步驟操作:

  1. 按一下「執行」
  2. 出現提示訊息時,請授權執行指令碼。如果 OAuth 同意畫面顯示警告「This app has not verification」(這個應用程式尚未驗證),請依序選取「Advanced」(進階) >「Go to {Project Name} (unsafe)」 (前往 {Project Name} (不安全))

  3. 指令碼執行完成後,請查看 Gmail 收件匣中的電子郵件。

  4. 開啟電子郵件並點選連結,即可開啟您建立的文件。

後續步驟