[英]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.