简体   繁体   English

服务器端代码在Google App脚本中不起作用

[英]Server side code not working in google app script

Hi I am trying to do a simple thing: 1) create two links in my HTML page 2) When user clicks on link1- I want it to call a server side function that displays "Hello World". 嗨,我正在尝试做一个简单的事情:1)在我的HTML页面中创建两个链接2)当用户单击link1-时,我希望它调用显示“ Hello World”的服务器端函数。 But when i click on link1- I am not able to see "Hello World" getting displayed anywhere. 但是,当我单击link1-时,我看不到“ Hello World”显示在任何地方。 I have tried doing this multiple times with different variations but it is not working. 我尝试使用不同的变体多次进行此操作,但是它不起作用。 Thanks in advance !! 提前致谢 !!

Here is my code 这是我的代码

Code.gs Code.gs

function doGet() {
  return HtmlService.createTemplateFromFile('IndexHTML').evaluate()
      .setTitle('Simple App')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function doSomething(){
   return ContentService.createTextOutput('Hello World');
}

IndexHTML.html IndexHTML.html

<!DOCTYPE html>
<html>
<head>
<?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>
</head>
<body>

<h1>Simple App</h1>
<p>
 Click on the buttons to view roles
</p>

<a href = "#" id = "index1" >I am index1</a>
    <a href = "#" id = "index2" >I am index2</a>

<?!= HtmlService.createHtmlOutputFromFile('JavaScript').getContent(); ?>

</body>
</html>

JavaScript.html JavaScript.html

<!-- Load the jQuery and jQuery UI libraries. -->
<script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.0/jquery-ui.min.js"></script>

<!-- Custom client-side JavaScript code. -->
<script>
$(document).ready(function() {
    $("#index1").click(function() {
       google.script.run.doSomething()     ;
}

    });
});
</script>

Stylesheet.html Stylesheet.html

<!-- Load the jQuery UI styles. -->
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />

<!-- Custom styles. -->
<style>
body
{
background-color:red;
}
</style>

You are not seeing "Hello World" displayed anywhere because you have not written any code to display it! 您没有看到“ Hello World”显示在任何地方,因为您尚未编写任何代码来显示它!

All your code does is run server-side doSomething() function, which returns text output. 您的所有代码都运行在服务器端doSomething()函数中,该函数返回文本输出。 You need to add a success handler to your google.script.run and specify a callback function to run after the server-side function returns successfully, eg: 您需要在google.script.run添加成功处理程序,并指定在服务器端函数成功返回后运行的回调函数,例如:

google.script.run.withSuccessHandler(processResult).doSomething();

Then write a processResult() javascript function that accepts server return as first argument and does whatever you need to do with it, ie: 然后编写一个processResult() javascript函数,该函数接受服务器返回作为第一个参数,并执行您需要做的任何事情,即:

function processResult(data) {
  console.log(data);
}

See google.script.run reference and HTML Service success handlers reference for more details. 有关更多详细信息,请参见google.script.run参考HTML Service成功处理程序参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将服务器端代码更改为脚本不起作用 - changing server side code to script not working Google脚本中对服务器端功能的异步调用 - Asynchronous call to server side function in google script Google Slide modalDialog HTML javascript 可以运行服务器端代码吗? 环境应用脚本 - Can Google Slide modalDialog HTML javascript run server-side code? Environment Apps Script 自定义脚本在 Blazor 服务器端工作出错 - Custom script working wrong in Blazor server side 表单验证后服务器端脚本无法正常工作 - Server side script not working after form validation 具有 Google Apps 脚本值的服务器端和客户端通信函数 - Server Side and Client Side communications Functions with values at Google Apps Script Google应用引擎上的服务器端javascript - Server side javascript on Google app engine 谷歌图表示例“使用服务器端代码填充数据”无法正常工作 - the google charts example “Populating Data Using Server-Side Code ” is not working 使用JavaScript和Google App Engine中的服务器端Python代码动态生成客户端HTML表单控件 - Dynamically generate client-side HTML form control using JavaScript and server-side Python code in Google App Engine *服务器端* Google Apps容器绑定脚本中的jQuery库 - jQuery library in *server side* Google Apps Container Bound Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM