javascript - Google Apps 脚本推送到数组问题

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

我使用 Google 表格来帮助跟踪承包商的工资。有时,工作订单有两个或多个承包商,我们不会使用新行来表示相同的信息,只是使用不同的承包商和付款金额,而是使用 / 分隔承包商的姓名及其工资。例如,在承包商栏中我们有:

John Doe/Frank

在薪资栏中我们有:

468/65

工资是各自的,因此约翰欠 468 美元,弗兰克欠 65 美元。

我想做的是为每个承包商设置一个单独的工作表,以便他们几乎可以实时查看自己订单的付款状态,而不会危及其他承包商的信息。我正在使用 Google Apps 脚本来传输信息,并且在大多数情况下它工作正常。我遇到的问题是,当脚本到达有两个订单承包商的行时。奇怪的是,John 的姓名和工资被写入正确的对应数组 (johnDest),但 Frank 的姓名和工资覆盖了 John 之前在该行中的条目。我将写入数组的函数设置为取决于我传入的承包商的个人名称,而不是承包商名称单元格的完整值。 我从每个承包商的数组开始复制电子表格 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 的 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

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

最佳答案

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

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

 }

}

希望有帮助!

关于javascript - Google Apps 脚本推送到数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26920904/

相关文章:

javascript - 带有 Bokeh 的依赖 slider ,如何编写回调

c - 使用 Lex 替换文本文件中句号后的第一个字符

c - 将 sizeof 与 char *array[] 一起使用

php - 计算相似的数组键

javascript - 在 Sheets/App Script 中将字符串拆分为单独单元格的方法

javascript - Jest 显示访问 PropTypes 和 createClass 的 console.warn

javascript - 使用 'j' 和 'k' 键导航到页面部分

javascript - 应用脚本 : filter out empty elements from array

javascript - 返回多个值并访问它们?

javascript - 显示在 spotfire 可视化中标记的行的值?