比较谷歌脚本

时间:2016-11-08 13:23:18

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

我有2个多维数组,我正在尝试比较数据。虽然嵌套数组的长度可能会根据具体情况而有所不同,但对于每个实例,长度将相同并在两个数组之间进行镜像,并且每个数组的第一个数组[0]将包含完全相同的信息。唯一可能/可能变化的是标题可能出现在数组[0]中的顺序。它可能是

  

h1,h2,Id

在第一个和

  

Id,h1,h2

在第二个数组中。

完整示例

第一阵列

  

[[Header1,Header2,ID],[dataA,dataB,000]]

第二阵列

  

[[Header1,Header2,ID],[dataA,dataB,111]]

问题

我如何解析,循环,拆分,以及任何(对此仍然是新的)以便我可以比较每个值并将它们全部放入一个数组中?

结果通缉

  

[[oldHeader1,oldHeader2,oldID,newHeader1,newHeader2,newID],[dataA,dataB,000,dataA,dataB,111]]

是的,两个来源的dataA和dataB应该是相同的。这就是我所反对的,每个ID都是唯一不同的东西。如果dataA不匹配但dataB不匹配,我想继续但添加日志记录。但那是后来的一步。

我已经考虑过循环第一个数组并获取每个值,然后循环遍历第二个值以查看该值是否存在。这将是好的和好的,但我似乎无法弄清楚如何.push这个价值并保持一切井井有条。这些值也是从表格中提取的,但我读到生成数组并使用Google服务器完成工作而不是查看每个单元格,查看或查找,返回结果,++和迭代更快。

这里的任何建议都会很棒,谢谢!

以下是我用于生成2个数组的代码,我将比较为FYI。也许这里有什么我可以修改的东西?我将此函数调用两次,其中包含一个包含源信息的文件和一个包含目标信息的文件。

//This will take a sheet and an array of headers and return the subset in a new array 
function grabColumnByHeader(headers,sheet,whichID) {
  var initialArray = new Array(); //Array to store sheet data
  var headerIndex = new Array(); //Blank array to house header index(es)
  var outputArray = []; //Will be returned
  var data = sheet.getDataRange(); //all data
  var dataNumRows = data.getNumRows(); //number of rows
  var dataNumCol = data.getNumColumns(); //number of columns
  initialArray = sheet.getRange(1, 1, dataNumRows, dataNumCol).getValues();

  //Get the index(es) of header(s). This is assuming that headers are in row 1
  for (i = 0;i<1;++i){
    for (j = 0;j<dataNumCol;++j){
      //loop through headers var for each header 
      for (k = 0;k<headers.length;++k){
        if(initialArray[i][j].toString().toUpperCase() == headers[k][i].toString().toUpperCase()) {
          headerIndex.push(j);
        }
      }
    }
  }

  //Using the array's indexes from headerIndex, loop through and grab values
  //If coming from SOURCE file, prepend 'SOURCE_'
  for (i = 0;i<dataNumRows;++i) {
    outputArray[i] = [];
    for (j = 0;j<headerIndex.length;++j) {
      if (i == 0 && whichID == 'TRUE') {
        outputArray[i][j] = 'SOURCE_' + initialArray[i][headerIndex[j]];
      } else {
        outputArray[i][j] = initialArray[i][headerIndex[j]];
      }
    }
  }
  //Logger.log(outputArray);
  return outputArray;
}

更新

以下是我能够将谷歌融合在一起并使用我的基本知识的代码。我意识到它正在执行不必要的循环,这仍然是一项正在进行的工作:

  var tempArray = [];
  var idIndex;
  //get index of ID so it can be skipped in comparison
  for (i=0;i<1;++i) {
    for (j=0;j<newData[i].length;++j) {
      if (newData[i][j].toString().toUpperCase() == 'ID') {
        idIndex = j;
      }
    }
  }
  //Logger.log(idIndex);
  for (i=0;i<newData.length;++i) {
    tempArray[i] = [];
    //if on headers, automatically concatenate and add to tempArray
    if (i==0) {
      tempArray[i] = newData[i].concat(oldData[i]);
    }
    else {
      //Logger.log('newData['+i+']');
      for (j=0;j<newData[i].length;++j) {
        //for newData[i][j], begin looking in oldData
        //if we're not comparing indexes then
        if (j != idIndex) {
          //Logger.log('newData['+i+']['+j+']');
          //begin looping through the oldData arrays
          for (k=0;k<oldData.length;++k){               
            //Logger.log('newData['+i+']['+j+'] == oldData['+k+']['+j+']');
            if (newData[i][j] == oldData[k][j]) {
              //NEED TO MAKE SURE HERE THAT ++j IS THE SAME TOO
              tempArray[i] = newData[i].concat(oldData[k]);//continue on with j
              break;

            }
          }
        }
        //continue through for(j)
      }
    }
    //continue through for(i)
  }
  output.getRange(1, 1, tempArray.length, tempArray[0].length).setValues(tempArray);

1 个答案:

答案 0 :(得分:0)

经过充分的试验和错误后找到了我自己的答案。可能不是专业人士会这样做的方式,但对我有用:

  var tempArray = []; //will be used for output
  var sameArray = []; //will look like this [sameIndex,sameCount]
  var sameIndex = ''; //will get the index number of match to be used when pushing to tempArray
  var sameCount = 0; //if count is == my keys-ID then push to tempArray

  //concat the headers, as this will be a constant
  tempArray[0] = newData[0].concat(oldData[0]);

  for (i=1;i<newData.length;++i) {
    //reset variables after each [i]
    //these are used to verify if my newData array and oldData array are the same
    sameArray = [];
    sameIndex = '';
    sameCount = 0;
    for (j=0;j<newData[i].length;++j) {
      //for newData[i][j], begin looking in oldData
      //we're not comparing our IDs because we know they'll be different
      //pulled those out earlier and set to idIndex
      if (j != idIndex) {
        //begin looping through the oldData arrays
        for (k=1;k<oldData.length;++k){
          for (l=0;l<oldData[k].length;++l) {
            if (l != idIndex) {
              if (newData[i][j] == oldData[k][l]){
                sameArray[0] = k;
                sameArray[1] = ++sameCount;
              }
            }
          }
        }
      }
      //since looped through all of oldData array, check to see
      //if the amount of matches in a single row were keyCount-1
      //keyCount is my variables and we subtract 1 because not comaring IDs
      if (sameArray[1] == keyCount-1) {
        tempArray.push(newData[i].concat(oldData[sameArray[0]]));
      } else {
        //Will be used to catch indexes that didn't match
      }
    }
  }
  output.getRange(1, 1, tempArray.length, tempArray[0].length).setValues(tempArray);