1

私はJavaScriptを初めて使用し、Googleスプレッドシートとメールのメールマージ機能を試してみました。チュートリアルスクリプトをコピーして、いくつかの必要な変更を加えました(少なくとも私が考えることができるもの)。しかし、スクリプトを実行しようとすると、TypeErrorが発生しました。nullからプロパティ「length」を読み取れません。(43行目)

上記の43行目は、以下のforループです。スクリプトを実行できるように、修正すべき点を教えてもらえますか?

// Replaces markers in a template string with values define in a JavaScript data object.
// Arguments:
//   - template: string containing markers, for instance ${"Column name"}
//   - data: JavaScript object with values to that will replace markers. For instance
//     data.columnName will replace marker ${"Column name"}
// Returns a string without markers. If no data is found to replace a marker, it is
// simply removed.
function fillInTemplateFromObject(template, data) {
    var email = template;
    // Search for all the variables to be replaced, for instance ${"Column name"}
    var templateVars = template.match(/\$\{\"[^\"]+\"\}/g);
    // Replace variables from the template with the actual values from the data object.
    // If no value is available, replace with the empty string.
    for (var i = 0; i < templateVars.length; ++i) {
        // normalizeHeader ignores ${"} so we can call it directly here.
        var variableData = data[normalizeHeader(templateVars[i])];
        email = email.replace(templateVars[i], variableData || "");
    }
    return email;
}
4

2 に答える 2

2

正規表現に一致するものがない場合は、templateVarsnull になります。ループの前にこれを確認する必要があります。

アップデート:

if (templateVars !== null) {
    for (var i = 0; i < templateVars.length; i++) {
        ...
    }
}
于 2012-10-18T07:58:24.340 に答える