Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*This is the document template I created*/
- var docTemplate = "1yw00mmVcRB8kEFYz4tyAcD1FiNx1tjKfMYrikMSuOHg"
- /*Document name for naming the PDF at the end*/
- var docName = "Fault Report";
- /*The email address the forms get emailed to*/
- var email_address = "example@example.com";
- /*Function grabs pdf file from ID passed from onFormSubmit line 18*/
- function getpdfFile(copyId) {
- var pdfFile = "No Content";
- pdfFile = DocsList.getFileById(copyId);
- return pdfFile
- }
- /*Download pdf file from copyId, runs at end of onFormSubmit*/
- function doGet(copyId) {
- return ContentService.createTextOutput().append(getpdfFile(copyId)).downloadAsFile('Fault report for '+company);
- }
- /*This function is linked as a trigger from the form page - onFormSubmit.*/
- function onFormSubmit(e) {
- /*I set the variable branch to the first value in the form - in this case, branch*/
- var branch = e.values[1];
- var received = e.values[2];
- var company = e.values[3];
- var account = e.values[4];
- var invoice = e.values[5];
- var name = e.values[6];
- var phone = e.values[7];
- var address = e.values[8];
- var desc = e.values[9];
- var part = e.values[10];
- var serial = e.values[11];
- var date = e.values[12];
- var fault = e.values[13];
- var info = e.values[14];
- var action = e.values[15];
- /*I make a copy of the document temporarily so that I can edit it*/
- var copyId = DocsList.getFileById(docTemplate).makeCopy(docName+' for '+company).getId();
- /*Open copied document*/
- var copyDoc = DocumentApp.openById(copyId);
- /*Grab the body of the document*/
- var copyBody = copyDoc.getActiveSection();
- /*Replacing my placeholder text with my variables from the form submission*/
- copyBody.replaceText('keyBranch', branch);
- copyBody.replaceText('keyReceive', received);
- copyBody.replaceText('keyCompany', company);
- copyBody.replaceText('keyAccount', account);
- copyBody.replaceText('keyInvoice', invoice);
- copyBody.replaceText('keyContact',name);
- copyBody.replaceText('keyPhone', phone);
- copyBody.replaceText('keyAddress', address);
- copyBody.replaceText('keyDesc', desc);
- copyBody.replaceText('keySerial', serial);
- copyBody.replaceText('keyPart', part);
- copyBody.replaceText('keyDate', date);
- copyBody.replaceText('keyFault',fault);
- copyBody.replaceText('keyInfo', info);
- copyBody.replaceText('keyAction', action);
- copyDoc.saveAndClose();
- /*Subject/body for the email*/
- var subject = 'Fault report for '+company;
- var body = "Here is the fault report for "+company;
- var faultreport = DocsList.getFileById(copyId).getAs("application/pdf");
- /*Send email to my email address stated above, with a PDF attachment of my fault report.*/
- MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: faultreport});
- doGet(copyId); /*Run above function to download PDF*/
- /*Delete temporary document so I don't get a drive full of fault reports*/
- DocsList.getFileById(copyId).setTrashed(true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement