Reputation: 5
I tried to create a script which reads values (name, date, etc) from a spreadsheet in google docs and customizes a specific form's description (text content) to be sent to users with those values. But the string.replace() method isn't working like I expect it to. It just replaces the key with nothing. Am I missing something? I've spent hours googling with no answers. Any help would be greatly appreciated.
var docId = ScriptProperties.getProperty("QuestionnaireDocId");
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1000000; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var name = row[0]; // this var isn't empty. - tested it to check
var formSpreadsheet = SpreadsheetApp.openById(docId);
var sFormUrl = formSpreadsheet.getFormUrl();
if(sFormUrl) {
var response = UrlFetchApp.fetch(sFormUrl);
var sFormContent = response.getContentText();
//Replace <keyName> in the form description with the value of var 'name'
sFormContent = sFormContent.replace("<keyName>", name);
//get only body content
sFormContent = sFormContent.replace(/[\s\S]+<body[^>]*>([\s\S]+)<\/body>[\s\S]+/i, '$1');
//remove script
sFormContent = sFormContent.replace(/<script[^>]*>[\s\S]*<\/script>/i, '');
//add inline style for form elements, since gmail does not allow style tag and link tag to style it
sFormContent = sFormContent.replace(/class\=\"errorbox-good\"/gi, 'class="errorbox-good" style="margin-bottom:1.5em;zoom:1"');
sFormContent = sFormContent.replace(/class\=\"ss-q-title\"/gi, 'class="ss-q-title" style="display:block;font-weight:bold;"');
sFormContent = sFormContent.replace(/class\=\"ss-q-help\"/gi, 'class="ss-q-help" style="display:block;color:#666;margin:.1em 0 .25em 0"');
sFormContent = sFormContent.replace(/class\=\"ss-choices\"/gi, 'class="ss-choices" style="list-style:none;margin:.5em 0 0 0;padding:0;"');
}
var mailBody2;
if(sFormUrl) {
mailBody2 = '<br /><br />If you have trouble viewing or submitting this form, you can fill it out online:<br />';
mailBody2 += '<a href="' + sFormUrl + '" title="">' + sFormUrl + '</a>';
mailBody2 += '</body></html>';
mailBody2 += '<br /><br />';
mailBody2 += sFormContent;
mailBody2 += '<br />';
}
// Embed form and send the email - the emails work fine
var subject = "Test email";
MailApp.sendEmail(email_address, subject, mailBody2, {htmlBody: mailBody2});
}
Upvotes: 0
Views: 7581
Reputation: 1809
It looks like there are a couple things going on that cause this not to work as written. First, in the form content that you get back from the UrlFetchApp call, there are two places where the form description shows up. One looks like this. I just put foo in the description field of my form:
<meta itemprop="description" content="foo <keyName>">
and the other looks like this:
<div class="ss-form-desc ss-no-ignore-whitespace">foo </div>
The second instance is the one that's actually displayed in your emails, and as you can see, it seems that the is being stripped out already. My guess is that it's stripping out anything that looks like it might be HTML.
So here's what I tried, which worked for me. In your form description, put [keyName] instead of .
Then, you also need to search and replace globally, not just the first instance of [keyName], to make sure you get it in both places. So change this line:
sFormContent = sFormContent.replace("<keyName>", name);
to this:
sFormContent = sFormContent.replace(/\[keyName\]/g, name);
Once I made those changes, I was able to get it working, where the emails I received contained the values from my spreadsheet in the description, after the word 'foo'.
Upvotes: 1