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 という名前を付けます。これは、上記の 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. Select function プルダウン メニューで関数 testSchemas が選択されていることを確認します。
  4. Apps Script 開発環境で Run をクリックします。

初めてスクリプトを実行する際は、承認を求められます。その後、再度実行する必要があります。スクリプトの実行後、[Go-To Action] ボタンを使って自分から送信されたメールが受信トレイにあるか確認します。次のスクリーンショットをご覧ください。

Apps Script チュートリアル

スクリプトの動作

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