2

我的公司最近切换到 Google Apps,我正试图将 Word 和 Excel 文档转换为 Google 文档。我对 Google App Script 完全陌生并且被卡住了。我一直在寻找几天没有找到我要找的东西。

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

例如,可选字段之一是客户网站地址,如果客户没有网站,我不希望网站出现在表单上。

如果有人对如何做到这一点有任何“傻瓜”说明,我将不胜感激。

谢谢。

这是我到目前为止所拥有的:

// 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。

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

再次感谢,丽莎

4

1 回答 1

0

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

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

此替代方法使用三元或条件运算,并且更紧凑:

...
// If website question was not answered, leave blank
var Website = (e.values[6] == 'website') ? '' : e.values[6];
...
于 2013-05-17T20:14:47.167 回答