Advertisement
Guest User

Untitled

a guest
Aug 18th, 2013
1,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*This is the document template I created*/
  2. var docTemplate = "1yw00mmVcRB8kEFYz4tyAcD1FiNx1tjKfMYrikMSuOHg"
  3. /*Document name for naming the PDF at the end*/
  4. var docName = "Fault Report";
  5. /*The email address the forms get emailed to*/
  6. var email_address = "example@example.com";
  7. /*Function grabs pdf file from ID passed from onFormSubmit line 18*/
  8. function getpdfFile(copyId) {
  9.   var pdfFile = "No Content";
  10.   pdfFile = DocsList.getFileById(copyId);
  11.   return pdfFile
  12. }
  13. /*Download pdf file from copyId, runs at end of onFormSubmit*/
  14. function doGet(copyId) {
  15.   return ContentService.createTextOutput().append(getpdfFile(copyId)).downloadAsFile('Fault report for '+company);
  16. }
  17. /*This function is linked as a trigger from the form page - onFormSubmit.*/
  18. function onFormSubmit(e) {
  19.   /*I set the variable branch to the first value in the form - in this case, branch*/
  20.   var branch = e.values[1];
  21.   var received = e.values[2];
  22.   var company = e.values[3];
  23.   var account = e.values[4];
  24.   var invoice = e.values[5];
  25.   var name = e.values[6];
  26.   var phone = e.values[7];
  27.   var address = e.values[8];
  28.   var desc = e.values[9];
  29.   var part = e.values[10];
  30.   var serial = e.values[11];
  31.   var date = e.values[12];
  32.   var fault = e.values[13];
  33.   var info = e.values[14];
  34.   var action = e.values[15];
  35.   /*I make a copy of the document temporarily so that I can edit it*/
  36.   var copyId = DocsList.getFileById(docTemplate).makeCopy(docName+' for '+company).getId();
  37.   /*Open copied document*/
  38.   var copyDoc = DocumentApp.openById(copyId);
  39.   /*Grab the body of the document*/
  40.   var copyBody = copyDoc.getActiveSection();
  41.   /*Replacing my placeholder text with my variables from the form submission*/
  42.   copyBody.replaceText('keyBranch', branch);
  43.   copyBody.replaceText('keyReceive', received);
  44.   copyBody.replaceText('keyCompany', company);
  45.   copyBody.replaceText('keyAccount', account);
  46.   copyBody.replaceText('keyInvoice', invoice);
  47.   copyBody.replaceText('keyContact',name);
  48.   copyBody.replaceText('keyPhone', phone);
  49.   copyBody.replaceText('keyAddress', address);
  50.   copyBody.replaceText('keyDesc', desc);
  51.   copyBody.replaceText('keySerial', serial);
  52.   copyBody.replaceText('keyPart', part);
  53.   copyBody.replaceText('keyDate', date);
  54.   copyBody.replaceText('keyFault',fault);
  55.   copyBody.replaceText('keyInfo', info);
  56.   copyBody.replaceText('keyAction', action);
  57.   copyDoc.saveAndClose();
  58.   /*Subject/body for the email*/
  59.   var subject = 'Fault report for '+company;
  60.   var body = "Here is the fault report for "+company;
  61.   var faultreport = DocsList.getFileById(copyId).getAs("application/pdf");
  62.   /*Send email to my email address stated above, with a PDF attachment of my fault report.*/
  63.   MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: faultreport});
  64.   doGet(copyId); /*Run above function to download PDF*/
  65.   /*Delete temporary document so I don't get a drive full of fault reports*/
  66.   DocsList.getFileById(copyId).setTrashed(true);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement