繁体   English   中英

Google Apps脚本推送到数组问题

[英]Google Apps Script Pushing to Array Issue

我使用Google表格来帮助跟踪向承包商的付款。 有时,工作秩序有两个或两个以上的承包商和,而不是使用一个新行相同的信息只是一个不同的承包商和支付金额,我们分开承包商的名字以及他们与付费/ 因此,例如,在承包商列中,我们有:

John Doe/Frank

在薪资栏中,我们有:

468/65

工资是分别的,所以约翰欠了468美元,弗兰克欠了65美元。

我正在尝试为每个承包商设置单独的工作表,以便他们几乎实时地查看自己订单的付款状态,而不会损害其他承包商的信息。 我正在使用Google Apps脚本传输信息,并且在大多数情况下它可以正常运行。 我遇到的问题是,脚本到达一行时有两个承包商的订单。 奇怪的是,约翰的名字和薪水被写到正确的对应数组(johnDest),但随后弗兰克的名字和薪水覆盖了约翰在该行的上一个条目。 我将写入数组的功能设置为取决于我要传入的承包商的个人名称,而不是承包商名称单元格的完整值。 我从每个承包商要复制电子表格ID的数组开始:

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);
  }
}

如果按原样运行脚本,则记录器将显示以下信息:

[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的脚本实际上是否在开关箱中,我在frankDest.push(contValues);之后注释了Logger行frankDest.push(contValues); 上面的行,我得到这个:

[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的工作表中的最终结果是:

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

对于弗兰克的信息为何写入约翰的数组以及他自己的行中,我感到非常困惑。 任何帮助是极大的赞赏。 谢谢!

我看到的问题是,当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);

 }

}

希望有帮助!

暂无
暂无

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

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