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 ファイルを作成します。上記の 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 をクリックします。

スクリプトを初めて実行するときに、承認を求められます。承認したら、スクリプトをもう一度実行してください。スクリプトが実行されたら、受信トレイに、次のスクリーンショットのように、[Go-To Action] ボタンが付いた自分宛てのメールが届いていることを確認します。

Apps Script チュートリアル

スクリプトの動作

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