我有一个谷歌应用程序脚本,它通过For循环从谷歌应用程序表中提取数据,然后将每一行放入一个阵列准备导出到外部API。我的问题是脚本跳过一些条目,我无法理解为什么。
以下是整个代码:
function prepEmails() {
var ss = SpreadsheetApp.getActive();
var s = ss.getSheetByName("Emails");
var sLastRow = s.getLastRow();
var scriptProperties = PropertiesService.getScriptProperties();
var logCount = scriptProperties.getProperty('EmailCount');
Logger.log("63 logCount = "+logCount);
var url = "https://apitoexportto.com";
var recordType = "email";
var logKey = "mailCount"
var activityDesc = "######"
if (logCount === null) {
scriptProperties.setProperty('EmailCount', 0);
logCount = 0;
}
Logger.log("68 logCount = "+logCount);
var msgs = sLastRow - logCount;
var arr = new Array();
for (var i=0; i<msgs-1; i++) {
Logger.log("78 inside FOR loop");
var nextRow = logCount+1; //+1 to skip header row
var msg = s.getRange(nextRow+1, 1, 1, 6).getValues(); //+1 to skip to next row
var payload = {
'type' : "TimeEntry",
'matter_id' : msg[0][0],
'price' : msg[0][1],
'date' : msg[0][2],
'note' : msg[0][3],
'quantity' : msg[0][4],
'activity_description_id': activityDesc
};
arr.push(payload);
var logCount = parseInt(logCount)+1;
}
Logger.log("95 logCount = "+logCount);
if (i>0) {
exportEmails(url, recordType, arr, logKey, activityDesc);
}
}
这是执行记录: 编辑:我还没有足够的代表发布图片,所以这里是成绩单的链接。 Execution Transcript我也会将其粘贴到下方。
[14-12-18 08:20:44:782 CST] Starting execution
[14-12-18 08:20:44:814 CST] SpreadsheetApp.getActive() [0 seconds]
[14-12-18 08:20:44:840 CST] Spreadsheet.getSheetByName([Emails]) [0.024 seconds]
[14-12-18 08:20:44:859 CST] Sheet.getLastRow() [0.019 seconds]
[14-12-18 08:20:44:861 CST] PropertiesService.getScriptProperties() [0 seconds]
[14-12-18 08:20:44:867 CST] (class).getProperty([EmailCount]) [0.004 seconds]
[14-12-18 08:20:44:868 CST] Logger.log([63 logCount = 98.0, []]) [0 seconds]
[14-12-18 08:20:44:868 CST] Logger.log([68 logCount = 98.0, []]) [0 seconds]
[14-12-18 08:20:44:869 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:870 CST] Sheet.getRange([98, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:913 CST] Range.getValues() [0.042 seconds]
[14-12-18 08:20:44:914 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:914 CST] Sheet.getRange([101, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:916 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:917 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:918 CST] Sheet.getRange([102, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:919 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:920 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:920 CST] Sheet.getRange([103, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:922 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:923 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:924 CST] Sheet.getRange([104, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:925 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:926 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:927 CST] Sheet.getRange([105, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:928 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:929 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:930 CST] Sheet.getRange([106, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:931 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:932 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:933 CST] Sheet.getRange([107, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:934 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:935 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:936 CST] Sheet.getRange([108, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:937 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:938 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:939 CST] Sheet.getRange([109, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:940 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:941 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:942 CST] Sheet.getRange([110, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:943 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:944 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:945 CST] Sheet.getRange([111, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:946 CST] Range.getValues() [0 seconds]
[14-12-18 08:20:44:947 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:948 CST] Sheet.getRange([112, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:949 CST] Range.getValues() [0.001 seconds]
[14-12-18 08:20:44:950 CST] Logger.log([78 inside FOR loop, []]) [0 seconds]
[14-12-18 08:20:44:951 CST] Sheet.getRange([113, 1, 1, 6]) [0 seconds]
[14-12-18 08:20:44:959 CST] Range.getValues() [0.008 seconds]
[14-12-18 08:20:44:960 CST] Logger.log([95 logCount = 112, []]) [0 seconds]
[14-12-18 08:20:44:961 CST] Logger.log([in exportEmails function, []]) [0 seconds]
正如您所看到的,它开始从第98行抓取值。这是基于logCount的预期(一个脚本属性跟踪导出的总数并根据该数字在适当的行中拾取)。但随后它跳过99和100并从第101行抓取。这些行中的数据没有什么异常。 (行不是空白或类似的任何奇怪)。我确认第99行和第100行的值未通过下一个函数导出。他们刚被跳过。
我对javascript相对较新(并且完全是自学成才的),但这对我来说似乎是一个简单的循环。没有continue
,没有break
,没有return
。我无法理解它会在哪里或为什么会像这样跳过。
有谁能告诉我为什么这些行会被跳过?
答案 0 :(得分:1)
我会改变这一行:
var nextRow = logCount+1; //+1 to skip header row
要:
nextRow = nextRow + 1; //+1 to skip header row
并将nextRow
初始值设置为For
循环:
var nextRow = logCount - 1; //initialize nextRow
for (var i=0; i<msgs-1; i++) {
Logger.log("78 inside FOR loop. Value of nextRow: " + nextRow);
nextRow = nextRow + 1; //+1 to skip header row
var msg = s.getRange(nextRow+1, 1, 1, 6).getValues(); //+1 to skip to next row
var payload = {
'type' : "TimeEntry",
'matter_id' : msg[0][0],
'price' : msg[0][1],
'date' : msg[0][2],
'note' : msg[0][3],
'quantity' : msg[0][4],
'activity_description_id': activityDesc
};
另请参阅我对Logger.log("78 inside FOR loop. Value of nextRow: " + nextRow);
声明所做的更改。