2015-04-12 71 views
0

長期以來的故事是我們在工作中使用3x表格來報告離職者。我想將所有3種形式合併爲一個,刪除任何重複的問題,並使用腳本將響應拆分爲適當的表格。設置Array [i] .length = 13;也改變其他數組長度。爲什麼?

我可以讀取所有我想要的數據並將其寫入正確的工作表。但是,當我嘗試操縱數組的佈局時,它會影響其他兩個數組,我不知道爲什麼。

這是我的代碼首先。

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個欄移動到熱軋板。

但是,這似乎也影響itArray & wfmArray的 他們也得到下調至13數據的列。

我錯過了什麼? 在此先感謝

+0

那麼你可能只需要參考的價值,而不是獨特的陣列。你不應該設置長度,而是限制for循環。 – adeneo

回答

1

values[i]的值是對數組的引用。當你把它推到你的三個其他陣列上時,你推動相同的參考。這並不涉及製作副本。

只有一個數組涉及,因此,當您更新.length值時。

您可以.slice()複製一個數組:

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

謝謝你已經做到了。 嘗試在我需要的佈局中獲取其他2個數組的時間。 非常感謝 – Munkey

相關問題