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)"
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();
// ...
}
You can define arrays simply as follows, instead of allocating and then assigning. Name[0] = Name_cell. getValue();
Create arrays You can also create your own arrays in a formula in your spreadsheet by using brackets { }. The brackets allow you to group together values, while you use the following punctuation to determine which order the values are displayed in: Commas: Separate columns to help you write a row of data in an array.
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[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.
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! ;)
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With