Javascript 从google脚本到html

Javascript 从google脚本到html,javascript,google-apps-script,Javascript,Google Apps Script,我在.gs文件中有一个函数: function testReturn(){ return "Finaly it works!"; } 另一个在html文件中: <script> window.addEventListener('load', function() { google.script.run.withSuccessHandler(createPost).testReturn(); }); /**/ document.getElementByI

我在.gs文件中有一个函数:

function testReturn(){
 return "Finaly it works!";
}
另一个在html文件中:

<script>
  window.addEventListener('load', function() {
    google.script.run.withSuccessHandler(createPost).testReturn();
  });

  /**/

  document.getElementById("go_button").addEventListener("click",functionGo);

  function functionGo(){

    var textToDisplay = google.script.run.testReturn();

    document.getElementById("input1_id").value = textToDisplay;   
  }

</script>

addEventListener('load',function()){
google.script.run.withSuccessHandler(createPost.testReturn();
});
/**/
document.getElementById(“go_按钮”).addEventListener(“单击”,functionGo);
函数functionGo(){
var textToDisplay=google.script.run.testReturn();
document.getElementById(“input1_id”).value=textToDisplay;
}
回报总是“未经罚款”。如何在gs和html脚本之间进行交互?(当然,我不想只返回一个整数,这个项目是要得到一个用许多函数编写的长文本,我只是想找到一种方法来获得结果并在html上显示它)


感谢您

您没有实现
createPost
函数,该函数是回调函数(因为您在
withSuccessHandler
函数[1]中设置了它),它将从code.gs接收
testReturn
函数中返回的值

对于html,下面的代码将在加载页面后立即更新输入值。如果您有一个id设置为“input1\u id”的输入元素,那么它应该适用于您:

<script>
  window.addEventListener('load', function() {
    google.script.run.withSuccessHandler(createPost).testReturn();
  });

  function createPost(returnedValue){
    document.getElementById("input1_id").value = returnedValue;   
  }
</script>

addEventListener('load',function()){
google.script.run.withSuccessHandler(createPost.testReturn();
});
函数createPost(returnedValue){
document.getElementById(“input1_id”).value=returnedValue;
}
如果您希望在单击按钮后更新输入值,则可以改用此选项(假设您有一个id为“go_button”的按钮):


document.getElementById(“go_按钮”).addEventListener(“单击”,functionGo);
函数functionGo(){
google.script.run.withSuccessHandler(createPost.testReturn();
}
函数createPost(returnedValue){
document.getElementById(“input1_id”).value=returnedValue;
}
基本上,使用
google.Script.run
[2]从html调用应用程序脚本函数(code.gs)不会直接返回值,而是必须使用一个或多个处理程序函数[1]中设置的回调函数来管理响应(如本例中的
withSuccessHandler

[1]


[2]

createPost的
函数在哪里?这里有一个完整的示例供您使用;这回答了你的问题吗?谢谢!第二个正是我想要的
<script>
  document.getElementById("go_button").addEventListener("click",functionGo);

  function functionGo(){
    google.script.run.withSuccessHandler(createPost).testReturn();
  }

  function createPost(returnedValue){
    document.getElementById("input1_id").value = returnedValue;   
  }

</script>