簡體   English   中英

Google Apps腳本使用功能寫入HTML

[英]Google Apps Script write to HTML with Function

我正在嘗試編寫一個Google Apps腳本,當它作為Web應用發布時,將具有一個按鈕,當按下該按鈕時,它將調用Code.gs中的一個函數,該函數將顯示或隱藏一段html(實際上,更改html的樣式具有特定ID的div)。

這是我設置的一個小案例:

我的index.html文件:

<!DOCTYPE html>
<html>
<head>
  <base target="_top">
</head>

<body>

  <button onclick="google.script.run.myFunction()">Click Me</button>

  <div id="myDIV">
    This is my DIV I want to Reveal/Hide.
  </div>

 </body>
</html>

我的Code.gs文件:

function doGet() {
  var template = HtmlService.createTemplateFromFile("index.html");
  var html = template.evaluate();
  return HtmlService.createHtmlOutput(html);
  }

function myFunction() {  
  var x = document.getElementById("myDIV");/*Where the Reference Error Shows Up*/
  if (x.style.display === "none") {
    x.style.display = "block";
    } 
    else {
      x.style.display = "none";
      }
   }

我在調試時不斷遇到的錯誤是:ReferenceError:未定義“文檔”。 發布后,該按鈕將顯示myDIV,並且單擊該按鈕將不執行任何操作。

我假設問題是我不應該使用document.getElementByID(),而應該使用somethingelse.getElementByID(),但是我不知道那是什么。 我在網上尋找的大多數情況並非特定於Google Web Apps。 任何幫助,將不勝感激。

您需要注意每個代碼的執行位置:GS代碼是在“服務器端”執行的,因此不能引用作為導航器(“客戶端”)變量的document

myFunction()添加到您的HTML中,然后直接使用onclick="myFunction();"調用onclick="myFunction();" , 這應該夠了吧。

問候,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM