简体   繁体   中英

Google App Script TypeError: Cannot read property “length” from undefined

I am receiving the length error when running this script, but if I comment out the condition and pull everything there is no issue. Error in line "sp.getRange(1,1,data.length,data[0].length).setValues(data); "

I have triple-checked that column 22 is the column I want (column V) and the criteria "Denver" exist in that column but nothing is being returned to data.

Any explanation would be appreciated.

function Packaging() {
 var t = SpreadsheetApp.openById('1kNq2irlV39yDRtuJPrkb3-572guw4wn9UsR2t5_I0pR');

 //Grab PACKAGING COST
var pp = t.getSheetByName('RawData');
var ppk = pp.getRange(1,1,pp.getLastRow(),pp.getLastColumn()).getValues();
var data = []                       
for (var i = 0; i< ppk.length ; i++){
if(ppk[i][22] == "Denver")        
{
data.push(ppk[i])
}
}

//Write in current sheet
var s = SpreadsheetApp.getActiveSpreadsheet();


//Write Packaging
var sp = s.getSheetByName('PackagingCost2');
sp.getRange(1,1,sp.getLastRow(),sp.getLastColumn()).clearContent();
sp.getRange(1,1,data.length,data[0].length).setValues(data);  
//sp.getRange(1,1,ppk.length,ppk[0].length).setValues(ppk);  

}

问题在于JavaScript数组使用基于0的索引,因此,列V的索引为21,而不是22,并且由于列V是最后一列,您会收到未定义错误消息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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