Hello, Gliders,
Many of you have use-cases like,
→ Sending certificates to your students
→ Sending Auto-Generated Invoices, Quotes & Many more
→ creating ID cards for the events
→ Mail merge custom PDFs
Here I’ll show you how to implement it.
Demo: https://milky-produce-8069.glideapp.io/ [It’s in Personal Plan, Only 80 Webhook Actions Left]
Whenever the user fills and submits a button, it’ll trigger the script and send a pdf attached mail to the receiver.
The flow of the System
Glide App → Webhook Action → Google Apps Script → Google Docs → PDF Mail
Implementation Steps
Step: 1Create a Google Doc Template (Source File) Make Copy from here for demo purpose Make a Copy of Google Doc
Now, open copied document, copy the document id from the URL. (Will be used later) (Don’t include /d/)
Step: 2
Make a Google Drive Folder (It’ll be used as temporary to store & delete files)
Copy Drive Folder ID (Will be used later)
Step: 3
Copy following code to Google Apps Script
Click here to make new script project script.new
function doPost(e) {
// Calling Parameters from GlideApp
const body = JSON.parse(e.postData.contents);
const ProjectName = body["params"]["ProjectName"]["value"];
const CompanyName = body["params"]["CompanyName"]["value"];
const YourName = body["params"]["YourName"]["value"];
const YourEmail = body["params"]["YourEmail"]["value"];
const Date = body["params"]["Date"]["value"];
// Copy IDs
const doc_id= "ENTER DOCUEMENT ID HERE";
const tempFolder_id = "ENTER DRIVE FOLDER ID HERE";
const template = DriveApp.getFileById(doc_id);
const tempFolder = DriveApp.getFolderById(tempFolder_id);
const doc_new = template.makeCopy(tempFolder).setName(ProjectName);
const doc_new_id = doc_new.getId();
const open_doc = DocumentApp.openById(doc_new_id);
const body_doc = open_doc.getBody();
// Replaces all values to new ones.
body_doc.replaceText("Project name", ProjectName);
body_doc.replaceText("Date", Date);
body_doc.replaceText("YOUR COMPANY NAME", CompanyName);
body_doc.replaceText("YOUR NAME", YourName);
body_doc.replaceText("YOUR EMAIL", YourEmail);
open_doc.saveAndClose();
const BLOBPDF = doc_new.getAs(MimeType.PDF);
//"Remove this to store pdf to temporary folder " tempFolder.createFile(BLOBPDF).setName(ProjectName);
tempFolder.removeFile(doc_new);
const senderName = "Project Proposal";
const subject = YourName + ", you're awesome!";
const body_mail = "Please find your proposal for "+ ProjectName + "attached";
GmailApp.sendEmail(YourEmail, subject, body_mail, {
attachments: [BLOBPDF],
name: senderName
});
SpreadsheetApp.flush();
}
Step: 3
Assign an Existing GCP Project to the settings of Apps Script or create a new GCP Project, Let me know if you're facing any difficulties.
Step: 4
Deploy GAS Project as a web app.
Copy Web App URL after deploying.
Important: Whenever you make changes to your code, you’ve to redeploy that code and change the URL.
Step: 5
Setup GlideApp Webhook Action to a button
Important: Check Spell twice before running the script.
Done!
You can also send pdfs via google slides. Let me know if anyone wants a tutorial on it.