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

时间:2015-04-12 15:25:18

标签: javascript arrays google-apps-script google-sheets

长期以来,我们在工作中使用3x表格来报告离场者。我想将所有3个表单合并为一个,删除任何重复的问题,并使用脚本将响应拆分为相应的表单。

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

首先是我的代码。

    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表。

但这似乎也会影响itArray&amp; wfmArray 它们也被削减到13列数据。

我错过了什么? 提前致谢

1 个答案:

答案 0 :(得分:1)

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

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

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

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