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

スプレッドシートの空白セルを直前の値で埋めたい

A列の空白セルに直前の値を入れたくて書いたコードです スプレッドシートに以下のようなBeforeの表があるとき (A列に空白セルがある) Before 1 A B 2 エリア 都市 3 東京 新宿 4 渋谷 5 池袋 6 神奈川 横浜 7 川崎 8 相模原 9 千葉 千葉 10 ...