简体   繁体   English

使用 Google 表格中的数据在 Google Apps 脚本中自动完成

[英]Autocomplete in Google Apps Script with data from Google Sheets

I am trying to use Google Apps Script to create a form, where the fields allow for autocompletion.我正在尝试使用 Google Apps 脚本创建一个表单,其中的字段允许自动完成。 In other forms I've created, I've been able to pull an array of options from a google sheet, and use them to populate a drop down list, so I have to think it's possible to do the same with an autocomplete process.在我创建的其他 forms 中,我已经能够从谷歌工作表中提取一系列选项,并使用它们来填充下拉列表,所以我必须认为可以通过自动完成过程来做同样的事情。

I've blatantly copied this example from w3schools, and it works exactly as needed, as long as I declare the array within the javascript (as done in the example).我公然从 w3schools 复制了这个示例,只要我在 javascript 中声明数组(如示例中所做的那样),它就可以完全按照需要工作。 But what I haven't been able to figure out is how to populate the array with options pulled from my google sheet.但我无法弄清楚的是如何使用从我的谷歌表中提取的选项填充数组。

Here is where I started:这是我开始的地方:

var PMOSupport;

$(function() {
   google.script.run.withSuccessHandler(buildDropDowns)
   .getPMOSupport();
});


function buildDropDowns(data) {
  PMOSupport = data;
  console.log(PMOSupport);
}


function autocomplete(inp, arr) {
  console.log("ENTER AUTO");
  var currentFocus;
  inp.addEventListener("input", function(e) {
  // all of the remaining code is direct from the w3schools example
  // I'm cutting it from here for brevity, 
  // and because I know this works, when using the declared array below
  });
}

var countries = ["Afghanistan","Albania","Algeria","Andorra"];

// this line works fine, when using the array declared above    
// autocomplete(document.getElementById("myInput"), countries);  

// this line does not work, when trying to use the array populated from the google sheet
autocomplete(document.getElementById("myInput"), PMOSupport);  

When I run this, the page creates, and as soon as I type into the entry field, I get a message in the console:当我运行它时,页面会创建,并且在我输入输入字段后,我会在控制台中收到一条消息:

`Uncaught TypeError: Cannot read property 'length' of undefined`
    at HTMLInputElement.<anonymous> (<anonymous>:32:28)

When I look into this, it's saying that the 'arr' argument (PMOSupport) isn't populated.当我研究这个时,它是说'arr'参数(PMOSupport)没有被填充。 That's why I added the 2 console.log lines, to see what order things are happening.这就是为什么我添加了 2 个 console.log 行,以查看事情发生的顺序。 When I open the page, “ENTER AUTO” logs first, then the State Changes from Idle to Busy and Busy to Idle (while it calls getPMOSupport()), then the PMOSupport array logs in the console (also proving that I am in fact getting the correct data back from the sheet).当我打开页面时,首先记录“ENTER AUTO”,然后是 State 从 Idle 变为 Busy 并从 Busy 变为 Idle(当它调用 getPMOSupport() 时),然后 PMOSupport 数组在控制台中记录(也证明我实际上是从工作表中获取正确的数据)。 So clearly, it's entering function autocomplete() before it calls the google.script.run.withSuccessHandler(buildDropDowns).getPMOSupport() portion, which is why the 'arr' argument is undefined.很明显,它在调用 google.script.run.withSuccessHandler(buildDropDowns).getPMOSupport() 部分之前输入了 function autocomplete(),这就是未定义“arr”参数的原因。

I've tried various ways of taking that out of the $(function() … });我尝试了各种方法将其从$(function() … });中取出。 block, to try to get the PMOSupport array populated before the autocomplete() function runs.块,尝试在 autocomplete() function 运行之前填充 PMOSupport 数组。 Nothing I've done seems to be working.我所做的一切似乎都没有奏效。

I'm sure this is something simple, and caused by bad habits I've picked up over time (I'm not a developer, I just cobble things together for my team).我确信这很简单,并且是由我随着时间的推移养成的坏习惯造成的(我不是开发人员,我只是为我的团队拼凑一些东西)。 But any help would be appreciated.但任何帮助将不胜感激。

You need to call autocomplete AFTER the asynchronous code has returned.您需要在异步代码返回后调用autocomplete So, you need to invoke it from the callback.因此,您需要从回调中调用它。

function buildDropdowns(data, userObject) {
  // probably you should indicate in data which field these data is for, or use
  // the userObject parameter in the google.script.run API
  autocomplete(document.getElementById("myInput"), data); 
}

Alternately (I haven't and won't look at the w3schools code), declare your PMOSupport as a const array initially, and then add the entries from your callback into it (instead of reassigning it).或者(我没有也不会查看 w3schools 代码),最初将您的PMOSupport声明为 const 数组,然后将回调中的条目添加到其中(而不是重新分配它)。 This way, the variable is not undefined, it is just empty at the start.这样,变量就不是未定义的,它在开始时只是空的。 Depending on the implementation of the autocomplete code, this may or may not work for you.根据自动完成代码的实现,这可能适合您,也可能不适合您。

const PMOSupport = [];
....
function buildDropdowns(data) {
  PMOSupport.push(...data);
  // or
  // Array.prototype.push.apply(PMOSupport, data);
}

暂无
暂无

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

相关问题 使用 Google 表格中的数组在 html 中创建一个数据列表,该列表可提供给 Google Apps 脚本以自动完成文本输入 - use an array from Google Sheets to create a data list in html that can be served to a Google Apps Script for Autocomplete a Text Input Append Google Apps 脚本中来自表格数据的新幻灯片 - Append New Slides from Sheets Data in Google Apps Script 在 Apps Script 和 Google Sheets 中使用 ImportHTML 进行数据抓取 - Data Scraping With ImportHTML in Apps Script & Google Sheets 从 Apps 脚本显示消息 Google 表格 - Display message Google Sheets from Apps Script 如何从 Google 表格中获取数据并在 Google Apps 脚本中处理它们? - How to get data from Google Sheets and handle them in Google Apps script? Google Apps脚本(在Google表格中)的行为与JavaScript不同 - Google Apps Script (in Google Sheets) behaves differently from JavaScript Google Apps 脚本/Google 表格 - 仅复制 IF - Google Apps Script/ Google Sheets - Only copy IF 在使用谷歌应用脚本将数据从谷歌表格注入谷歌幻灯片期间将日期转换为 dd/mm/yyyy - Convert date to dd/mm/yyyy during injection of data from google sheets to google slides using google apps script 更新工作表、创建 PDF、pdf 中的旧数据(谷歌应用程序脚本) - Update sheets, create PDF, old data in pdf (google apps script) 如何通过使用 arrays 从 Google 表格中提取数据并格式化来优化 Apps 脚本代码? - How to optimise Apps Script code by using arrays to pull data from Google Sheets and format it?
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM