Last active
August 31, 2019 11:16
-
-
Save loftwah/b2d36e1b7f10e8a3ff6287635ad3ab85 to your computer and use it in GitHub Desktop.
Send a PDF attachment in an email using GMail, with a document generated with a Docs template and a Google Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this is a function that fires when the webapp receives a GET request | |
function doGet(e) { | |
return HtmlService.createHtmlOutput("request received"); | |
} | |
//this is a function that fires when the webapp receives a POST request | |
function doPost(e) { | |
var params = JSON.stringify(e.postData.contents); | |
params = JSON.parse(params); | |
var myData = JSON.parse(e.postData.contents); | |
var workorder = myData.workorder; | |
var priority = myData.priority; | |
var address = myData.address; | |
var issued = myData.issued; | |
var completion = myData.completion | |
var tenant = myData.tenant | |
var description = myData.description | |
var assigned = myData.assigned | |
var selectedTemplateId = "1800REVERSE";//Copy and paste the ID of the template document here (you can find this in the document's URL) | |
var templateFile = DriveApp.getFileById(selectedTemplateId); | |
//Make a copy of the template file | |
var documentId = DriveApp.getFileById(templateFile).makeCopy().getId(); | |
//Rename the copied file | |
DriveApp.getFileById(documentId).setName(nameResponse.getResponseText() + date + ' Work Order'); | |
//Get the document body as a variable | |
var body = DocumentApp.openById(documentId).getBody(); | |
//Insert the entries into the document | |
body.replaceText('##workorder##', workorder); | |
body.replaceText('##priority##', priority); | |
body.replaceText('##address##', address); | |
body.replaceText('##issued##', issued); | |
body.replaceText('##completion##', completion); | |
body.replaceText('##tenant##', tenant); | |
body.replaceText('##assigned##', assigned); | |
body.replaceText('##description##', description); | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var lastRow = Math.max(sheet.getLastRow(),1); | |
sheet.insertRowAfter(lastRow); | |
var timestamp = new Date(); | |
sheet.getRange(lastRow + 1, 1).setValue(timestamp); | |
sheet.getRange(lastRow + 1, 2).setValue(workorder); | |
sheet.getRange(lastRow + 1, 3).setValue(priority); | |
sheet.getRange(lastRow + 1, 4).setValue(address); | |
sheet.getRange(lastRow + 1, 5).setValue(issued); | |
sheet.getRange(lastRow + 1, 6).setValue(completion); | |
sheet.getRange(lastRow + 1, 7).setValue(tenant); | |
sheet.getRange(lastRow + 1, 8).setValue(description); | |
sheet.getRange(lastRow + 1, 9).setValue(assigned); | |
var email = sheet.getRange(lastrow + 1, 10).getValue(); | |
var subject = "bleh bleh momomomo"; | |
var message = params; | |
var attachment = DocumentApp.openById(documentId).getAs('application/pdf'); | |
DriveApp.createFile(attachment); | |
GmailApp.sendEmail(email, subject, message, { | |
attachments: [attachment], | |
name: 'Sweaty Buttcrack' | |
}); | |
SpreadsheetApp.flush(); | |
return HtmlService.createHtmlOutput("Great Success!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment