我正在尝试将数组推送到Google Apps脚本中的另一个数组中。我从电子表格(数组数组)中提取数据,执行相对于另一个数组的匹配,然后查找将该匹配中的相应值传递给新数组。新数组需要与原始数组(数组数组)具有相同的结构。我没有运气得到那个结果。我试图推动'匹配数组,但我得到一个巨大的数组或一个与原始数组的结构不匹配的较小数组的数组。当我尝试使用基于原始数组循环的索引时,我得到了一个' TypeError'。
/*
Step 1 - Read All Data Into An Array
*/
//Gets Client Data For Each Firm
mysheet = ss.getSheetByName(sheetNames[1]); //Adjusted worksheet
ss.setActiveSheet(mysheet);
arrInput = ss.getRangeByName(rngNameRawClientType).getValues();
//Gets Client Classifcation and Score Data
mysheet = ss.getSheetByName(sheetNames[2]); //Data Validation worksheet
ss.setActiveSheet(mysheet);
arrClassification = ss.getRangeByName(rngNameClient).getValues();
/*
Step 2 - Perform Calculations on the Data
*/
//Iterate Through Raw Data Input Array (Rows)
for(var r = 0; r < arrInput.length; r++) {
//Iterate Through Column of Each Row
for(var c = 0; c < arrInput[r].length; c++) {
var strClientType = arrInput[r][c];
//Compare To Classification Array - Return Corresponding Score
var matchScores = [];
for(var z = 0; z < arrClassification.length-1; z++) {
if(arrClassification[z][0] === strClientType) {
//Add Score to Scores Array
matchScores.push(arrClassification[z][1]);
}
}
}
scores.push(matchScores);
答案 0 :(得分:0)
您需要在两个for循环(scoreRow)之间创建一个空数组,并在每次增加c时将值推入该数组。
然后每次增加r scores.push(scoreRow)
。
for(var r = 0; r < arrInput.length; r++) {
var scoreRow = []
//Iterate Through Column of Each Row
for(var c = 0; c < arrInput[r].length; c++) {
var strClientType = arrInput[r][c];
//Compare To Classification Array - Return Corresponding Score
var matchScores = [];
for(var z = 0; z < arrClassification.length-1; z++) {
if(arrClassification[z][0] === strClientType) {
//Add Score to Scores Array
matchScores.push(arrClassification[z][1]);
}
}
scoreRow.push(matchScores);
}
scores.push(scoreRow);
}