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 ファイルを作成します。上記の JavaScript のパラメータと一致するように、ファイルに 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. [Select function] プルダウン メニューで関数 testSchemas が選択されていることを確認します。
  4. Apps Script 開発環境で [Run] をクリックします。

スクリプトを初めて実行するときは、承認を求めるメッセージが表示されます。承認したら、もう一度実行してください。スクリプトが実行されたら、受信トレイに移動して、自分から送信されたメールに移動ボタンがあることを確認します。次のスクリーンショットをご覧ください。

Apps Script チュートリアル

スクリプトの動作

testSchemas 関数は、mail_template.html という名前のファイルから HTML コンテンツを読み取り、そのコンテンツを現在認証されているユーザーにメールとして送信します。Google への登録で説明したように、自分宛てに送信したすべてのスキーマが Gmail に表示されるため、スクリプトから送信されたメールを使用して、テスト目的で登録要件を無視できます。