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 (将#修改为@)