简体   繁体   English

为什么我的 Google Apps 脚本中的 arrays 会被覆盖?

[英]Why do the arrays in my google apps script get overwritten?

I have a big array with all data from a spreadsheet from which I derive smaller arrays containing parts of the initial array.我有一个大数组,其中包含来自电子表格的所有数据,我从中派生出较小的 arrays 包含部分初始数组。

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.现在,随着每一步,前面的数组都会改变并变得等于派生数组,直到最后所有的 arrays 都相同。

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.日志 1 的预期 output 是完整的电子表格数据,没有一些空行 - 工作得很好并且吐出大约 15 行。

Expected output of Log 2 is an indexed array of step one as well as the original array. Log 2 的预期 output 是第一步的索引数组以及原始数组。 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.日志 3 和 4 的预期 output 将是原始数组、索引原始数组和两个分解的 arrays,每个大约两行。 Instead, all of the logged arrays are the broken down arrays containing two lines.相反,所有记录的 arrays 都是包含两行的分解 arrays。 They are all identical.它们都是相同的。

In the process the original complete array seems to get overwritten multiple times.在此过程中,原始完整数组似乎被多次覆盖。

Instead of using this var completeTableIndexed = completeTable;而不是使用这个var completeTableIndexed = completeTable;

try this:尝试这个:

var completeTableIndexed=[];
completeTable.forEach(function(r){completeTableIndexed.push(r.slice();});

and that will return a copy of the entire array and not a reference to it.这将返回整个数组的副本,而不是对它的引用。 While researching this I found that slice doesn't appear to work on multidimensional arrays.在研究这个时,我发现切片似乎不适用于多维 arrays。 I don't know if this will fix all of your problems because your question is quite broad.我不知道这是否会解决您的所有问题,因为您的问题非常广泛。 But it may help get you a little further down the road.但这可能会帮助您走得更远。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM