Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
143
rated 0 times [  150] [ 7]  / answers: 1 / hits: 5643  / 6 Years ago, mon, october 22, 2018, 12:00:00

I put together a Google Apps Script to pull some data from my emails and insert it into a spreadsheet. The script is actually running great and inserting the data fine, but I keep getting the following error in my log:



TypeError: Cannot read property 1 from null. (line 46, file Code)



According to MDN, the exec method returns an array with the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured. Since I want the value within the parenthesis of my regex, rather than the entire matched text, I'm using [1].



Here's the function in question:



function getEmails() {
var label = GmailApp.getUserLabelByName(CapitalOne Transaction);
var threads = label.getThreads(); // Get threads of label above
for (var i in threads) {
var messages = threads[i].getMessages();
for (var j in messages) {
if ( messages[j].isUnread() ) {
var emailBody = messages[j].getPlainBody();
Logger.log(Email body: + emailBody);

// Get account number
const regExpAcct = /Account ending in (d{4})/g;
var message_account = regExpAcct.exec(emailBody);
if(message_account){ Logger.log(Email message accnt: + message_account[1]); }

// Get date of transaction
var regExpDate = /we're notifying you that on (...+), at/g;
var message_date = regExpDate.exec(emailBody);
if(message_date){ Logger.log(Email message date: + message_date[1]); }

// Get vendor name
var regExpVendor = /, at (...+),/g;
var message_vendor = regExpVendor.exec(emailBody);
if(message_vendor){ Logger.log(Email message vendor: + message_vendor[1]); }

// Get transaction amount
const regExpAmount = /purchase in the amount of $(S+) was/g;
var message_amount = regExpAmount.exec(emailBody);
if(message_amount){ Logger.log(Email message amount: + message_amount[1]); }

addDataToSpreadsheet( message_date[1], message_account[1], message_vendor[1], message_amount[1] );
messages[j].markRead();
}
}
}
}


The line(46) in question is this one: addDataToSpreadsheet( message_date[1], message_account[1], message_vendor[1], message_amount[1] );



Which calls this function:



function addDataToSpreadsheet( date, account, vendor, amount ) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow( [date, account, vendor, amount] );
}


My Logger.logs after each regex all output the variables fine, and like I said, the data is being input into the spreadsheet perfectly with the function above, but I still get the error every time the getEmails() function runs.



Any ideas why?


More From » arrays

 Answers
0

It is clear that one of the regexps does not match and when you try accessing the Group 1 value of such a match, you get an exception. I suggest assigning those values to separate variables and use them later to pass to addDataToSpreadsheet function:



var emailBody = messages[j].getPlainBody();
Logger.log(Email body: + emailBody);
var message_account = , message_date = , message_vendor = , message_amount = ;

// Get account number
const regExpAcct = /Account ending in (d{4})/;
var message_account_m = regExpAcct.exec(emailBody);
if(message_account_m){ message_account = message_account_m[1]; Logger.log(Email message accnt: + message_account); }
// Get date of transaction
var regExpDate = /we're notifying you that on (...+), at/;
var message_date_m = regExpDate.exec(emailBody);
if(message_date_m){ message_date = message_date_m[1]; Logger.log(Email message date: + message_date); }

// Get vendor name
var regExpVendor = /, at (...+),/;
var message_vendor_m = regExpVendor.exec(emailBody);
if(message_vendor_m){ message_vendor = message_vendor_m[1]; Logger.log(Email message vendor: + message_vendor); }

// Get transaction amount
const regExpAmount = /purchase in the amount of $(S+) was/;
var message_amount_m = regExpAmount.exec(emailBody);
if(message_amount_m){ message_amount = message_amount_m[1]; Logger.log(Email message amount: + message_amount); }

addDataToSpreadsheet( message_date, message_account, message_vendor, message_amount);
messages[j].markRead();


Now, all the four variables - message_account, message_date, message_vendor and message_amount - are assigned an empty string. Even if a regex fails, and a Group 1 value is not assigned to any of them, when they are referred later in the code, they will cause no errors upon invoking as they are assigned some value.



Note that since you are not iterating over the matches, /g modifier is unnecessary.


[#10691] Friday, October 19, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quinn

Total Points: 160
Total Questions: 86
Total Answers: 101

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
quinn questions
Sat, Aug 3, 19, 00:00, 5 Years ago
Mon, Apr 22, 19, 00:00, 5 Years ago
Thu, Nov 22, 18, 00:00, 6 Years ago
Sat, Jun 23, 18, 00:00, 6 Years ago
;