מדריך למתחילים ב-Google 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 כדי שיתאים לפרמטר בסקריפט הקודם. מחליפים את התוכן של קובץ ה-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.

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

מדריך ל-Apps Script

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

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