gpt4 book ai didi

javascript - Google Apps 脚本 - TypeError : Cannot read property "1" from null

转载 作者:行者123 更新时间:2023-11-30 06:21:15 27 4
gpt4 key购买 nike

我整理了一个 Google Apps 脚本,从我的电子邮件中提取一些数据并将其插入到电子表格中。该脚本实际上运行良好并且可以正常插入数据,但我的日志中不断出现以下错误:

TypeError:无法从 null 中读取属性“1”。 (第 46 行,文件“代码”)

According to MDNexec 方法返回一个数组,其中“匹配的文本作为第一项,然后是每个匹配的包含捕获的文本的捕获括号的项目。”因为我想要正则表达式括号内的值,而不是整个匹配的文本,所以我使用的是 [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 在每个正则表达式之后都输出了很好的变量,就像我说的那样,数据正在使用上面的函数完美地输入到电子表格中,但我仍然每次都会收到错误getEmails() 函数运行的时间。

有什么想法吗?

最佳答案

很明显,其中一个正则表达式不匹配,当您尝试访问此类匹配项的第 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_accountmessage_datemessage_vendormessage_amount - 都被分配了一个空值字符串。即使正则表达式失败,并且没有为它们中的任何一个分配第 1 组值,当它们稍后在代码中被引用时,它们也不会在调用时导致错误,因为它们被分配了一些值。

请注意,由于您没有遍历匹配项,因此不需要 /g 修饰符。

关于javascript - Google Apps 脚本 - TypeError : Cannot read property "1" from null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52934813/

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