Reputation: 27
I was trying to call Stripe API for some payment information. The code is as follow:
function callStripe() {
var ss = SpreadsheetApp.openById("XXX");
var sheet = ss.getSheetByName("XXX");
var lastRow = sheet.getLastRow();
var startRow = 1; // First row of data to process
var dataRange = sheet.getRange(startRow, 1, sheet.getLastRow(), sheet.getLastColumn());
var data = dataRange.getValues();
for (var i = 0; i < lastRow + 1; ++i) {
var row = data[i];
var missedFee = row[6];
if (missedFee == "") { //only call for missing figures
var txId = sheet.getRange(startRow + i, 2).getDisplayValue();
var url = "https://api.stripe.com/v1/balance_transactions/" + txId;
var params = {
method: "GET",
headers: {Authorization: "Basic " + Utilities.base64Encode("XXX:")}
};
var res = UrlFetchApp.fetch(url, params);
var transaction = JSON.parse(res);
var fee = transaction["fee"];
var net = transaction["net"];
sheet.getRange(startRow + i, 7).setValue(fee / 100);
sheet.getRange(startRow + i, 9).setValue(net / 100);}
}}
I can get what I need from the API after running the function, but there is an error as below:
TypeError: Cannot read property '6' of undefined (line 112, file "Code")
Line 112 is referring to
var missedFee = row[6];
May I know what has done wrong?
Upvotes: 0
Views: 162
Reputation: 26806
for (var i = 0; i < lastRow + 1; ++i)
Given that you start with 0
(which is correct since the first array entry is 0
), your last i
should be lastRow-1
Sample:
data[0]
, data[1]
and data[2]
i < lastRow + 1
as the condition to execute the loop, it will also execute for i = 3
data[3]
(that would be the forth row) and thus, there is no column 7 in row 4 (var missedFee = row[6];
)Upvotes: 1