gpt4 book ai didi

javascript - For 循环跳过条目

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:31:32 25 4
gpt4 key购买 nike

我有一个 google apps 脚本,它通过 For 循环从 google apps 工作表中提取数据,然后将每一行放入一个数组中,以准备导出到外部 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 比较陌生(并且完全是自学的),但这对我来说似乎是一个简单的 for 循环。没有continue,没有break,没有return。我不明白它会在哪里或为什么会像这样跳过。

谁能告诉我为什么这些行会被跳过?

最佳答案

我会改变这一行:

var nextRow = logCount+1; //+1 to skip header row

收件人:

nextRow = nextRow + 1; //+1 to skip header row

并在 For 循环之上给 nextRow 一个初始值:

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); 语句所做的更改。

关于javascript - For 循环跳过条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27549555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com