[英]Google App Script Bounded to Spreadsheet
我想要完成的是用于项目管理的Google Spreadsheet。 我在网格中有很多单元格,用户应在其中选择项目是否完成。 现在,此电子表格仅对项目经理可用。 我想象该过程将起作用的方式是,项目经理选择特定的单元格并将其分配给技术人员的电子邮件地址。 然后,脚本将生成移动友好的html UI并将其发送给技术人员(我想到了Google表单,但我想创建更多自定义的UI)。 然后,技术人员将在完成任务后选择一个复选框,该复选框将同时更新电子表格。 下次技术人员打开UI时,它将填充先前选择的所有复选框。 我发现可以使它正常工作的唯一方法是绑定到电子表格的google脚本网络应用。 我创建了一个测试HTML文件和.gs文件:
.html文件
<head> <base target="_top"> <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> </head> <body> <h1> Web App Test </h1> <input type="button" value="Click Me" id="buttonclicked" onclick="getSomeData()"/> <div id="output" class="current">output</div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script> <script> function getSomeData() { google.script.run .withSuccessHandler(onSuccess) .withFailureHandler(showError) .testForWebApp(); myLog("in WebAppTest.html getSomeData()"); } function onSuccess(testParam) { var div = document.getElementById('output'); if (sectionName == null) div.innerHTML = "<p style='color:red;'>You didn't hit the script</p>"; else div.innerHTML = "<p style='color:white;'>" + testParam + "</p>"; } function showError() { var div = document.getElementById('output'); div.innerHTML = "<p style='color:red;'>You didn't hit the script</p>"; } </script> </body>
和.gs文件:
function doGet() { return HtmlService.createHtmlOutputFromFile('WebAppTest') .setSandboxMode(HtmlService.SandboxMode.IFRAME); } function testForWebApp() { myLog("In testForWebApp()"); var msg = "Yep you hit the script!"; return msg; } function myLog(log) { //log = 'test'; Logger.log(log); var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var sheet = spreadsheet.getSheetByName('log'); var lastRow = sheet.getLastRow(); sheet.insertRowBefore(1); var newLogDateRange = sheet.getRange(1, 1); var newLogTextRange = sheet.getRange(1, 2); var now = new Date(); newLogDateRange.setValue(now); newLogTextRange.setValue(log) }
当我发布应用程序并遵循生成的链接时,我看到了带有Click Me
按钮的html页面。 点击事件运行了名为google.script.run
函数的getSomeData()
函数。 服务器端.testForWebApp()
被执行是因为我从myLog()
获得了一个日志条目,但是从未调用过.withSuccessHandler
或.withFailureHandler
。 同时,应该在google.script.run
之后执行的myLog()
google.script.run
不会运行。 我绝对不明白它是如何工作的,并且怀疑如果将脚本发布为Web应用程序,HTML不再受脚本的限制,但是我无法在线找到有关该脚本的任何信息。 谢谢你的帮助。
尝试重新部署Web应用程序,但使用新的项目版本。
首先,除非您使用google.script.run.myLog()
调用它,否则无法从客户端javascript调用服务器端myLog()函数。
myLog("in WebAppTest.html getSomeData()");
在您的getSomeData()
不会在Google工作表中记录任何内容
其次,此代码在onSuccess(testParam)
函数中
if (sectionName == null)
由于没有定义名为sectionName的变量,导致函数过早终止。
注意:您可以在Web浏览器的控制台中监视所有这些错误。
以下是修改后的代码,该代码应按最终代码的预期工作:
function getSomeData() { google.script.run .withSuccessHandler(onSuccess) .withFailureHandler(showError) .testForWebApp(); console.log("in WebAppTest.html getSomeData()"); //Log it on the browser console } function onSuccess(testParam) { var div = document.getElementById('output'); if (testParam == null) // Changed it to testParam from sectionName, to check the value returned from testWebApp() div.innerHTML = "<p style='color:red;'>You didn't hit the script</p>"; else div.innerHTML = "<p style='color:black;'>Success:" + testParam + "</p>"; } function showError() { var div = document.getElementById('output'); div.innerHTML = "<p style='color:red;'>You didn't hit the script</p>"; }
编辑
最后一点,下面的代码将使返回文本不可见,因为文本和背景颜色将是相同的颜色(白色):
div.innerHTML = "<p style='color:white;'>Success:" + testParam + "</p>";
因此在最终代码中将文本颜色更改为黑色
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.