设置 Array[i].length = 13;也更改其他数组长度。为什么?

Setting Array[i].length = 13; changes other array lengths too. Why?

长话短说,我们在工作中使用 3x 表格来报告离职者。我想将所有 3 种形式合并为一个,删除所有重复的问题并使用脚本将响应拆分为适当的 sheet.

我可以读取所有我想要的数据并写入正确的sheet。但是当我试图操纵一个数组的布局时,它会影响其他两个数组,我不确定为什么。

首先是我的代码。

    function splitForm() {


// Part 1 of the code. Set the variables and read load data.

// Control variables. Can be changed by user.
var done = 22; // In the source sheet, what column number is "Done". If A = 1, B = 2
var when = 23; // In the source sheet, what column number is "Date Moved". If A = 1, B = 2

// Sheet name variables. Can be changed if sheet names change.
var source = "Form responses"; //Name of the sheet with form responses
var hrSheet = "HR"; // Name of the sheet for HR responses
var itSheet = "IT"; // Name of the sheet for IT responses
var wfmSheet = "WFM"; // Name of the sheet for WFM responses


// Temporary array variables. As the script splits data, this is where we'll store the data.
var hrArray = [];
var itArray = [];
var wfmArray = []; 


// Gets the data from the source sheet and loops through it a row at a time, starting at row 2. Row 1 has headers and can be ignored.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(source);
var values = sheet.getDataRange().getValues();




// Part 2 of the code. Read and split the data into formats ready for the 3 different sheets.



for (i=1;i<values.length;i++){


// The if statement is a duplicate check. It checks if the "Done" column is empty. If it is, it then gets data.
if(values[i][done-1] == ""){

// The var name gets the employees full name from the split first and second name.
var name = values[i][2]+ " "+ values[i][3];

// Below we replace the email domain with nothing. Then replace the full stop with a space. This is to give us the "Name of the person submitted form" value
var submit1 = values[i][1].replace("@email-domain.co.uk","");
var submit2 = submit1.replace("."," ");


// Pushes unsubmitted data to the HR data Array

hrArray.push(values[i]);


//hrArray[i-1].length = 13;


itArray.push(values[i]);
wfmArray.push(values[i]);







// All data has been pushed to the various Arrays, time to mark the items as done on the source sheet before it checks the next line of data

//var date = new Date();
//sheet.getRange(i+1, done).setValue("Hell yeah");
//sheet.getRange(i+1, when).setValue(date);

}


}
// Part 3 of the code which writes the data into the appropriate sheets.


var hrLength = hrArray.length;
var itLength = itArray.length;
var wfmLength = wfmArray.length;

Logger.log(itLength);

 if (hrLength != 0){
  var hrTarget = ss.getSheetByName(hrSheet);
  var lastRow = hrTarget.getLastRow();
  var requiredRows = lastRow + hrLength - hrTarget.getMaxRows();
  if (requiredRows > 0) hrTarget.insertRowsAfter(lastRow, requiredRows);
  hrTarget.getRange(lastRow + 1, 1, hrLength, hrArray[0].length).setValues(hrArray);
  }


  if (itLength != 0){
  var itTarget = ss.getSheetByName(itSheet);
  var lastRow1 = itTarget.getLastRow();
  var requiredRows1 = lastRow1 + itLength - itTarget.getMaxRows();
  if (requiredRows1 > 0) itTarget.insertRowsAfter(lastRow1, requiredRows1);
  itTarget.getRange(lastRow1 + 1, 1, itLength, itArray[0].length).setValues(itArray);
  }


  if (wfmLength != 0){
  var wfmTarget = ss.getSheetByName(wfmSheet);
  var lastRow2 = wfmTarget.getLastRow();
  var requiredRows2 = lastRow2 + wfmLength - wfmTarget.getMaxRows();
  if (requiredRows2 > 0) wfmTarget.insertRowsAfter(lastRow2, requiredRows2);
  wfmTarget.getRange(lastRow2 + 1, 1, wfmLength, wfmArray[0].length).setValues(wfmArray);
  }


}

这是我遇到问题的地方,这部分

hrArray.push(values[i]);


//hrArray[i-1].length = 13;


itArray.push(values[i]);
wfmArray.push(values[i]);

如果我添加行 hrArray[i-1].length = 13; 那么 hrArray 是完美的,只有前 13 列被移动到 HR sheet.

但这似乎也影响了 itArraywfmArray 它们也被缩减为 13 列数据。

我错过了什么? 提前致谢

values[i] 的值是对数组的引用。当您将其推送到其他三个数组时,您将推送 相同的引用 。这不涉及复制。

只涉及一个数组,因此,当您更新 .length 值时。

您可以使用 .slice() 复制数组:

hrArray.push(values[i].slice(0));