【问题标题】:Type Error: Cannot read property "length" from undefined in Google Apps Script类型错误:无法从 Google Apps 脚本中的未定义中读取属性“长度”
【发布时间】:2015-01-06 01:33:55
【问题描述】:

我正在尝试使用 @serge 的 script 将电子邮件提取到电子表格中。

我收到一个类型错误:“无法从未定义中读取属性“长度””。

任何解决问题的帮助将不胜感激。

function getMessagesWithLabel() {
    var destArray = new Array();
    var threads = GmailApp.getUserLabelByName('CDC Health Alerts').getThreads(1,30);

    for(var n in threads){
        var msg = threads[n].getMessages();
        var destArrayRow = new Array();
        destArrayRow.push('thread has '+threads[n].getMessageCount()+' messages');
        for(var m in msg){
            destArrayRow.push(msg[m].getSubject());
        }
        destArray.push(destArrayRow);
    }
    Logger.log(destArray);
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sh = ss.getActiveSheet();
    if(ss.getLastRow()==0){
        sh.getRange(1,1).setValue('getMessagesWithLabel() RESULTS')
    };
    sh.getRange(ss.getLastRow()+1,1,destArray.length,destArray[0].length).setValues(destArray)
}

【问题讨论】:

  • 请检查异常堆栈跟踪中发生错误的行,并明确您的上述代码 sn-p 这是哪一行

标签: google-apps-script typeerror


【解决方案1】:

length() 属性用于的唯一行是最后一行。

sh.getRange(ss.getLastRow()+1,1,destArray.length,destArray[0].length).setValues(destArray)

所以名为 destArray 的数组没有将任何数据推入其中。如果您检查destArray 中的值,它将显示值undefined。您需要从代码的顶部开始,并检查分配给变量的关键点。例如:

var threads = GmailApp.getUserLabelByName('CDC Health Alerts').getThreads(1,30);
Logger.log('threads: ' + threads);

注意上面代码中的 Logger.log() 语句。添加Logger.log('Some Text: ' + variableName); 语句,然后查看日志,查看变量“threads”是否获得了分配给它的所需内容。

继续这样做,直到发现问题为止。尽管错误来自最后一行,但代码中仍有错误,因为变量 destArray 没有获得赋值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2018-04-25
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 2013-11-24
    相关资源
    最近更新 更多