LoginSignup
1
2

More than 5 years have passed since last update.

[GAS] GmailでCSSを反映させた下書きを生成する方法

Last updated at Posted at 2018-06-29

ゴール

以下を下書きメールとして生成すること。
スクリーンショット 2018-06-30 4.47.17.png

前提

GAS

htmlファイルをTemplateとして読み込み、createDraftで下書きを生成したい。
以下のようなコードを書いた時、CSSをどう書くかという話。

sample.js
function createGreetingEmail() {
  // index.htmlファイルをテンプレートとして読み込む
  var template = HtmlService.createTemplateFromFile("index");
  // テンプレートの変数に値を代入
  template.name = "tarou";
  // 文字列として生成
  var html = template.evaluate().getContent();

  GmailApp.createDraft(
    "done@example.com",
    "Greeting",
    "htmlが表示できません...",
    {htmlBody: html}
  );
}

HTML & CSS の書き方

こう書く

createDraftでスタイルを反映させるには、インライン形式で書くしかない。

index.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1 style=color:red;> hello <?= name ?> </h1>
  </body>
</html>

補足: <?= hogehoge ?>という書き方で、GASの変数を代入できる。

失敗例

<head><body>内に書く

index.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style type="text/css">
      h1 {color:red;}
    </style>
  </head>
  <body>
    <h1 >hello <?= name ?> </h1>
  </body>
</html>

ちなみに、sendEmailではちょっと違う

createDraftではなく、sendEmailの場合は、<head><body>内に書いてもOK

不便だなぁ、、なんでだろう??...

参考

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2