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;
}
If there were no matches for the regular expression,
templateVars
will be null. You need to check for this before your loop.UPDATE:
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 differentThe difference is the
"
vs the“
One is vertical (works), the other is "italicised" (doesn't work)Someone who knows about how computers format data will be able to explain the importance of this, but simply it breaks the code.