מדריך למתחילים בנושא Apps Script

במאמר הזה נסביר איך להשתמש ב-Apps Script כדי לשלוח לעצמכם אימייל עם סכימות, במטרה לבדוק את תגי העיצוב של האימייל.

יצירת הפרויקט

נכנסים לכתובת script.google.com. אם זו הפעם הראשונה שאתם נכנסים ל-script.google.com, תופנו לדף מידע. לוחצים על Start Scripting (התחלת כתיבת סקריפט) כדי להמשיך לעורך הסקריפטים. בכלי לעריכת סקריפטים, יוצרים סקריפט לפרויקט ריק.

מחליפים את הקוד בקובץ 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 כדי שיתאים לפרמטר ב-JavaScript שלמעלה. מחליפים את התוכן של קובץ ה-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 נבחרה בתפריט הנפתח Select function.
  4. לוחצים על Run בסביבת הפיתוח של Apps Script.

בפעם הראשונה שמריצים את הסקריפט, מוצגת בקשה להענקת הרשאה. לאחר מכן צריך להריץ אותו שוב. אחרי שהסקריפט פועל, בודקים אם הגיע לתיבת הדואר הנכנס אימייל שנשלח מעצמכם עם לחצן Go-To Action, כמו בצילום המסך הבא:

מדריך לשימוש ב-Apps Script

איך הסקריפט עובד?

הפונקציה testSchemas קוראת את תוכן ה-HTML מהקובץ שנקרא mail_template.html ושולחת את התוכן הזה באימייל למשתמש המאומת הנוכחי. כמו שמוסבר במאמר בנושא הרשמה ל-Google, כל הסכימות שאתם שולחים לעצמכם יוצגו ב-Gmail, כך שאפשר להשתמש באימייל שנשלח על ידי הסקריפט כדי להתעלם מדרישות ההרשמה למטרות בדיקה.