我尝试创建一个脚本,该脚本从谷歌文档中的电子表格中读取值(名称、日期等),并自定义特定表单的描述(文本内容)以发送给具有这些值的用户。但是 string.replace() 方法并没有像我期望的那样工作。它只是用任何东西代替钥匙。我错过了什么吗?我花了几个小时谷歌搜索没有答案。任何帮助将不胜感激。
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});
}