简体   繁体   English

Google Web应用程序将单元素数组视为单个字符数组

[英]google web app treating single-element array as array of individual characters

I am building a web app with Google Apps Script. 我正在使用Google Apps脚本构建网络应用。 It currently consists of an HTML form that passes the form data to a GAS function that retrieves certain information from a Google spreadsheet, depending on the form data. 目前,它由一个HTML表单组成,该表单会将表单数据传递给GAS函数,该函数根据表单数据从Google电子表格中检索某些信息。 It then returns that spreadsheet data to the HTML side and prints out a list. 然后,它将电子表格数据返回到HTML端并打印出一个列表。

Part of the HTML form is a set of checkboxes that looks like this: HTML表单的一部分是一组复选框,如下所示:

<input type="checkbox" name="stage" value="startup" id="startup" />
<label for="startup">Startup</label><br>
<input type="checkbox" name="stage" value="yrs210" id="yrs210" />
<label for="yrs210">Years 2-10</label><br>
<input type="checkbox" name="stage" value="experienced" id="experienced" />
<label for="experienced">Experienced</label><br>
etc...

The checkbox values are compared to the contents of a certain column in the spreadsheet and used to determine what data to return. 复选框values将与电子表格中特定列的内容进行比较,并用于确定要返回的数据。

To make a long story short (too late) , it works as expected when multiple checkboxes are checked. 长话短说(too late) ,当选中多个复选框时,它可以按预期工作。 When only one is checked, however, it is treated not as a single element in an array but as an array made up of each of the characters. 但是,如果仅选中一个,则不会将其视为数组中的单个元素,而是视为由每个字符组成的数组。 For example, "startup" becomes ["s","t","a","r","t","u","p"] . 例如, "startup"变为["s","t","a","r","t","u","p"] Then my script searches for each of those letters in the spreadsheet, and of course there are a lot of matches. 然后,我的脚本会在电子表格中搜索每个字母,当然还有很多匹配项。

This is probably something really basic that I missed in trying to teach myself JavaScript. 在尝试自学JavaScript时,这可能是我真正想念的东西。 Any pointers? 有指针吗? Ideas why this is happening? 为什么会这样?

In case it helps, here is the main part of my script: 如果有帮助,这是我脚本的主要部分:

var stage = formObject.stage;
//get spreadsheet contents
var ss = SpreadsheetApp.openById('ID');
var dataSheet = ss.getSheetByName('sheet1');
var dataRange = dataSheet.getDataRange();
var data = dataRange.getValues();
var cStage = 2; //Number of column containing stages
var cOutput = 4; //Number of column containing output

//compare selections to spreadsheet, list matches
var array1 = [];
for(var i = 0; i < stage.length; i++) {
  for(var j = 0; j < data.length; j++) {
    if(data[j][cStage - 1].indexOf(stage[i]) > -1) {
      array1.push(data[j][cOutput - 1]);
    }
  }
}
//loop to delete duplicate entries
var array = [];
for(var o = 0; o < array1.length; o++) {
  if(array.indexOf(array1[o]) == -1) {
    array.push(array1[o]);
  }
}
return array;

I assume this looks rather stupid, but I am using what I know, and it mostly works, except for this one issue. 我认为这看起来很愚蠢,但是我使用的是我所知道的,并且除了这一个问题外,它大部分都有效。 I am open to any and all advice, especially in regard to this one question about my single-element array being split up into an array of single-character elements. 我乐于接受所有建议,尤其是在有关将我的单元素数组拆分为单字符元素数组的问题上。

Thanks! 谢谢!

When you select multiple checkboxes, the value is passed as an Array of strings as you are expecting. 当您选择多个复选框时,值将按预期方式作为字符串数组传递。 However, when you select only one checkbox, the value is passed as a String, rather than an Array. 但是,当您仅选择一个复选框时,该值将作为字符串而不是数组传递。

Your code is then treating the string as an array, which leads to you looping over the individual characters. 然后,您的代码会将字符串视为数组,从而导致您遍历各个字符。

You can use Array.isArray() to check if you are dealing with an Array or not, then adjust your code accordingly to handle the string. 您可以使用Array.isArray()来检查是否正在处理Array,然后相应地调整代码以处理该字符串。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

You can use a line like the example below to place your plain string into an Array, avoiding the need to make significant modifications to your other code: 您可以使用下面的示例行将纯字符串放入Array中,而无需对其他代码进行重大修改:

if(!Array.isArray(result)) result = [result];

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

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