Dictionary Syntax Error in Google Apps Script -
i have function creates dictionary based on series of values sheet. try pull 1 of values sheet using key. works fine log console. however, in if statement, says syntax error , nothing else. cannot figure out. here function , code crashes. problem occurs in loop, , not occur outside of it.
//creates dictionary function columnlocationwithnotation(notation) { var spreadsheet = spreadsheetapp.openbyurl(); var sheet = spreadsheet.getactivesheet(); var data = sheet.getdatarange(); var cells = data.getvalues(); var dictionary = {}; switch (notation) { case "zeroindex": (var = 0; < sheet.getlastrow(); i++) { dictionary[cells[i][0]] = cells[i][1] } return dictionary break; case "regularindex": (var = 0; < sheet.getlastrow(); i++) { dictionary[cells[i][0]] = cells[i][2] } return dictionary break; case "string": (var = 0; < sheet.getlastrow(); i++) { dictionary[cells[i][0]] = cells[i][3] } return dictionary break; } } var master0indexdictionary = columnlocationwithnotation("zeroindex") (var = 1; =< (sheet.getlastrow() - 1); i++) { var phone = master0indexdictionary["tutor name"] if (cells[i][phone] === phonenumber) { //line syntax error //do }
it's not highlighted line causing problem, though there many other issues script. there's no '=<' operator in javascript. use '<=' instead:
for (var = 1; <= (sheet.getlastrow() - 1); i++) {
also, tanaike pointed out, 'cells' variable defined in context of 'columnlocationwithnotation(notation)' function , not accessible global context. globally-defined variables visible functions declare inside global object, not vice versa. same applies 'sheet' variable. 'phonenumber' variable doesn't seem defined, @ least not in snippet of code provided.
note putting 'break' after 'return' statements redundant.
return dictionary; break;
you can return out of 'switch' statement without using breaks, or leave breaks , put single 'return' statement after 'switch'.
finally, put semicolon @ end of line. doing avoid many potential pitfalls , issues js parser. noticed several instances omitted semicolon:
return dictionary break; var phone = master0indexdictionary["tutor name"]
for example, following code break if don't have habit of putting semicolon in rightful place
var = {name: 'john'} //no semicolon [a].foreach(function(element) { logger.log(element); //logs undefined })
the js parser treats code 1 line, 'a' still 'undefined' time call 'foreach()' loop on array.
Comments
Post a Comment