LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Saturday, December 21, 2019

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列にある複数のテキストをスライドに追加したい(Google Apps Script)

今回Google Apps Scriptでやりたいこと GoogleスプレッドシートA列にある複数の値を取得して Googleスライドに渡して 図形オブジェクトのテキストとして追加したい ① スプレッドシートのA列に値を入れておく ② Code.gsのinsertNewShape...