admin管理员组

文章数量:1022935

I'm very new with javascript and trying to play around with Mail Merge function in Google spreadsheet and mail. I copied the tutorial script and made some necessary changes (at least what I can think of). But when I tried to run the script, I got TypeError: Cannot read property "length" from null. (line 43)

The line 43 mentioned above is the for loop below. Can someone please help let me know what to be fixed so I can run the script?

// 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;
}

I'm very new with javascript and trying to play around with Mail Merge function in Google spreadsheet and mail. I copied the tutorial script and made some necessary changes (at least what I can think of). But when I tried to run the script, I got TypeError: Cannot read property "length" from null. (line 43)

The line 43 mentioned above is the for loop below. Can someone please help let me know what to be fixed so I can run the script?

// 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;
}
Share Improve this question edited Oct 18, 2012 at 7:53 Torsten Robitzki 2,5541 gold badge22 silver badges36 bronze badges asked Oct 18, 2012 at 7:47 user1755506user1755506 111 gold badge1 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

If there were no matches for the regular expression, templateVars will be null. You need to check for this before your loop.

UPDATE:

if (templateVars !== null) {
    for (var i = 0; i < templateVars.length; i++) {
        ...
    }
}

I just had this same issue but the issue the OP is having I don't think is one that relates to the code.

It is the formatting of the email template offered by Google in their tutorial.

The placeholder is ${"First Name"} however depending on how you edit these you can get ${“First Name”} which is quite different

The difference is the " vs the One is vertical (works), the other is "italicised" (doesn't work)

Someone who knows about how puters format data will be able to explain the importance of this, but simply it breaks the code.

I'm very new with javascript and trying to play around with Mail Merge function in Google spreadsheet and mail. I copied the tutorial script and made some necessary changes (at least what I can think of). But when I tried to run the script, I got TypeError: Cannot read property "length" from null. (line 43)

The line 43 mentioned above is the for loop below. Can someone please help let me know what to be fixed so I can run the script?

// 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;
}

I'm very new with javascript and trying to play around with Mail Merge function in Google spreadsheet and mail. I copied the tutorial script and made some necessary changes (at least what I can think of). But when I tried to run the script, I got TypeError: Cannot read property "length" from null. (line 43)

The line 43 mentioned above is the for loop below. Can someone please help let me know what to be fixed so I can run the script?

// 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;
}
Share Improve this question edited Oct 18, 2012 at 7:53 Torsten Robitzki 2,5541 gold badge22 silver badges36 bronze badges asked Oct 18, 2012 at 7:47 user1755506user1755506 111 gold badge1 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

If there were no matches for the regular expression, templateVars will be null. You need to check for this before your loop.

UPDATE:

if (templateVars !== null) {
    for (var i = 0; i < templateVars.length; i++) {
        ...
    }
}

I just had this same issue but the issue the OP is having I don't think is one that relates to the code.

It is the formatting of the email template offered by Google in their tutorial.

The placeholder is ${"First Name"} however depending on how you edit these you can get ${“First Name”} which is quite different

The difference is the " vs the One is vertical (works), the other is "italicised" (doesn't work)

Someone who knows about how puters format data will be able to explain the importance of this, but simply it breaks the code.

本文标签: javascriptTypeError Cannot read property quotlengthquot from nullStack Overflow