javascript 在 Google Scripts 中定义数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18584081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Defining arrays in Google Scripts
提问by derekantrican
I'm a javascript newbie and trying to write a script for a spreadsheet that will pull various things out of it. Right off the bat, I'm having trouble defining an array of names that will be in the spreasheet. The error says "Missing ; before statement (line 10)"
我是 javascript 新手,正在尝试为电子表格编写脚本,以便从中提取各种内容。马上,我在定义将在电子表格中的名称数组时遇到了麻烦。错误说“缺少;在语句之前(第 10 行)”
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
//var values = rows.getValues();
var Names = sheet.getRange("A2:A7");
var Name = new Array(6);
var Name_cell = Names.getCell(1, 1);
var Name[0] = Name_cell.getValue(); // <-- Here's the issue
var Name_cell = Names.getCell(2, 1);
var Name[1] = Name_cell.getValue();
var Name_cell = Names.getCell(3, 1);
var Name[2] = Name_cell.getValue();
var Name_cell = Names.getCell(4, 1);
var Name[3] = Name_cell.getValue();
var Name_cell = Names.getCell(5, 1);
var Name[4] = Name_cell.getValue();
var Name_cell = Names.getCell(6, 1);
var Name[5] = Name_cell.getValue();
// ...
}
回答by Karthik T
Try this
试试这个
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
//var values = rows.getValues();
var Names = sheet.getRange("A2:A7");
var Name = [
Names.getCell(1, 1).getValue(),
Names.getCell(2, 1).getValue(),
.....
Names.getCell(5, 1).getValue()]
You can define arrays simply as follows, instead of allocating and then assigning.
您可以简单地按如下方式定义数组,而不是先分配再分配。
var arr = [1,2,3,5]
Your initial error was because of the following line, and ones like it
您最初的错误是因为以下行,以及类似的
var Name[0] = Name_cell.getValue();
Since Name
is already defined and you are assigning the values to its elements, you should skip the var
, so just
由于Name
已经定义并且您正在为其元素分配值var
,因此您应该跳过,所以只需
Name[0] = Name_cell.getValue();
Pro tip: For most issues that, like this one, don't directly involve Google services, you are better off Googling for the way to do it in javascript in general.
专业提示:对于大多数问题,比如这个,不直接涉及谷歌服务,你最好在谷歌上搜索一下在 javascript 中执行它的方法。
回答by mario
I think that maybe it is because you are declaring a variable that you already declared:
我认为这可能是因为您声明了一个已经声明的变量:
var Name = new Array(6);
//...
var Name[0] = Name_cell.getValue(); // <-- Here's the issue: 'var'
I think this should be like this:
我认为这应该是这样的:
var Name = new Array(6);
//...
Name[0] = Name_cell.getValue();
Tell me if it works! ;)
告诉我它是否有效!;)
回答by Terry
This may be of help to a few who are struggling like I was:
这可能对一些像我一样挣扎的人有所帮助:
var data = myform.getRange("A:AA").getValues().pop();
var myvariable1 = data[4];
var myvariable2 = data[7];