我整理了一个Google Apps脚本,以从电子邮件中提取一些数据并将其插入电子表格中。该脚本实际上运行良好,并且可以很好地插入数据,但是我在日志中始终收到以下错误:
TypeError: Cannot read property "1" from null. (line 46, file "Code")
According to MDN,exec
方法返回一个数组,其中“匹配的文本为第一项,然后每个匹配的捕获括号中的一个包含捕获的文本的项为一个”。由于我希望值放在正则表达式的括号内,而不是整个匹配的文本中,因此我使用的是[1]
。
这是有问题的功能:
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();
}
}
}
}
有问题的第46行是这样的:addDataToSpreadsheet( message_date[1], message_account[1], message_vendor[1], message_amount[1] );
哪个调用此函数:
function addDataToSpreadsheet( date, account, vendor, amount ) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow( [date, account, vendor, amount] );
}
在每个正则表达式之后,我的Logger.log
都很好地输出了变量,就像我说的那样,数据正通过上述函数完美地输入到电子表格中,但是每次{{1 }}函数运行。
有什么想法吗?
答案 0 :(得分:0)
很明显,其中一个正则表达式不匹配,并且当您尝试访问此类匹配的组1值时,会遇到异常。我建议将这些值分配给单独的变量,并在以后使用它们传递给addDataToSpreadsheet
函数:
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();
现在,所有四个变量-message_account
,message_date
,message_vendor
和message_amount
-被分配了一个空字符串。即使正则表达式失败,并且没有为其中的任何一个分配Group 1值,当在代码中的后面引用它们时,它们也不会因调用而分配任何值,因此不会导致错误。
请注意,由于您没有迭代匹配项,因此/g
修饰符是不必要的。