微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JavaScript,无法读取 null 的属性“map””,嵌套 ForEach?

如何解决JavaScript,无法读取 null 的属性“map””,嵌套 ForEach?

我有一个脚本(特别是谷歌应用程序脚本),其中包含 3 个函数:gather()、getGmail() 和 parseEmail() ...

    function gather() {
    let messages = getGmail();

    let curSheet = SpreadsheetApp.getActive();

    messages.forEach(message => {curSheet.appendRow(parseEmail(message))});
    }

function getGmail() {
const query = "label:MySummaryList NOT label:done";

let threads = GmailApp.search(query);

let label = GmailApp.getUserLabelByName("done");
if (!label) {label = GmailApp.createLabel("done")}

let messages = [];

threads.forEach(thread => {
    messages.push(thread.getMessages()[0].getPlainBody());
    label.addToThread(thread);
});

return messages;
}

    function parseEmail(message){
    let parsed = message.replace(/=\n/g,'') //replace =newline with nothing
                        .split(','); //split on commas
    let result = parsed;

    return result;
    }

这 3 个函数非常适合将 1 个 gmail 电子邮件解析为 1 个电子表格行。问题是我试图解析的 gmail 是一个需要生成多个电子表格行的摘要

我有这个匹配代码我想添加函数 parseEmail() ...

//.split(','); //split on commas   //commented out with inclusion of match code
.match(/5px">(.*?)<\/b>/g).map(function(val){return val.replace(/<\/?b>/g,'').replace(/5px">/g,'').trim();});

... 成功匹配 Gmail 邮件文本...

5px">First text in array </b> random text 5px"> Second text in array </b>

... 并返回一个这样的数组...

[“数组中的第一个文本”,“数组中的第二个文本”]

所需的结果是这样的电子表格行... 第 1 行 A 列:数组中的第一个文本 第 2 行 A 列:数组中的第二个文本 ... 对数组中捕获的所有匹配项依此类推

当我将匹配代码添加函数 parseEmail() 时,我收到错误消息“TypeError:无法读取 null 的属性 'map'”。我假设函数 gather() 期望单个拆分字符串附加 1 个电子表格行,现在我传入一个由匹配代码产生的数组。我是否需要在函数 gather() 中使用嵌套的 forEach 来循环匹配数组?还是我的方法完全有缺陷?感谢您考虑我的问题!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。