Google Appscript 无法使用 google.script.run 函数调用服务器端函数。

huangapple go评论70阅读模式
英文:

Google Appscript unable to invoke server side function using google.script.run function

问题

我的设置如下:

我在我的Google表格中有一个绑定的应用脚本,名为MainCode.gsMainPage.html。除此之外,我还有一个独立的Appscript库,其中包含LibraryCode.gsLibraryPage.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

Google Appscript 无法使用 google.script.run 函数调用服务器端函数。

MainCode.gs

function showSideBar(){
   var widget= HtmlService.createHtmlOutputFromFile(&quot;MainPage&quot;);
   SpreadsheetApp.getUi().showSidebar(widget); 
   }

function Report(){
   myLibrary.showDialog();
   }

MainPage.html

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;base target=&quot;_top&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
     &lt;button type=&quot;button&quot; onclick=&quot;LogIssue()&quot; class=&quot;menu-item-button&quot;&gt; Log Issue &lt;/button&gt;

     &lt;script&gt;
      function LogIssue(){
        google.script.run.Report();}
     &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

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.

Google Appscript 无法使用 google.script.run 函数调用服务器端函数。

LibraryCode.gs

function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile(&#39;LibraryPage&#39;);
  SpreadsheetApp.getUi().showModelessDialog(html, &quot;Test&quot;);
}

function submit(){
  SpreadsheetApp.getUi().alert(&quot;HELLO WORLD!&quot;);
}

LibraryPage.html

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;base target=&quot;_top&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
     &lt;input type=&quot;button&quot; class=&quot;menu-item-button&quot; onclick=&quot;submitIssue()&quot; value=&quot;Submit Issue&quot; /&gt;

     &lt;script&gt;
       function submitIssue(){
       google.script.run.submit();}
     &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

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.

huangapple
  • 本文由 发表于 2023年6月22日 11:18:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定