它给了我错误“无法从未定义中读取 属性”

it gives me the error" can not read property from undefined"

我为 google 应用程序脚本编写了一个脚本,用于从 googlesheet 的电子邮件列表中读取并向每个电子邮件发送电子邮件。但是当我 运行 脚本时,它给了我错误:

TypeError: Cannot read property 'charAt' of undefined capitalizeFirstLetter @ Code.gs:5

这是我的脚本:

// It will only works on sheets with below schema:
// A      B          C          D         E         F           G
// ID   Email   First Name  Last Name   Status  Email Body  Email Subject

// DO NOT change any line of this code
// DO NOT change schema of your sheet or swap columns with each other (otherwise code may not work properly)
// Write your Email Body in "F2" cell and your Subject in "G2" cell
// To use rich text editor, use https://onlinehtmleditor.dev/ , then copy & paste ""HTML source code"" in "F2" cell

var EMAIL_SENT = 'SENT';
var EMAIL_ERROR = 'ERROR';

function capitalizeFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

function sendEmails() {
    var emailQuotaRemaining = MailApp.getRemainingDailyQuota() - 1; // leave 1 for being sure
    Logger.log("Number of emails you can send in this run:" + emailQuotaRemaining);
    var sheet = SpreadsheetApp.getActiveSheet();
    var message = sheet.getRange(2, 6).getValue();
    var subject = sheet.getRange(2, 7).getValue();
    var startRow = 2;
    var numRows = sheet.getLastRow() - 1; // Number of rows to process
    var dataRange = sheet.getRange(startRow, 1, numRows, 5);
    var data = dataRange.getValues();
    var sentCount = 0;
    for (var i = 0; i < data.length; ++i) {
        var row = data[i];
        var emailAddress = row[1];
        var firstName = row[2].toString().trim();
        var status = row[4];
        message = "<p>Dear " + capitalizeFirstLetter(firstName) + "</p>" + message;
        if (status !== EMAIL_SENT && MailApp.getRemainingDailyQuota() > 1) { // Prevents sending duplicates AND check for remaining daily quotes
            var isFailed = false;
            try {
                MailApp.sendEmail({
                    to: emailAddress,
                    subject: subject,
                    htmlBody: message
                });
            } catch (error) {
                Logger.log(error);
                isFailed = true;
            }
            if (isFailed) {
                sheet.getRange(startRow + i, 5).setValue(EMAIL_ERROR);
                Logger.log(`something went wrong for sending to: ${emailAddress}`);
            }
            else {
                sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT);
                Logger.log(`successfully sent to: ${emailAddress}`);
                sentCount += 1;
            }

            // Make sure the cell is updated right away in case the script is interrupted
            SpreadsheetApp.flush();
        }
    }
    Logger.log(`${sentCount} emails has been sent successfully!`);
}

这是link到googlesheet的一个例子:

https://docs.google.com/spreadsheets/d/1edVlVYuxMv_zs4zahYELL5SeT4ONW_r1RjXmXzaWLfg/edit?usp=sharing

我是新手。任何帮助将不胜感激。

解释:

您的代码应该可以正常工作(至少带有字符串的部分)。

问题是您没有执行正确的功能。

默认情况下 google 脚本执行脚本中定义的 第一个函数 ,即 capitalizeFirstLetter。后者不能独立工作,因为它需要变量 string 的值,因此会出现未定义的错误。

您的目标是执行 sendEmails 因为这是 feeds/uses capitalizeFirstLetter.

的主要功能

解决方案:

Select sendEmails 从脚本编辑器执行该函数: