I have a big array with all data from a spreadsheet from which I derive smaller arrays containing parts of the initial array.
Now, with every step the previous array changes and becomes equal to the derived array until at the end all the arrays are the same.
Any idea what I am doing wrong?
(I am not a pro and still in the process of constructing the script, so please bear with me regarding ugly/inefficient code :)
function dataTables() {
/* All spreadsheet data to tables */
var mastersheet = SpreadsheetApp.openById("xyz");
// Data array of whole sheet with values in "Artikelbezeichnung" (field 6)
var completeTable = mastersheet.getSheetByName("Mastersheet").getDataRange().getValues();
completeTable.splice(0,2); // Remove header lines 1 & 2
for ( i = completeTable.length; i > 0 ; i-- ) {
if (completeTable[i-1][6-1] == "") { completeTable.splice(i-1,1); } // Remove lines with empty "Artikelbezeichnung" (field 6) if necessary
}
Logger.log(completeTable); // Log 1
// Indexeded data array of whole sheet
var completeTableIndexed = completeTable;
for ( i = completeTableIndexed.length; i > 0; i-- ) {
completeTableIndexed[i-1].unshift(i+2);
}
Logger.log(completeTableIndexed); // Log 2
Logger.log(completeTable);
// Data array of all lines eligible for RTN sheet with Indexed
var rtnTableIndexed = completeTableIndexed;
for ( i = rtnTableIndexed.length; i > 0 ; i-- ) {
if ( rtnTableIndexed[i-1][17] == "Nein" ) { rtnTableIndexed.splice(i-1,1); } // Remove lines with "Reparaturcontroller" (field 17) = "Nein" if necessary
else if ( rtnTableIndexed[i-1][28] != "" ) { rtnTableIndexed.splice(i-1,1); } // Remove lines with "Versanddatum G4G -> Hersteller" (tracker, field 28) if necessary
else if ( rtnTableIndexed[i-1][23] == "Ja" && rtnTableIndexed[i-1][25] == "" ) { rtnTableIndexed.splice(i-1,1); } // Remove lines with "KVA (J/N)" (field 23) = "Ja" and KVA is not yet confirmed ("KVA bestätigt am", field 25) if necessary
}
// Data array of all lines eligible for KVA sendings with Indexed
var kvaTableIndexed = completeTableIndexed;
for ( i = kvaTableIndexed.length; i > 0 ; i-- ) {
if ( kvaTableIndexed[i-1][23] != "Ja" && kvaTableIndexed[i-1][24] != "" ) { kvaTableIndexed.splice(i-1,1); } // Remove lines with "KVA (J/N)" (field 23) not "Ja" and "KVA versendet am" (field 24) not empty if necessary
}
var results = [completeTableIndexed,rtnTableIndexed, kvaTableIndexed, completeTable]; // Log 3
Logger.log(completeTableIndexed); // Log 4
Logger.log(rtnTableIndexed);
Logger.log(kvaTableIndexed);
Logger.log(completeTable);
}
Expected output of Log 1 is the complete spreadsheet data without some empty lines - that works just fine and spits out about 15 lines.
Expected output of Log 2 is an indexed array of step one as well as the original array. Instead it outputs the indexed array twice.
Expected output of Log 3 and 4 would be the original array, the indexed original array, and two broken down arrays with each about two lines. Instead, all of the logged arrays are the broken down arrays containing two lines. They are all identical.
In the process the original complete array seems to get overwritten multiple times.