Changing HtmlOutput in Google Apps Script Web App

huangapple go评论115阅读模式
英文:

Changing HtmlOutput in Google Apps Script Web App

问题

在我的Google Apps Script中,我有两个HTML文件,让我们称它们为A.html和B.html。我的doGet()函数如下:

function doGet() {    
  return HtmlService.createHtmlOutputFromFile('A').setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

它可以工作,但我想在单击按钮时重定向到B页面,或者至少用B页面的内容替换HtmlOutput的内容。通过JavaScript操作更改页面内容是唯一的方法吗,还是可以使用Google提供的一些更智能的方法?

英文:

In my Google Apps Script I have two html files, let's call them A.html and B.html. My doGet() function is this simple one:

function doGet() {    
  return HtmlService.createHtmlOutputFromFile('A').setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

It works, but I would like to redirect to the B page when clicking a button, or at least replacing the content of the HtmlOutput with the content of the B page. Is changing the content of the page through javascript manipulation the only way or is there some smarter way using the functions provided by Google?

答案1

得分: 1

以下是代码的翻译部分:

Code.gs

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

function showTest() {
  let html = HtmlService.createTemplateFromFile("HTML_Body");
  SpreadsheetApp.getUi().showModalDialog(html.evaluate(),"Test");
}

function getHtml(id) {
  let fileName = id === "buttonA" ? "HTML_B" : "HTML_A";
  let html = HtmlService.createHtmlOutputFromFile(fileName);
  return html.getContent();
}

HTML_Body.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <div id="htmlBody">
      <?!= include("HTML_A"); ?>
    </div>
    <?!= include("JS_ButtonOnClick"); ?>
  </body>
</html>

HTML_A.html

<p>This is A</p>
<input id="buttonA" type="button" value="Click Me" onclick="buttonOnClick(this)">

HTML_B.html

<p>This is B</p>
<input id="buttonB" type="button" value="Click Me" onclick="buttonOnClick(this)">

JS_ButtonOnClick

<script>
  function buttonOnClick(button) {
    try {
      google.script.run.withSuccessHandler( 
        function(html) {
          document.getElementById("htmlBody").innerHTML = html;
        }
      ).getHtml(button.id);
    }
    catch(err) {
      alert(err);
    }
  }
</script>

参考链接

英文:

Here is an example of using GAS to replace the contents of the HTML body. Although I am using a spreadsheet as my basis, I believe it should work with a Web App. I add a &lt;div&gt; to the body so as not to disturb the &lt;script&gt; section.

Code.gs

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

function showTest() {
  let html = HtmlService.createTemplateFromFile(&quot;HTML_Body&quot;);
  SpreadsheetApp.getUi().showModalDialog(html.evaluate(),&quot;Test&quot;);
}

function getHtml(id) {
  let fileName = id === &quot;buttonA&quot; ? &quot;HTML_B&quot; : &quot;HTML_A&quot;;
  let html = HtmlService.createHtmlOutputFromFile(fileName);
  return html.getContent();
}

HTML_Body.html

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;base target=&quot;_top&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;htmlBody&quot;&gt;
      &lt;?!= include(&quot;HTML_A&quot;); ?&gt;
    &lt;/div&gt;
    &lt;?!= include(&quot;JS_ButtonOnClick&quot;); ?&gt;
  &lt;/body&gt;
&lt;/html&gt;

HTML_A.html

&lt;p&gt;This is A&lt;/p&gt;
&lt;input id=&quot;buttonA&quot; type=&quot;button&quot; value=&quot;Click Me&quot; onclick=&quot;buttonOnClick(this)&quot;&gt;

HTML_B.html

&lt;p&gt;This is B&lt;/p&gt;
&lt;input id=&quot;buttonB&quot; type=&quot;button&quot; value=&quot;Click Me&quot; onclick=&quot;buttonOnClick(this)&quot;&gt;

JS_ButtonOnClick

&lt;script&gt;
  function buttonOnClick(button) {
    try {
      google.script.run.withSuccessHandler( 
        function(html) {
          document.getElementById(&quot;htmlBody&quot;).innerHTML = html;
        }
      ).getHtml(button.id);
    }
    catch(err) {
      alert(err);
    }
  }
&lt;/script&gt;

References

huangapple
  • 本文由 发表于 2023年7月10日 18:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定