Skip to content

Instantly share code, notes, and snippets.

@barrieroberts
Last active April 5, 2021 23:09
Show Gist options
  • Save barrieroberts/013e6c710f5afa369184ccb47a9b6dfb to your computer and use it in GitHub Desktop.
Save barrieroberts/013e6c710f5afa369184ccb47a9b6dfb to your computer and use it in GitHub Desktop.
Conference talk information
//Bound to a Google Form
//SCRIPT 1
//Get talks from form submission
function getFormSubmission(e) {
const form = FormApp.getActiveForm();
//Get list of talks selected
const lastRespNo = form.getResponses().length - 1;
const latestResp = form.getResponses()[lastRespNo];
const listOfTalks = latestResp.getItemResponses()[0]
.getResponse();
//Get talk info and add to Google Doc
const talkInfoSelected = getTalkInfo(listOfTalks);
const doc = addTalksToDoc(talkInfoSelected);
//Send email
const userEmail = e.response.getRespondentEmail();
sendTalkInfo(userEmail, doc);
}
//SCRIPT 2
//Get info on talks selected
//Get info on talks selected
function getTalkInfo(listOfTalks) {
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];
});
var talkInfoSelected = listOfTalks.map(function (talk2) {
var talkIndex = talkTitles.indexOf(talk2);
if (talkIndex > -1) {
return talkInfo[talkIndex];
}
});
return talkInfoSelected;
}
//SCRIPT 3
//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) {
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 4
//Email talk info to user
//Email talk info to user
function sendTalkInfo(userEmail, doc) {
var emailBody = HtmlService.createHtmlOutputFromFile('email')
.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
<!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.
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