การเริ่มต้นใช้งานการทำงานอัตโนมัติอย่างรวดเร็ว

สร้างและเรียกใช้การทำงานอัตโนมัติที่จะสร้างเอกสาร Google เอกสารและ ส่งอีเมลลิงก์ไปยังเอกสารให้คุณ

วัตถุประสงค์

  • ตั้งค่าสคริปต์
  • เรียกใช้สคริปต์

ข้อกำหนดเบื้องต้น

หากต้องการใช้ตัวอย่างนี้ คุณต้องมีข้อกำหนดเบื้องต้นต่อไปนี้

  • บัญชี Google (บัญชี Google Workspace อาจต้อง ได้รับการอนุมัติจากผู้ดูแลระบบ)
  • เว็บเบราว์เซอร์ที่มีสิทธิ์เข้าถึงอินเทอร์เน็ต

ตั้งค่าสคริปต์

หากต้องการสร้างการทำงานอัตโนมัติ ให้ทำดังนี้

  1. หากต้องการเปิดโปรแกรมแก้ไข Google 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. คลิกบันทึก ไอคอนที่แสดงการดำเนินการบันทึก

  5. คลิกโปรเจ็กต์ที่ไม่มีชื่อ

  6. ป้อนชื่อสคริปต์ แล้วคลิกเปลี่ยนชื่อ

เรียกใช้สคริปต์

หากต้องการเรียกใช้สคริปต์ ให้ทำดังนี้

  1. คลิกเรียกใช้
  2. ให้สิทธิ์สคริปต์เมื่อได้รับข้อความแจ้ง <<../samples/_snippets/oauth.md>>
  3. เมื่อสคริปต์ทำงานเสร็จแล้ว ให้ตรวจสอบอีเมลในกล่องจดหมาย Gmail
  4. เปิดอีเมลแล้วคลิกลิงก์เพื่อเปิดเอกสารที่คุณสร้าง

ขั้นตอนถัดไป