Skip to content

Instantly share code, notes, and snippets.

@501A-Designs
Created November 10, 2021 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 501A-Designs/e0ee3eeb78b1cda8f2fc6694bfc9ea5a to your computer and use it in GitHub Desktop.
Save 501A-Designs/e0ee3eeb78b1cda8f2fc6694bfc9ea5a to your computer and use it in GitHub Desktop.
Takes files from a folder and attaches it to a Gmail draft
function doGet(){return HtmlService.createHtmlOutputFromFile("index")}
function myFunction() {
var folderId = ""; // folder with docs
var moveToFolderId = ""; // folder to move PDF
var doneFolderId = ""; // folder with docs that have already been converted to PDF (to reduce duplication)
var filesN = DriveApp.getFolderById(folderId).getFiles();
var downloadedFolder = DriveApp.getFolderById(moveToFolderId);
var doneFolder = DriveApp.getFolderById(doneFolderId);
while(filesN.hasNext()){
var doc = filesN.next();
doc.moveTo(doneFolder);
var docName = doc.getName();
// PDF Conversion
docBlob = doc.getAs('application/pdf');
docBlob.setName(docName + ".pdf");
var file = downloadedFolder.createFile(docBlob);
// Logger.log('Your PDF file is available at ' + file.getUrl());
}
}
function createGmailDraft(){
var moveToFolderId = ""; // folder to move PDF
var getPdfFiles = DriveApp.getFolderById(moveToFolderId).getFiles();
var pdfFileArray = [];
while(getPdfFiles.hasNext()){
var pdfFile = getPdfFiles.next();
pdfFileArray.push(pdfFile);
}
Logger.log(pdfFileArray);
GmailApp.createDraft(
"EMAIL", //Sender
"SUBJECT", //Subject
"〇〇 Files Attached (Confirm and write nunber).\nMultiple lines.", //Content
{attachments:pdfFileArray} //Attach File
);
}
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<base target="_top">
</head>
<style>
body{
padding:5%;
font-family:arial;
}
a{
cursor:pointer;
border-radius:5px;
padding: 0.3em 0.5em;
margin-right:2px;
background:#f0f0f0;
text-decoration:none;
color:grey;
}
header{
border-radius:10px;
padding:1em;
box-shadow: 0px 5px 10px #e6e6e6;
border: 1px solid #e6e6e6;
}
header p{
font-style:italic;
}
#status,#emailStatus{
display:none;
margin: 0.5em 0;
padding: 1em;
border-radius:10px;
box-shadow: 0px 2px 5px #e6e6e6;
border: 1px solid #e6e6e6;
}
#status div,
#emailStatus div{
padding:0;
margin:0;
display:flex;
gap:1em;
flex-direction:row;
align-items:center;
}
#doneId,#emailDoneId{
display:none;
margin: 0.5em 0;
padding: 0.8em 1em;
border-radius:10px;
box-shadow: 0px 2px 5px #e6e6e6;
border: 1px solid #e6e6e6;
}
#doneId h3{
color:#4c8bf5;
}
#emailDoneId h3{
color:#de5246;
}
input{
width: auto;
min-width: 200px;
max-width: 500px;
border-radius: 5px;
outline: none;
border: 2px solid #e6e6e6;
background:#f0f0f0;
padding: 15px;
transition: .5s;
}
input:focus{
background:white;
border: 2px solid #4c8bf5;
box-shadow: 0 1px 5px lightblue;
color: black;
}
button{
cursor:pointer;
width: auto;
border-radius: 5px;
font-weight:bold;
color: #fff;
outline: none;
border: none;
padding: 15px;
transition: .5s;
}
#convertBtn{
background: #4c8bf5;
}
#createDraftBtn{
display:none;
background: #de5246;
}
</style>
<script>
function createPdf(){
var result = confirm('Are you sure?');
if(result) {
google.script.run.withSuccessHandler(conversionUpdate).myFunction();
document.getElementById("status").style.display = 'block';
document.getElementById("convertBtn").style.display = 'none';
} else {
alert('Cancelled')
}
}
function conversionUpdate(){
document.getElementById("status").style.display = 'none';
document.getElementById("doneId").style.display = 'block';
document.getElementById("createDraftBtn").style.display = 'block';
}
function createDraft(){
var result = confirm('Would you like to create a draft?');
if(result) {
google.script.run.withSuccessHandler(emailUpdate).createGmailDraft();
document.getElementById("emailStatus").style.display = 'block';
document.getElementById("createDraftBtn").style.display = 'none';
} else {
alert('Cancelled')
}
}
function emailUpdate(){
document.getElementById("emailStatus").style.display = 'none';
document.getElementById("emailDoneId").style.display = 'block';
}
</script>
<body>
<h1>PDF to Gmail draft</h1>
<ol>
<li>Instructions go here</li>
</ol>
<div id="updates">
<div id="status">
<div>
<img width="40" height="40" src="https://cutewallpaper.org/21/loading-gif-transparent-background/Update-throbber-icon-in-Seven-theme-2775725-Drupalorg.gif">
<p>Now converting</p>
</div>
</div>
<div id="doneId">
<h3>✓ Successfully converted</h3>
</div>
<div id="emailStatus">
<div>
<img width="40" height="40" src="https://cutewallpaper.org/21/loading-gif-transparent-background/Update-throbber-icon-in-Seven-theme-2775725-Drupalorg.gif">
<p>Creating Gmail draft</p>
</div>
</div>
<div id="emailDoneId">
<h3>✓ Successfully created draft</h3>
</div>
</div>
<br/>
<button id="createDraftBtn" onclick='createDraft()'>Create Gmail draft with PDFs attached</button>
<button id="convertBtn" onclick="createPdf();">Convert Doc to PDF</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment