Google Apps脚本推送阵列问题

时间:2014-11-14 00:18:14

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

我使用Google表格帮助跟踪承包商的付款情况。有时工单有两个或更多的承包商,而不是使用新行作为相同的信息只是一个不同的承包商和支付金额,我们将承包商的名称以及他们的工资与/分开。例如,在承包商专栏中,我们有:

John Doe/Frank

在付款栏中,我们有:

468/65

薪水是相应的,所以约翰欠468美元,弗兰克65美元。

我要做的是为每个承包商设置一张单独的表格,这样他们几乎可以实时查看自己订单的付款情况,而不会危及其他承包商和#39;的信息。我使用Google Apps脚本传输信息,而且大多数情况下它都正常运行。我遇到的问题是当脚本到达有两个订单承包商的行时。奇怪的是,约翰的名字和薪水被写入正确的相应数组(johnDest),但随后弗兰克的名字和薪水将覆盖约翰在此行的前一个条目。我设置写入数组的功能取决于我传入的承包商的个人名称,而不是承包商名称单元格的全部价值。 我从每个承包商的数组开始,电子表格用于复制:

var johnDest = [];
var john = "this-is-the-link-to-johns-sheet";
var frankDest = [];
var frank = "this-is-the-link-to-franks-sheet";

然后我进入一个循环,将行值添加到这些数组中,这样我最终可以将数组写入各自的电子表格中:

function exportData() {

    var columnM = thisWorksheet.getRange(2, 1, thisWorksheet.getLastRow(), thisWorksheet.getLastColumn());
    var mValues = columnM.getValues();

    for(var i = 0; i < mValues.length; i++){
      var mName = mValues[i][0];
      if(mName.indexOf('/') > -1){ //If contractor name column contains a '/' split it.
        var names = mName.split('/');
        var pays = mValues[i][1];
        pays = pays.split('/');
        for(var g = 0; g < names.length; g++){ //For each name in split array, get name and corresponding pay to add to array.
           var cName = names[g];
           mValues[i][0] = names[g];
           mValues[i][1] = pays[g];
           Logger.log(cName); //To log the contractor's name that I am currently working with in the loop.
           switchcontractor(cName, mValues[i]);
        }
      }else{
        switchcontractor(mName, mValues[i]);
      }
    }
    copyData(john, johnDest); //Once loop is through and arrays are completed, copy data to respective sheets.
    copyData(frank, frankDest);
}

以下是switchcontractor功能:

function switchcontractor(cName, contValues){
  Logger.log(johnDest.length + ' ' + cName); //Log the length of johnDest and the current contractor in the loop.
  if(cName == 'John'){
    johnDest.push(contValues);
  }else if(cName == 'Frank'){
    frankDest.push(contValues);
    Logger.log(johnDest.length + ' ' + cName + ' ' + contValues);
  }
}

如果我按原样运行脚本,Logger会显示以下信息:

[14-11-13 16:16:01:843 MST] John  //Current contractor I'm working with
[14-11-13 16:16:01:843 MST] 23 John  //Current length of johnDest before row information is pushed to it and contractor's name
[14-11-13 16:16:01:844 MST] 24 John John,468  //Updated length of johnDest, current contractor, and row information
[14-11-13 16:16:01:844 MST] Frank  //Current contractor
[14-11-13 16:16:01:844 MST] 24 Frank  //Current length of johnDest and contractor's name
[14-11-13 16:16:01:845 MST] 24 Frank Frank,65  //Length stays the same for johnDest, current contractor and row information 

为了验证脚本是否真的在Frank的switchcase中,我在上面的frankDest.push(contValues);行之后注释了Logger行,我给出了这个:

[14-11-13 16:22:52:684 MST] John
[14-11-13 16:22:52:684 MST] 23 John
[14-11-13 16:22:52:684 MST] 24 John John,468
[14-11-13 16:22:52:685 MST] Frank
[14-11-13 16:22:52:685 MST] 24 Frank
//The Logger line that was commented out in the switchcase for Frank doesn't show, so obviously I'm in that case, right?

然而,当Frank的名字通过循环时,它被写入johnDest数组以及Frank自己的数组。 约翰小册子的最终结果如下:

John     |   55   //This is an example of a previous row
Frank    |   65   //This is the row in question
John     |   125  //This is an example of a following row

这就是弗兰克的表格:

Frank    |    25  //This is an example of a previous row
Frank    |    65  //This is the row in question
Frank    |    15  //This is an example of a following row

我很困惑为什么弗兰克的信息被写入约翰的数组而他自己的数据写入该行。任何帮助是极大的赞赏。谢谢!

1 个答案:

答案 0 :(得分:1)

我看到问题是当if条件满足时,将值添加到函数switchcontractor(cName,contValues)。对代码进行了以下更改,它可以正常工作。

function exportData() {

 var sheet = SpreadsheetApp.getActiveSheet();
 var columnM = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());
 var mValues = columnM.getValues();
 for(var i = 0; i < mValues.length; i++){
  var mName = mValues[i][0];
  if(mName.indexOf('/') > -1){ //If contractor name column contains a '/' split it.
    var names = mName.split('/');
    var pays = mValues[i][1];
    pays = pays.split('/');

    for(var g = 0; g < names.length; g++){ //For each name in split array, get name and corresponding pay to add to array.
       var cName = names[g];
      var addValues = [];
       addValues[0] = names[g];
       addValues[1] = pays[g];
//           Logger.log(cName); //To log the contractor's name that I am currently working with in the loop.
       switchcontractor(cName, addValues);
    }
  }else{
    switchcontractor(mName, mValues[i]);
  }
}
 Logger.log('John values : ' + johnDest);
 Logger.log('Frank values: ' + frankDest);
 copyData(john, johnDest); //Once loop is through and arrays are completed, copy data to respective sheets.
 copyData(frank, frankDest);
}

function switchcontractor(cName, contValues){
 Logger.log(cName + 'value is ' + contValues); 
 if(cName.search('John Doe') > -1){
   johnDest.push(contValues);
 }else if(cName == 'Frank'){
   frankDest.push(contValues);

 }

}

希望有所帮助!