Google表单文档自动化

时间:2016-01-31 19:45:07

标签: google-apps-script google-form

我在谷歌表单提交时收到了两个主要问题 - 第一个是我设置的e.values无法读取,第二个是没有定义Doclist。我已经为e.values设置了触发器,并尝试使用e.namedvalues。我想通过向表单提交者发送文档而不是pdf来消除第二个问题,但最重要的是我首先解决了第一个问题。

我正在使用我已修改的框架以适合我自己的文档,因此这可能是我遇到问题的地方。

// G. Hopper
// Subcontract Generator
// Get template from Google00 Docs and name it
var docTemplate = "16bdl2myh7f0ywlxmf3iimq4Nz6-j8VTs7hkMbshRjp0"; 
var docName = "Subcontract";

// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var sub_name = e.namedValues["Subcontractor Name"].toString();
var sub_address = e.namedValues["Subcontractor address"].toString();
var proj_name = e.namedValues["Project Name"].toString();
var own_name = e.namedValues["Owner Name"].toString();
var own_address = e.namedValues["Owner Address"].toString();
var designer = e.namedValues["Design Firm"].toString();
var design_address = e.namedValues["Design Firm Address"].toString();
var proj_address = e.namedValues["Project Address"].toString();
var scope = e.namedValues["Scope of work"].toString();
var date_bid = e.namedValues["Date on bid"].toString();
var sub_value = e.namedValues["Subcontract Value"].toString();
var date_pay = e.namedValues["Date of the month pay applications are due"].toString();
var email = e.namedValues["Your email address"].toString();
var day = e.namedValues["Day"].toString();
var month_year = e.namedValues["Month and Year"].toString();

// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy('Subcontract for '+sub_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('keySubName', sub_name);
copyBody.replaceText('keySubAddress', sub_address);
copyBody.replaceText('keyProject', proj_name);
copyBody.replaceText('keyOwner', own_name);
copyBody.replaceText('keyOwnAddress', own_address);
copyBody.replaceText('keyDesigner',designer);
copyBody.replaceText('keyDesignAddress', design_address);
copyBody.replaceText('keyProjAdd', proj_address);
copyBody.replaceText('keyScope', scope);
copyBody.replaceText('keyBidDate', date_bid);
copyBody.replaceText('KeySubValue', sub_value);
copyBody.replaceText('keyPayApp', date_pay);
copyBody.replaceText('keyDay', day);
copyBody.replaceText('keyMonthDay', month_year);

// 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 = 'Subcontract'
var body = "Here is your subcontract for " + sub_name + "";
MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});

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

0 个答案:

没有答案