List <string>语法错误google脚本

时间:2016-01-15 02:50:17

标签: javascript arrays google-apps-script

我无法理解在Google脚本中设置数组(基于Javascript)。

我有一个国家列表,'AU'和'NZ',我需要在数组中,这将添加到稍后。我将需要稍后搜索数组以检查字符串是否与数组中的值匹配。

我尝试在Google脚本中添加几种方法。

第一次尝试:

List<String> country_list = new ArrayList<String>();
country_list.add("AU");
country_list.add("NZ");

这会在第1行引发Syntax error.

对此的变体:

第二次尝试:

var country_list = [];
country_list.add("AU")
country_list.add("NZ")

在第二行引发错误TypeError: Cannot find function add in object .

第三次尝试:

var Country_List = ["AU","NZ"];
if (country_list.contains("AU")) {
    // is valid
    ui.alert("great success");
} else {
    // not valid
    ui.alert('Nope try again');
}

这会引发错误'TypeError:找不到对象中包含的函数...'

这是有道理的,所以我尝试转换为Array.asList()

第四次尝试:

var original_country_list = ["AU","NZ"];
List<String> country_list = Arrays.asList(original_country_list)

if (country_list.contains("AU")) {
    // is valid
    ui.alert("great sucsess");
} else {
    // not valid
    ui.alert('Nah Mate try again');
}

这会在使用Invalid assignment left-hand side[]保留()时引发错误original_country_list。使用{}保留original_country_list时,会引发错误Missing : after property ID

我尝试的最后一件事是:

第五次尝试:

var original_country_list = ["AU","NZ"];
var country_list = Arrays.asList(original_country_list)

这会引发错误ReferenceError: "Arrays" is not defined

道歉,如果这很明显,但这里出了什么问题?

1 个答案:

答案 0 :(得分:0)

我在这里看到的大多数内容看起来比Javascript更接近Java。但#2可以是这样的Javascript:

var country_list = [];
country_list.push("AU");
country_list.push("NZ");

3也很容易修复:

var Country_List = ["AU","NZ"];
if (Country_list.indexOf("AU") > -1) {
  // is valid
  ui.alert("great success");
 } else {
   // not valid
   ui.alert('Nope try again');
 }