【问题标题】:Google Apps Script Pushing to Array IssueGoogle Apps 脚本推送到数组问题
【发布时间】:2014-11-14 00:18:13
【问题描述】:

我使用 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);
  }
}

如果我按原样运行脚本,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

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

【问题讨论】:

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


    【解决方案1】:

    我看到问题是当您将值添加到函数 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);
    
     }
    

    }

    希望有帮助!

    【讨论】:

    • 太棒了!感谢您的时间和帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多