英文:
Google Appscript unable to invoke server side function using google.script.run function
问题
我的设置如下:
我在我的Google表格中有一个绑定的应用脚本,名为MainCode.gs
和MainPage.html
。除此之外,我还有一个独立的Appscript库,其中包含LibraryCode.gs
和LibraryPage.html
。
MainCode.gs
将显示MainPage.html
作为自定义侧边栏。在MainPage.html
上,有一个按钮,用于在点击时调用来自LibraryCode.gs
的服务器端函数showDialog()
。请注意,库代码脚本被导入为myLibrary
。
MainCode.gs
function showSideBar(){
var widget = HtmlService.createHtmlOutputFromFile("MainPage");
SpreadsheetApp.getUi().showSidebar(widget);
}
function Report(){
myLibrary.showDialog();
}
MainPage.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button type="button" onclick="LogIssue()" class="menu-item-button"> Log Issue </button>
<script>
function LogIssue(){
google.script.run.Report();
}
</script>
</body>
</html>
当调用showDialog()
函数时,LibraryCode.gs
将显示一个带有提交按钮的模式对话框。按下此提交按钮应该触发一个包含"Hello World!"文本的警报。
LibraryCode.gs
function showDialog() {
var html = HtmlService.createHtmlOutputFromFile('LibraryPage');
SpreadsheetApp.getUi().showModelessDialog(html, "Test");
}
function submit(){
SpreadsheetApp.getUi().alert("HELLO WORLD!");
}
LibraryPage.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input type="button" class="menu-item-button" onclick="submitIssue()" value="Submit Issue" />
<script>
function submitIssue(){
google.script.run.submit();
}
</script>
</body>
</html>
我的问题是,当我点击"Submit Issue"按钮时,google.script.run.submit()
函数不起作用。根据我的代码,它应该调用submit()
函数并创建一个包含"Hello World!"文本的警报。问题可能出在哪里?
英文:
My setup is as follow:
I have a bound appscript named MainCode.gs
and MainPage.html
in my google sheet. On top of that, I have a standalone Appscript Library with LibraryCode.gs
and LibraryPage.html
.
The MainCode.gs
will show the MainPage.html
as the custom sidebar. On the MainPage.html
, there will be a button which is used to invoke the server side function showDialog()
from the LibraryCode.gs
when clicked. Please note that the library code script is imported as myLibrary
MainCode.gs
function showSideBar(){
var widget= HtmlService.createHtmlOutputFromFile("MainPage");
SpreadsheetApp.getUi().showSidebar(widget);
}
function Report(){
myLibrary.showDialog();
}
MainPage.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button type="button" onclick="LogIssue()" class="menu-item-button"> Log Issue </button>
<script>
function LogIssue(){
google.script.run.Report();}
</script>
</body>
</html>
When showDialog()
function is called, LibraryCode.gs
will display a Modeless Dialog with a submit button. Pressing this submit button should invoke an alert with "Hello World!" text.
LibraryCode.gs
function showDialog() {
var html = HtmlService.createHtmlOutputFromFile('LibraryPage');
SpreadsheetApp.getUi().showModelessDialog(html, "Test");
}
function submit(){
SpreadsheetApp.getUi().alert("HELLO WORLD!");
}
LibraryPage.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input type="button" class="menu-item-button" onclick="submitIssue()" value="Submit Issue" />
<script>
function submitIssue(){
google.script.run.submit();}
</script>
</body>
</html>
My problem is when I click on the "Submit Issue" button, the google.script.run.submit()
function does not do anything.Based on my code, it should invoke the submit()
function and create alert with "Hello World!" text. What could be the problem here?
答案1
得分: 1
我相信脚本需要在主页面中添加一个submit
函数:
function submit(){
myLibrary.submit();
}
请注意,最佳实践是在界面重的脚本中避免使用库。
英文:
I believe that the script needs a submit
function in the main page:
function submit(){
myLibrary.submit();
}
Please do note that the Best practice is to avoid libraries in UI heavy scripts.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论