【问题标题】:for loop not iterating over last element of 2D array in google app script (GAS)for 循环没有迭代谷歌应用脚​​本(GAS)中二维数组的最后一个元素
【发布时间】:2016-05-26 12:44:49
【问题描述】:

我正在编写一个脚本,该脚本部分采用一组名称,将每个名称与工作表中的 A 列进行比较,并返回 B 列中的行匹配值。(就像工作表中的 vLookup 命令)

设置

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var clientsSheet = ss.getSheetByName("Clients");
  var cRow = clientsSheet.getLastRow();
  var cColumn=clientsSheet.getLastColumn();
  var cData=clientsSheet.getRange(1,1,cRow,cColumn).getValues(); //create array of client data

故障码

//put each client on their own row and add hour
for(i=0; i < client.length; ++i){
var cl = client[i]

//iterate over array of clients (column A) and hours (Column B) to find match and log the number in column B
for(j=0;j<cData.length;++j){
 if (cData[j][0]==cl){
    var hour = cData[j][1]; 

     }
   }
//return the matched values
Logger.log(cl+" - "+hour);
}

var 'client' 是一个数组,它是从以逗号分隔的单个单元格中的名称列表拆分出来的(请参阅下面的完整代码)

目前它工作得很好,只是它错过了数组中的最后一个元素。

例如:

如果我有一张这样的两列三行工作表:

A    1
B    2
C    3

我会回来的

A-1
B-2
C-

最后一个元素上的最后一块丢失了 - 它应该是

A-1
B-2
C-3

我被难住了,我知道这一定是一些简单的小事。

任何帮助都会很棒 谢谢!

代码:

function logClients()

/*
Take data from a google form check box submissions.  Check box submissions put all checked answers into a single cell separated by a comma.  The function first takes the most recently submitted row, removes unneeded spaces, and splits each element into its own part of an array. 

Then, the function compares each clients name in the array to a sheet with other info, such as the default number of hours we meet.  It takes the clients name, the date of submission, and the hours, and logs them on a new row in two different sheets, the Hours sheet and the Trans Log sheet.
*/

var ss = SpreadsheetApp.getActiveSpreadsheet();
var logSheet = ss.getSheetByName("Log");  //Raw data from the Google form
var hourSheet = ss.getSheetByName("Hours"); //logged data for my records, separated into individual clients
var transLog = ss.getSheetByName("Trans Log"); // logged data minus "other" catagory 
var clientsSheet = ss.getSheetByName("Clients"); //sheet containing all clients names and the typical hours we meet
var lRow = logSheet.getLastRow();
var hRow = hourSheet.getLastRow();
var tRow = transLog.getLastRow();
var cRow = clientsSheet.getLastRow();
var cColumn = clientsSheet.getLastColumn();


var cData = clientsSheet.getRange(1, 1, cRow, cColumn).getValues();


//get list of clients from cell and split it into an array
var Client = logSheet.getRange(lRow, 2).getValue().replace(", ", ","); //remove all spaces after a comma
var client = Client.split(",");

//get "other" information and do the same
var Other = logSheet.getRange(lRow, 5).getValue().replace(", ", ",");
var other = Other.split(",");




//check the date and set to today if nothing else has been entered
var dcell = logSheet.getRange(lRow, 4).getValue();
var date = new Date()
if (dcell == "") {} else if (dcell == "Yesterday") {
  date = new Date(date.getTime() - 1 * (24 * 3600 * 1000));
} else {
  date = dcell
}

var date = Utilities.formatDate(date, "GMT-8", "MM/dd/yy"); //format date




//put each client on their own row
for (i = 0; i < client.length; ++i) {


  var cl = client[i]
  var hour = logSheet.getRange(lRow, 3).getValue(); //hours 

  if (hour == !"") {
    break;
  }
  for (j = 0; j < cData.length; ++j) {
    if (cData[j][0] == cl) {
      var hour = cData[j][1];

    }
  }
  Logger.log(date + " - " + cl + " - " + hour);
  hourSheet.appendRow([date,cl, hour]);
  transLog.appendRow([date, cl, hour]);
}


//put each client on their own row
for (i = 0; i < other.length; i++) {
  hourSheet.appendRow([date, other[i], getHour(client[i])]);
}


} //end of function
``

这是我一直在编写的用于自学 Java 和 Apps-script 的代码

【问题讨论】:

  • 外循环,循环通过client。您没有展示如何获得client。内部循环得到了时间:var hour = cData[j][1];,但是你用hour做什么呢?使用您的示例代码,我们必须假设原始数据已正确检索。但我们无法知道这一点。首先想到的是您的for 循环在处理最后一行之前结束,但是根据提供的信息,无法确定这一点。也许“硬代码”来测试数据:var cData = ['one','two','three','four'];
  • 感谢您的反馈。我试图保持简单,但我发现我遗漏了一些重要信息。我已将原始代码添加到帖子的底部。希望对您有所帮助...

标签: arrays loops multidimensional-array google-apps-script iteration


【解决方案1】:

是的,简单的错误。

并非每个名称之前的所有空格都从数组中删除,因此 if 语句会直接跳过它们,因为它们不是真正的匹配项

【讨论】:

    猜你喜欢
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2018-05-07
    • 2015-06-02
    • 1970-01-01
    相关资源
    最近更新 更多