Last active
April 5, 2021 23:12
-
-
Save barrieroberts/36cd66b71183631dc62b286a1d5d3002 to your computer and use it in GitHub Desktop.
Conference talk information-webapp form
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
//Stand alone script file | |
//SCRIPT 1 | |
function doGet() { | |
//Get talks | |
var ssConf = SpreadsheetApp.openById('SPREADSHEET ID'); | |
var shTalkInfo = ssConf.getSheetByName('TALKINFO'); | |
var allTalkInfo = shTalkInfo.getDataRange().getValues(); | |
allTalkInfo.shift(); | |
//Build form | |
var formTmp = HtmlService.createTemplateFromFile('2form'); | |
formTmp.talks = allTalkInfo.map(function (talkInfo, n) { | |
return '<p><label><input id="CB' + n + | |
'" type="checkbox" class="indeterminate-checkbox"/><span>' | |
+ talkInfo[0] + | |
'</span></label></p>' | |
}).join(''); | |
return formTmp.evaluate(); | |
} | |
function include(filename) { | |
return HtmlService.createHtmlOutputFromFile(filename) | |
.getContent(); | |
} | |
//HTML 2 | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<base target="_top"> | |
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | |
<link rel="stylesheet" | |
href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="row"> | |
<h3 class="card-panel blue darken-4 white-text">CONFERENCE TALKS</h3> | |
</div> | |
<form> | |
<div class="row" class="input-field col s12"> | |
<input type="email" id="emailAdd"> | |
<label>Email address</label> | |
</div> | |
<div id="talksCBs" class="row" class="input-field col s12" class="checkbox"> | |
<?!= talks; ?> | |
</div> | |
</form> | |
<div class="row"> | |
<button id="btn" class="btn-small blue darken-4 col s2"> | |
<i class="material-icons left">send</i>SEND</button> | |
</div> | |
</div> <!--CLOSE CONTAINER--> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script> | |
<?!= include('3getFormSub'); ?> | |
</body> | |
</html> | |
//HTML 3 | |
<script> | |
//Initiate - Materialize | |
document.addEventListener('DOMContentLoaded', function() { | |
var elems = document.querySelectorAll('input[type="checkbox"].checkbox'); | |
var instances = M.FormSelect.init(elems); | |
}); | |
//Listen for send button click | |
document.getElementById("btn") | |
.addEventListener("click", sendInfoFromForm); | |
function sendInfoFromForm(){ | |
//Store multiple form info | |
var cbDiv = document.getElementById("talksCBs"); | |
var numOfCBs = cbDiv.getElementsByTagName("P").length; | |
//Get email address and talk status (ticked or not) | |
var emailAdd = document.getElementById("emailAdd").value; | |
var tInfo = []; | |
for(i=0;i<numOfCBs;i++){ | |
tInfo.push(document.getElementById("CB"+i).checked); | |
} | |
//Send to Apps Script side | |
google.script.run.getTalkInfo(tInfo, emailAdd); | |
//Reset form | |
document.getElementById("emailAdd").value = ""; | |
for(j=0;j<numOfCBs;j++){ | |
document.getElementById("CB"+j).checked = false; | |
} | |
M.updateTextFields(); | |
} | |
</script> | |
//SCRIPT 4 | |
//Get info on talks selected | |
function getTalkInfo(tInfo, emailAdd) { | |
const ss = SpreadsheetApp.openById('SPREADSHEET ID'); | |
const shTalkInfo = ss.getSheetByName('TALKINFO'); | |
var talkInfo = shTalkInfo.getDataRange().getValues(); | |
talkInfo.shift(); | |
var talkTitles = talkInfo.map(function (talk) { | |
return talk[0]; | |
}); | |
//If true get talk title | |
var listOfTalks = []; | |
tInfo.forEach(function (ti, n) { | |
if (ti === true) { | |
listOfTalks.push(talkTitles[n]); | |
} | |
}); | |
var talkInfoSelected = listOfTalks.map(function (talk2) { | |
var talkIndex = talkTitles.indexOf(talk2); | |
if (talkIndex > -1) { | |
return talkInfo[talkIndex]; | |
} | |
}); | |
var doc = addTalksToDoc(talkInfoSelected); | |
var userEmail = emailAdd; | |
sendTalkInfo(userEmail, doc); | |
} | |
//SCRIPT 5 | |
//Create Doc and add talk info to it | |
//Create Doc and add talk info to it | |
function addTalksToDoc(talkInfoSelected) { | |
//Create doc | |
var doc = DocumentApp.create('Conference Talk Information'); | |
var body = doc.getBody(); | |
doc.addHeader(); | |
var header = doc.getHeader(); | |
var confLogo = DriveApp.getFileById('IMAGE ID').getBlob(); | |
var hparas = header.getParagraphs(); | |
hparas[0].insertInlineImage(0, confLogo); | |
var style = {}; | |
style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri'; | |
style[DocumentApp.Attribute.FONT_SIZE] = 16; | |
style[DocumentApp.Attribute.BOLD] = true; | |
//Loop thru talks | |
talkInfoSelected.forEach(function (talkSel, t) { | |
var talk = talkSel[0], | |
speaker = talkSel[1], | |
image = talkSel[2], | |
time = talkSel[3], | |
room = talkSel[4], | |
capacity = talkSel[5], | |
type = talkSel[6], | |
audience = talkSel[7], | |
blurb = talkSel[8]; | |
//Add info to Doc | |
var fileID = image.match(/[\w\_\-]{25,}/).toString(); | |
var blob = DriveApp.getFileById(fileID).getBlob(); | |
body.appendImage(blob); | |
var allListInfo = ["Talk: " + talk, | |
"Speaker: " + speaker, | |
"Time: " + time, | |
"Room: " + room + " - Capacity: " | |
+ capacity + " people", | |
"Type: " + type, | |
"Target audience: " + audience]; | |
allListInfo.forEach(function (info) { | |
body.appendListItem(info) | |
.setIndentStart(0) | |
.setLineSpacing(2) | |
.setGlyphType(DocumentApp.GlyphType.SQUARE_BULLET) | |
.setAttributes(style); | |
}); | |
var style2 = {}; | |
style2[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] | |
= DocumentApp.HorizontalAlignment.JUSTIFY; | |
body.appendHorizontalRule(); | |
body.appendParagraph(blurb).setAttributes(style2); | |
body.appendHorizontalRule(); | |
body.appendPageBreak(); | |
}); | |
doc.saveAndClose(); | |
return doc; | |
} | |
//SCRIPT 6 | |
//Email talk info to user | |
//Email talk info to user | |
function sendTalkInfo(userEmail, doc) { | |
var emailBody = HtmlService.createHtmlOutputFromFile('7email') | |
.getContent(); | |
//Get G Doc and make a PDF then attach | |
var pdf = doc.getAs('application/pdf').getBytes(); | |
var attach = { | |
fileName: 'Conference Talk Information.pdf', | |
content: pdf, | |
mimeType: 'application/pdf' | |
}; | |
MailApp.sendEmail(userEmail, "Conference Talk Information", '', | |
{ | |
htmlBody: emailBody, | |
replyTo: 'baz@bazroberts.com', | |
attachments: [attach] | |
}); | |
//Delete Doc from Drive | |
var document = DriveApp.getFileById(doc.getId()); | |
DriveApp.removeFile(document); | |
} | |
//HTML 7 | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<base target="_top"> | |
<style> | |
p { | |
font-family: verdana; | |
font-size: 1.2em; | |
} | |
#image { | |
float: left; | |
width: 60; | |
padding-left: 10px; | |
} | |
</style> | |
</head> | |
<body> | |
<div> | |
<img id="image" src="https://docs.google.com/uc?id=1TgkYB5JEgE6umXfKb1FxhHkKm4gfvy4g" height=60 width=60> | |
<h2>CONFERENCE TALK INFORMATION</h2> | |
<p>Please find attached the information on the talks you requested.</p> | |
<p>We hope you enjoy the conference.</p> | |
<p>Best regards,</p> | |
<p>Barrie Roberts</p> | |
<p>Conference Manager</p> | |
<hr> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment