เริ่มต้นใช้งาน Google Apps Script

เอกสารนี้จะแสดงวิธีใช้ Apps Script เพื่อ ส่งอีเมลที่มีสคีมาให้ตัวเองเพื่อทดสอบมาร์กอัปอีเมล

สร้างโปรเจ็กต์

ไปที่ script.google.com หากคุณเข้าชมเป็นครั้งแรก ที่ script.google.com ระบบจะเปลี่ยนเส้นทางคุณไปยังหน้าข้อมูล คลิกเริ่มเขียนสคริปต์ เพื่อไปยังเครื่องมือแก้ไขสคริปต์ ในเครื่องมือแก้ไขสคริปต์ ให้สร้างสคริปต์สำหรับโปรเจ็กต์เปล่า

แทนที่โค้ดใน Code.gs ด้วยโค้ดต่อไปนี้

gmail/markup/Code.gs
/**
 * Send an email with schemas in order to test email markup.
 */
function testSchemas() {
  try {
    const htmlBody =
      HtmlService.createHtmlOutputFromFile("mail_template").getContent();

    MailApp.sendEmail({
      to: Session.getActiveUser().getEmail(),
      subject: `Test Email markup - ${new Date()}`,
      htmlBody: htmlBody,
    });
  } catch (err) {
    console.log(err.message);
  }
}

เลือกไฟล์ > ใหม่ > ไฟล์ HTML เพื่อสร้างไฟล์ HTML ตั้งชื่อไฟล์ว่า mail_template ให้ตรงกับพารามิเตอร์ในสคริปต์ก่อนหน้า แทนที่เนื้อหาของไฟล์ HTML ด้วยเนื้อหาต่อไปนี้

gmail/markup/mail_template.html
<!--
 Copyright 2022 Google LLC

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<html>
  <head>
    <script type="application/ld+json">
    {
      "@context": "https://schema.org",
      "@type": "EmailMessage",
      "description": "Check this out",
      "potentialAction": {
        "@type": "ViewAction",
        "target": "https://www.youtube.com/watch?v=eH8KwfdkSqU"
      }
    }
    </script>
  </head>
  <body>
    <p>
      This a test for a Go-To action in Gmail.
    </p>
  </body>
</html>

ทดสอบสคริปต์

วิธีทดสอบสคริปต์

  1. บันทึกโปรเจ็กต์
  2. เลือกแท็บสำหรับ Code.gs
  3. ตรวจสอบว่าได้เลือกฟังก์ชัน testSchemas ในเมนูเลือกฟังก์ชัน แล้ว
  4. คลิกเรียกใช้ ในสภาพแวดล้อมการพัฒนา Apps Script

เมื่อเรียกใช้สคริปต์เป็นครั้งแรก ระบบจะขอให้คุณให้สิทธิ์ หลังจากให้สิทธิ์แล้ว ให้เรียกใช้สคริปต์อีกครั้ง หลังจากสคริปต์ทำงานแล้ว ให้ตรวจสอบกล่องจดหมายว่ามี อีเมลที่ส่งจากตัวคุณเองพร้อม ปุ่มการดำเนินการ "ไปที่" ดังที่แสดง ในภาพหน้าจอด้านล่างหรือไม่

บทแนะนำ Apps Script

สคริปต์ทำงานอย่างไร

ฟังก์ชัน testSchemas จะอ่านเนื้อหา HTML จากไฟล์ mail_template.html และส่งเนื้อหานั้นเป็นอีเมลไปยังผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์ ดังที่อธิบายไว้ ใน ลงทะเบียนกับ Google, Gmail จะแสดงสคีมาทั้งหมดที่คุณส่งให้ตัวเอง คุณสามารถใช้อีเมลที่สคริปต์ส่งมาเพื่อข้ามข้อกำหนดการลงทะเบียนสำหรับการทดสอบได้