0

我收到以下错误:“TypeError:无法从 null 读取属性“0.0”。(line227)”

我一直在查看这里的文档:https ://developers.google.com/apps-script/articles/gmail-stats#section1 但无法排除故障。

Line227: variables.dayOfEmailsReceived[day]++;

抱歉,stackoverflow 的新手,但非常感谢任何帮助。

参考:

var BATCH_SIZE = 50;
var ss = SpreadsheetApp.getActiveSpreadsheet();
// If you are using several email addresses, list them in the following variable 
// - eg 'romain.vialard@gmail.com,romain.vialard@example.com'
var aliases =  'romain.vialard@euromed-management.com';

function activityReport() {
var status = ScriptProperties.getProperty("status");
if (status == null) {
    // If the script is triggered for the first time, init
    init_();
}
else {
    status = Utilities.jsonParse(status);
    var previousMonth = new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1).getMonth();
    if (status == null || (status.customReport == false && status.previousMonth != previousMonth)) {
        init_();
        fetchEmails_(status.customReport);
    }
    // If report not sent, continue to work on the report
    else if (status.reportSent == "no") {
        fetchEmails_(status.customReport);
    }
}
}

function fetchEmails_(customReport) {
var variables = Utilities.jsonParse(ScriptProperties.getProperty("variables"));
if (!customReport) {
    var query = "after:" + variables.year + "/" + (variables.previousMonth) + "/31";
    query += " before:" + new Date().getYear() + "/" + (variables.previousMonth + 1) + "/31";
}
else {
    var query = "after:" + Utilities.formatDate(new Date(variables.startDate), variables.userTimeZone, 'yyyy/MM/dd');
    query += " before:" + Utilities.formatDate(new Date(variables.endDate), variables.userTimeZone, 'yyyy/MM/dd');
}
query += " in:anywhere -label:sms -label:call-log -label:chats -label:spam -filename:ics";
query += " -from:maestro.bounces.google.com -from:unified-notifications.bounces.google.com -from:docs.google.com";
query += " -from:group.calendar.google.com -from:apps-scripts-notifications@google.com";
query += " -from:sites.bounces.google.com -from:noreply -from:notify -from:notification";
var startDate = new Date(variables.startDate).getTime();
var endDate = new Date(variables.endDate).getTime();
var conversations = GmailApp.search(query, variables.range, BATCH_SIZE);
variables.nbrOfConversations += conversations.length;
var sheets = ss.getSheets();
var people = sheets[0].getDataRange().getValues();
var record = [];
for (var i = 0; i < conversations.length; i++) {
    Utilities.sleep(1000);
    var conversationId = conversations[i].getId();
    var firstMessageSubject = conversations[i].getFirstMessageSubject();
    var starred = false;
    if (conversations[i].hasStarredMessages()) {
        variables.nbrOfConversationsStarred++;
        starred = true;
    }
    var important = false;
    if (conversations[i].isImportant()) {
        variables.nbrOfConversationsMarkedAsImportant++;
        important = true;
    }
    var location = "";
    var labels = conversations[i].getLabels();
    var nbrOfLabels = labels.length;
    if (nbrOfLabels == 0) {
        if (conversations[i].isInInbox()) {
            variables.nbrOfConversationsInInbox++;
            location += "Inbox,";
        }
        else if (conversations[i].isInTrash()) {
            variables.nbrOfConversationsInTrash++;
            location += "Trashed,";
        }
        else {
            variables.nbrOfConversationsArchived++;
            location = "Archived";
        }
    }
    else {
        variables.nbrOfConversationsInLabels++;
        for (var j = 0; j < nbrOfLabels; j++) {
            location += labels[j].getName() + ",";
        }
    }
    var youReplied = false;
    var youStartedTheConversation = false;
    var someoneAnswered = false;
    var messages = conversations[i].getMessages();
    var nbrOfMessages = messages.length;
    variables.nbrOfEmailsPerConversation[nbrOfMessages]++;
    for (var j = 0; j < 10; j++) {
        if (variables.topThreads[j][1] < nbrOfMessages) {
            variables.topThreads.splice(j, 0, [firstMessageSubject, nbrOfMessages]);
            variables.topThreads.pop();
            j = 10;
        }
    }
    var timeOfFirstMessage = 0;
    var waitingTime = 0;
    for (var j = 0; j < nbrOfMessages; j++) {
        var process = true;
        var date = messages[j].getDate();
        var month = date.getMonth();
        if (customReport) {
            if (date.getTime() < startDate || date.getTime() > endDate) {
                process = false;
            }
        }
        else {
            if (month != variables.previousMonth) {
                process = false;
            }
        }
        if (process) {
            //////////////////////////////////
            // Fetch sender of each emails
            //////////////////////////////////
            var from = messages[j].getFrom().replace(/"[^"]*"/g,'');
            if (from.match(/</) != null) {
                from = from.match(/<([^>]*)/)[1];
            }
            var time = Utilities.formatDate(date, variables.userTimeZone, "H");
            var day = Utilities.formatDate(date, variables.userTimeZone, "d") - 1;

            // Use function from Utilities file
            variables = countSendsPerDaysOfWeek_(variables, date, from);
            var body = messages[j].getBody();
            // Use function from Utilities file
            var resultsFromCalcMessagesLength = calcMessagesLength_(variables, body, from);
            variables = resultsFromCalcMessagesLength[0];
            var messageLength = resultsFromCalcMessagesLength[1];
            var cc = messages[j].getCc().replace(/"[^"]*"/g,'').split(/,/);
            for (var k = 0; k < cc.length; k++) {
                if (cc[k].match(/</) != null) {
                    cc[k] = cc[k].match(/<([^>]*)/)[1];
                }
            }
            var reg = new RegExp(from, 'i');
            if ((variables.user + aliases).search(reg) != -1) {
                if (j == 0) {
                    youStartedTheConversation = true;
                    timeOfFirstMessage = date.getTime();
                }
                if (j > 0 && !youStartedTheConversation) {
                    if (!youReplied) {
                        youReplied = true;
                        // Use function from Utilities file
                        variables = calcWaitingTime_(variables, date, timeOfFirstMessage, youStartedTheConversation);
                    }
                }
                variables.nbrOfEmailsSent++;
                variables.timeOfEmailsSent[time]++;
                variables.dayOfEmailsSent[day]++;
                if (customReport) {
                    variables.monthOfEmailsSent[month]++;
                }
                var sharedWithTheOutsideWorld = false;
                var to = messages[j].getTo().replace(/"[^"]*"/g,'').split(/,/);
                for (var k = 0; k < to.length; k++) {
                    if (to[k].match(/</) != null) {
                        to[k] = to[k].match(/<([^>]*)/)[1];
                    }
                    if (to[k].search(variables.companyname) == -1){
                      sharedWithTheOutsideWorld = true;
                    }
                    var found = false;
                    for (var l = 0; l < people.length; l++) {
                        if (to[k] == people[l][0]) {
                            people[l][2]++;
                            found = true;
                        }
                    }
                    if (!found) {
                        people.push([to[k], 0, 1]);
                    }
                }
                if(!sharedWithTheOutsideWorld){
                  variables.sharedInternally++;
                }
            }
            else {
                if (j == 0) {
                    timeOfFirstMessage = date.getTime();
                }
                else if (youStartedTheConversation && !someoneAnswered) {
                    someoneAnswered = true;
                    // Use function from Utilities file
                    variables = calcWaitingTime_(variables, date, timeOfFirstMessage, youStartedTheConversation);
                }
                var found = false;
                for (var k = 0; k < people.length; k++) {
                    if (from == people[k][0]) {
                        people[k][1]++;
                        found = true;
                    }
                }
                if (!found) {
                    people.push([from, 1, 0]);
                }
                var to = messages[j].getTo().replace(/"[^"]*"/g,'');
                var checkSendToYou = false;
                var aliasesTemp = new Array(variables.user).concat(aliases.split(','));
                for(var k = 0; k < aliasesTemp.length; k++){
                    if(aliasesTemp[k] != ''){
                        var reg = new RegExp(aliasesTemp[k], 'i');
                        if (to.search(reg) != -1) {
                            checkSendToYou = true;
                        }
                    }
                }
                if(checkSendToYou)variables.sentDirectlyToYou++;
                var sharedWithTheOutsideWorld = false;
                to = to.split(/,/);
                for (var k = 0; k < to.length; k++) {
                    if (to[k].match(/</) != null) {
                        to[k] = to[k].match(/<([^>]*)/)[1];
                    }
                    if (to[k].search(variables.companyname) == -1){
                      sharedWithTheOutsideWorld = true;
                    }
                }
                if(sharedWithTheOutsideWorld == false && from.search(variables.companyname) != -1){
                  variables.sharedInternally++;
                }
                variables.nbrOfEmailsReceived++;
                variables.timeOfEmailsReceived[time]++;
                variables.dayOfEmailsReceived[day]++;
                if (customReport) {
                    variables.monthOfEmailsReceived[month]++;
                }
            }
            if (to != null) {
                to = to.toString();
            }
            if (cc != null) {
                cc = cc.toString();
            }
            var dayOfWeek = Utilities.formatDate(date, variables.userTimeZone, "EEEE");
            record.push([date, dayOfWeek, firstMessageSubject, from, to, cc, messageLength, location]);
        }
    }
    if (youStartedTheConversation) {
        variables.nbrOfConversationsStartedByYou++;
    }
    if (youReplied) {
        variables.nbrOfConversationsYouveRepliedTo++;
    }
}
variables.range += BATCH_SIZE;
ScriptProperties.setProperty("variables", Utilities.jsonStringify(variables));
sheets[0].getRange(1, 1, people.length, 3).setValues(people);
if (record[0] != undefined && sheets[1].getMaxRows() < 38000) {
    sheets[1].getRange(sheets[1].getLastRow() + 1, 1, record.length, record[0].length).setValues(record);
}
if (conversations.length < BATCH_SIZE) {
    sendReport_(variables);
}
}
4

2 回答 2

2

这是时区问题。转到您的 Google Drive 帐户上的 Gmail Meter 文件,然后:

文件 -> 电子表格设置...

将时区更改为您当前的时区。我假设您的 Gmail 帐户会自动更改时区,而不是您的电子表格。

于 2012-12-05T00:36:41.973 回答
0

错误表示和 day 为 0 变量。dayOfEmailsReceived 为空。不能引用 null 的索引。variables.dayOfEmailsReceived 是在 init_() 函数中创建的,因此请检查您是否定义了该函数并正确使用它。

于 2012-05-11T14:02:30.863 回答