Apps Scriptリファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2019年12月21日土曜日

gsファイルからHTMLタグを読み込んでindex.htmlに表示してみる


Google Apps Scriptの.gs側にHTMLを返す関数を用意して
index.html側で読み込んで表示する

ということがやりたくて書いたコードです。


今回やること

コード.gsにテキストエリアを返すだけの returnHtml() という関数を用意して
index.htmlの getHtml() でそれを実行して
返ってきたHTMLを innerHTML で表示する



コード.gs
function doGet(e) {
  return HtmlService.createHtmlOutputFromFile("index");
}

function returnHtml(){
  return "<textarea>hello</textarea>"; 
}




index.html
<!DOCTYPE html>
<html>
  <body>
    <div id="mainDiv"></div>

<script>

getHtml();
function getHtml() {
  google.script.run
  .withFailureHandler(onFailure)
  .withSuccessHandler(gotHtml)
  .returnHtml();
}

function gotHtml(result) {
  document.getElementById('mainDiv').innerHTML = result;
}

function onFailure(e) {
  alert([e.message, e.stack]);
}

</script>
</body>
</html>



Latest post

Google Formsでクイズを作りたい

Googleフォームには回答を判定するクイズモードがあります 今回はそのクイズモードで回答の判定とフィードバックについて書いていきます 「クイズモード」の表記: 日本語の表記は「テストにする」ですが 英語の表記は「Make this a quiz」となっています この記事ではでは...