Bắt đầu nhanh cho quy trình tự động hoá

Tạo và chạy một quy trình tự động hoá để tạo tài liệu trên Google Tài liệu và gửi cho bạn một đường liên kết đến tài liệu đó qua email.

Mục tiêu

  • Thiết lập tập lệnh.
  • Chạy tập lệnh.

Điều kiện tiên quyết

Để sử dụng mẫu này, bạn cần đáp ứng các điều kiện tiên quyết sau:

  • Một Tài khoản Google (tài khoản Google Workspace có thể yêu cầu quản trị viên phê duyệt).
  • Một trình duyệt web có quyền truy cập vào Internet.

Thiết lập tập lệnh

Để tạo quy trình tự động hoá, hãy làm như sau:

  1. Để mở trình chỉnh sửa tập lệnh Google Apps Script, hãy truy cập vào script.google.com. Nếu đây là lần đầu tiên bạn truy cập vào script.google.com, hãy nhấp vào Xem trang tổng quan.
  2. Nhấp vào Dự án mới.
  3. Xoá mọi mã trong trình chỉnh sửa tập lệnh rồi dán mã sau vào.

    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. Nhấp vào Lưu Biểu tượng đại diện cho thao tác lưu.

  5. Nhấp vào Dự án chưa có tiêu đề.

  6. Nhập tên cho tập lệnh rồi nhấp vào Đổi tên.

Chạy tập lệnh

Để chạy tập lệnh, hãy làm như sau:

  1. Nhấp vào Chạy.
  2. Khi được nhắc, hãy cho phép tập lệnh chạy. <<../samples/_snippets/oauth.md>>
  3. Khi quá trình thực thi tập lệnh hoàn tất, hãy kiểm tra hộp thư đến trong Gmail để tìm email.
  4. Mở email rồi nhấp vào đường liên kết để mở tài liệu mà bạn đã tạo.

Các bước tiếp theo