简体   繁体   English

无法将 InputBox 字符串值从 HTML 传递到 Google Apps 脚本

[英]Cannot Pass InputBox String value From HTML To Google Apps Script

I created a sidebar for a Google Document that takes in user input.我为接受用户输入的 Google 文档创建了一个侧边栏。 I'd like to use the collected data to update some fields on my document, but for now, I am just trying to display the data collected on screen via an alert to test if my code is working.我想使用收集到的数据来更新文档上的某些字段,但现在,我只是尝试通过警报在屏幕上显示收集到的数据,以测试我的代码是否正常工作。 Unfortunately, I am stuck and not sure what I am doing wrong.不幸的是,我被卡住了,不确定我做错了什么。

There are two problems I am running into, but due to my limited knowledge of app script and coding as a whole, I just can't seem to resolve my issue.我遇到了两个问题,但由于我对应用程序脚本和整个编码的了解有限,我似乎无法解决我的问题。 The problems I am having are, 1) my eventListener may not be be firing and 2) I can't seem to access the data collected from the input boxes.我遇到的问题是,1)我的 eventListener 可能没有被触发,2)我似乎无法访问从输入框中收集的数据。

Could someone assist me with this problem?有人可以帮我解决这个问题吗? I've been searching through the forum for an answer for a few days now, but again, my knowledge is very limited so it's possible I've come across the answer a few times, but just don't understand it.几天来我一直在论坛中寻找答案,但同样,我的知识非常有限,所以我可能已经多次遇到答案,但就是不明白。 The code I am using is below and a link to my document is here .我使用的代码如下,我的文档链接在这里

App Script Code:应用脚本代码:

function onOpen() {
  // Add a custom menu to the document.
  var ui = DocumentApp.getUi(); // Or SpreadsheetApp or SlidesApp or FormApp.
  ui.createMenu('Menu')
      .addItem('Sidebar', 'showSidebar')
      .addToUi();
}

function showSidebar() {
  // Create HTML template from a file
  var html = HtmlService.createHtmlOutputFromFile('userform')
       .setTitle("TEST FORM")
       .setWidth(300);
  DocumentApp.getUi()
       .showSidebar(html);
}


function appendData(data) {
   DocumentApp.getUi().alert(data.f + ' ' + data.l);
}

HTML code HTML代码

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <div class="container">
             <div>
                <input id="firstName" type="text" name="firstName" placeholder="Enter first name">
             </div>

             <div>
                <input id="lastName" type="text" name="lastName" placeholder="Enter last name">
             </div>

             <div>
                <input type="button" value="SUBMIT" onClick="submitData();"/>
             </div>
   </div> <!-- End of container div -->

   <script>


   function submitData() { 

      var fName = document.getElementById('firstName').value;
      var lName = document.getElementById('lastName').value;
      var data {
         f: fName,
         l: lName
      };

      google.script.run.appendData(data);
   }
   </script>
  </body>
</html>

Try this:尝试这个:

function submitData() { 
  var first=document.getElementById('firstname').value;
  var last=document.getElementById('lastname').value;
  var data={f:first,l:last};
  google.script.run.appendData(data);
}

for simplicity you could also change this <button id="btn">Submit</button> to this <input type="button" value="Submit" onClick="submitData();" />为简单起见,您还可以将此<button id="btn">Submit</button>更改为此<input type="button" value="Submit" onClick="submitData();" /> <input type="button" value="Submit" onClick="submitData();" />

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM