1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【小ネタ】GASでHTMLをレンダリングするテンプレート

Last updated at Posted at 2020-04-17

概要

小物制作でGASでWEBページを作ることがあるのですが、都度他プロジェクトからコピペしていたので、テンプレートとして残しておきます。

コード

g_main.js
function doGet(e) {  
  //スマホ表示に対応させる呼び出し方 GASではHTML内のviewportが反応しない
  var htmlOutput = HtmlService.createTemplateFromFile("h_index.html").evaluate();
  htmlOutput
    .setTitle('test')
    .addMetaTag('viewport', 'width=device-width, initial-scale=1')
  return htmlOutput;
}
h_index.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <?!= HtmlService.createHtmlOutputFromFile('h_style.html').getContent(); ?>
    <?!= HtmlService.createHtmlOutputFromFile('h_script.html').getContent(); ?>
  </head>
  <body>
    <div class="container">
      <p class="target">これはテストメッセージです。</p>
      <input type="button" value="ボタン" onclick="OnButtonClick();"/>
    </div>
  </body>
</html>
h_style.html
<style>
.container{
  line-height:200px;
  background-color:#eeeeff;
  text-align:center;
  border:#0000ff solid 3px;
}
.container .target{
  display:inline-block;
  vertical-align: middle;
  line-height:normal;
}
</style>
h_script.html
<script language="javascript" type="text/javascript">
  function OnButtonClick() {
    alert('アラートです。');
  }
</script>

備考

  • 公開->WEBアプリケーションとして導入 でWEBサイトのURLを取得できます。
  • GASエディタはファイル名でソートされるようなので、g_ や h_ から始めるようにしています。
1
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?