自動化快速入門導覽課程

建立及執行簡單的自動化程序,建立 Google 文件並透過電子郵件傳送文件連結。

目標

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

必要條件

如要使用這個範例,您必須符合下列先決條件:

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

設定指令碼

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

  1. 如要開啟 Apps Script 編輯器,請前往 script.google.com。如果這是你第一次前往 script.google.com,請按一下「查看資訊主頁」
  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. 按一下「Save」(儲存) 圖示 「儲存」圖示

  5. 按一下「Untitled project」(未命名的專案)

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

執行指令碼

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

  1. 按一下「執行」
  2. 出現提示訊息時,請授權執行指令碼。如果 OAuth 同意畫面顯示「這個應用程式未經驗證」警告,請依序選取「進階」>「前往『{專案名稱}』(不安全)」,繼續操作。

  3. 指令碼執行完畢後,請檢查 Gmail 收件匣是否收到電子郵件。

  4. 開啟電子郵件,然後點選連結,開啟您建立的文件。

後續步驟