如果Google表单上的字段留空,如何创建一个不打印某些文本的套用信函

时间:2012-12-13 17:51:12

标签: google-apps-script

我的公司最近切换到Google Apps,我正在尝试将Word和Excel文档转换为Google文档。我是谷歌应用程序脚本的新手,我被困住了。我一直在寻找一个没找到我要找的东西。

我有一个Google表单,通过电子邮件发送给销售人员。完成后,Google电子表格会更新,然后向销售人员发送一份表格信函,总结他们的输入。表单上的某些字段是可选的。如果问题未得到回答,如何创建不包含某些套用信函文本的模板?

例如,其中一个可选字段用于客户网站地址,如果客户没有网站,我不希望单词网站显示在表单上。

如果有人有关于如何做到这一点的“For Dummies”说明,我将非常感激。

感谢。

这是我到目前为止所做的:

// Business Review Report
// Get template from Google Docs and name it
var docTemplate = "1gRk2irahyW2Sf4pQMTDykXlMQqYgbe1Ba1vkh20KDYo";
var docName = "Business Review";

// When Form Gets submitted
function onFormSubmit (e) {
//Get information from form and set as variables
var variablename = "static entry or form value"
var email_address = e.values[1];
var Business_Name = e.values[2];
var Business_Address = e.values[3];
var Phone_Number = e.values[4]
var Email_address = e.values[5];
var Website = e.values[6];
var Participants = e.values[7];
var Agenda = e.values[8];
var Updates_and_Progress1 = e.values[9]; 
var Updates_and_Progress2 = e.values[10];
var Updates_and_Progress3 = e.values[11]; 
var Current_Issues1 = e.values[12]; 
var Current_Issues2 = e.values[13]; 
var Current_Issues3 = e.values[14]; 
var Next_Steps = e.values[15]; 

// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(docName+' for '+ Business_Name)
.getId();

// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();

// Replace place holder keys,in our google doc template
copyBody.replaceText('keyBusinessName', Business_Name);
copyBody.replaceText('keyBusinessAddress', Business_Address);
copyBody.replaceText('keyPhoneNumber', Phone_Number);
copyBody.replaceText('keyEmailaddress', email_address);
copyBody.replaceText('keywebsite', Website);
copyBody.replaceText('keyParticipants', Participants);
copyBody.replaceText('keyAgenda', Agenda);
copyBody.replaceText('keyUpdatesAndProjects1', Updates_and_Progress1);
copyBody.replaceText('keyUpdatesAndProjects2', Updates_and_Progress2);
copyBody.replaceText('keyUpdatesAndProjects3', Updates_and_Progress3);
copyBody.replaceText('keyCurrentIssues1',Current_Issues1);
copyBody.replaceText('keyCurrentIssues2', Current_Issues2);
copyBody.replaceText('keyCurrentIssues3', Current_Issues3);
copyBody.replaceText('keyNextSteps', Next_Steps);

// Save and close the temporary document
copyDoc.saveAndClose();

// Convert temporary document to PDF
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");

// Attach PDF and send the email
var subject = "Business Review";
var body = "Business Review for " + Business_Name + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});

// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);

}

它有效,但生成的PDF还包含未填充字段的文本,如CurrentIssues2。

我不确定我是否清楚地解释了这个,所以请回答任何问题,我会尽量更清楚。

再次感谢, 莉莎

1 个答案:

答案 0 :(得分:0)

您的表单问题显然具有您在表单信函中不需要的默认值。您可以检查这些默认值并将其替换为更合适的内容,例如空白字符串。

...
// If website question was not answered, leave blank
var Website = e.values[6];
if (Website == 'website') Website = '';
...

此替代方案使用三元组或Conditional Operation,并且更紧凑:

...
// If website question was not answered, leave blank
var Website = (e.values[6] == 'website') ? '' : e.values[6];
...