Skip to content

Instantly share code, notes, and snippets.

@jsdbroughton
Last active December 19, 2015 15:29
Show Gist options
  • Save jsdbroughton/5976449 to your computer and use it in GitHub Desktop.
Save jsdbroughton/5976449 to your computer and use it in GitHub Desktop.
1. Visit the Google Sites hosted knowledge library. The script checks Session.getActiveUser() to grab just the name and checks ScriptProperties for you. If no entry exists it serves a HTML form with few options. 2. Save the settings and you are presented with the resultant signature block. If its ok, send to GMail. 3. That's it. Next time you vi…
function doGet(request) {
request = request || {parameter: {fn: null}};
var content, user = Session.getActiveUser(), data, json, importData, mode;
mode = request.parameter.fn;
content = mode;
try {
switch (mode) {
case 'Start Over':
ScriptProperties.deleteProperty(user.getEmail());
case 'Build Signature':
if (mode === 'Build Signature') {
importData = request.parameter;
importData.addresses = request.parameters.address;
Utilities.jsonStringify(request.parameters);
data = {
name: (function() { var u = UserManager.getUser(Session.getActiveUser()); return [u.getGivenName(), u.getFamilyName()].join(' ');})(),
title: importData.title,
pa: importData.pa,
addresses: (importData.addresses || []).map(function(address) { return getAddress(address); }),
company: importData.company,
delegators: importData.delegators,
directline: importData.directline,
mobile: importData.mobile,
build: true,
send: false,
sent: false
}
json = Utilities.jsonStringify(data);
ScriptProperties.setProperty(user.getEmail(), json);
}
default:
data = ScriptProperties.getProperty(user.getEmail());
if (data) {
page = HtmlService.createTemplateFromFile('view-signature.html');
page.data = Utilities.jsonParse(data);
page.data.url = ScriptApp.getService().getUrl();
page.data.send = false;
page.data.sent = false;
if (mode !== 'Build Signature') {
page.data.build = false;
}
if (mode === 'Set Signature') {
page.data.send = true;
sendSignature(user.getEmail(), page.evaluate().getContent());
page.data.send = false;
page.data.sent = true;
}
content = page.evaluate().getContent();
data = page.data;
json = Utilities.jsonStringify(data);
ScriptProperties.setProperty(user.getEmail(), json);
} else {
page = HtmlService.createTemplateFromFile('build-signature.html');
page.data = {
url: ScriptApp.getService().getUrl(),
name: (function() { var u = UserManager.getUser(Session.getActiveUser()); return [u.getGivenName(), u.getFamilyName()].join(' ');})(),
norecord: true
};
content = page.evaluate().getContent();
}
}
} catch (err) {
Logger.log(err);
return ContentService.createTextOutput(JSON.stringify(err)).setMimeType(ContentService.MimeType.JSON);
}
return HtmlService.createHtmlOutput().setContent(content).setTitle('AAM Signature Builder');
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
function getAddress(address) {
var addresses = {
london: 'london.html',
liverpool: 'amup-liverpool.html',
doha: 'doha.html'
}
return HtmlService.createHtmlOutputFromFile(addresses[address]).getContent();
}
function getPayload(signature) {
signature = signature.replace(/&/g, '&amp;').replace(/</g, '&lt;');
signature = signature.replace(/>/g, '&gt;').replace(/'/g, '&apos;').replace(/"/g, '&quot;');
var xml = '<?xml version="1.0" encoding="utf-8"?>' +
'<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:apps="http://schemas.google.com/apps/2006" >' +
'<apps:property name="signature" value="'+signature+'" /></atom:entry>';
return xml;
}
function sendSignature(email, signature) {
var requestData = {
'method': 'PUT',
'contentType': 'application/atom+xml',
'payload': getPayload(signature)
};
var result = authorisedUrlFetch(email, requestData);
return result.getResponseCode();
}
function authorisedUrlFetch(email, requestData) {
var oAuthConfig = UrlFetchApp.addOAuthService('google');
oAuthConfig.setConsumerSecret(UserProperties.getProperty('oAuthConsumerSecret'));
oAuthConfig.setConsumerKey(UserProperties.getProperty('oAuthClientID'));
oAuthConfig.setRequestTokenUrl('https://www.google.com/accounts/OAuthGetRequestToken?scope=https%3A%2F%2Fapps-apis.google.com%2Fa%2Ffeeds%2Femailsettings%2F');
oAuthConfig.setAuthorizationUrl('https://www.google.com/accounts/OAuthAuthorizeToken');
oAuthConfig.setAccessTokenUrl('https://www.google.com/accounts/OAuthGetAccessToken');
UrlFetchApp.addOAuthService(oAuthConfig);
requestData['oAuthServiceName'] = 'google';
requestData['oAuthUseToken'] = 'always';
var emailParts = email.split('@');
var url = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/' + emailParts[1] + '/' + emailParts[0] + '/signature';
var result = UrlFetchApp.fetch(url, requestData);
if ( result.getResponseCode() != 200 ) {
}
return result;
}
<? if (data.send !== true || data.sent === true) { ?>
<?!= include('styles.css.html'); ?>
<? if (data.build === true) { ?>
<p>Your settings resulted in the signature block below. To add it to your account click "Save to GMail".</p>
<? } else { ?>
<p>Your <? if (data.sent === true) { ?>saved signature is<? } else { ?>previously generated signature block<? } ?> is below.</p><p><? if (data.sent !== true) { ?>To reset your account signature to this, click "Save to GMail".<? } else { ?>You will need to close any GMail windows and reopen for this to become effective.<? } ?></p>
<? } ?>
<? } ?>
<div dir="ltr" style="color:#222;font-family:'Open Sans',arial;font-size:13px">
<div style="color:#222;font-family:'Open Sans',arial;font-size:13px;"><?!= data.name ?></div>
<? if (data.title !== '') { ?><div style="margin-bottom:1em"><?= data.title ?></div><? } ?>
<? if (data.delegators) { ?><div>PA to <?= data.delegators; ?></div><? } ?>
<? if (data.pa) { ?><div><span>PA</span>&nbsp;<span><?= data.pa; ?></span></div><? } ?>
<? if (data.directline) { ?><div><span>direct line</span>&nbsp;<span><?= data.directline; ?></span></div><? } ?>
<? if (data.mobile) { ?><div><span>mobile</span>&nbsp;<span><?= data.mobile; ?></span></div><? } ?>
<div>
<div style="margin-top:1em; font-weight:bold;color:#000;font-family:'Open Sans',arial;font-size:13px">
Allies and Morrison<br />
<? if (data.company === 'amup') { ?>
Urban Practitioners
<? } else { ?>
Architects
<? } ?>
</div>
<div style="color:#222;font-family:'Open Sans',arial;font-size:13px">
<? for (address in data.addresses) { ?>
<?!= data.addresses[address]; ?>
<? } ?>
</div>
<div style="color:#222;font-family:'Open Sans',arial;font-size:13px">
<? if (data.company === 'amup') { ?>
<a href="http://am-up.com" style="text-decoration:underline;" target="_blank">am-up.com</a>
<? } else { ?>
<a href="http://alliesandmorrison.com?source=emailsignature" style="text-decoration:underline;" target="_blank">alliesandmorrison.com</a>
<? } ?>
</div>
</div>
</div>
<? if (data.send !== true || data.sent === true) { ?>
<div id="buttons">
<form method="get" action="<?= data.url ?>">
<button name="fn" value="Start Over">Build a new Signature Block</button>
<? if (data.sent !== true) { ?>
<button name="fn" value="Set Signature">Save to GMail</button>
<? } ?>
</form>
</div>
<? } ?>
<?!= include('styles.css.html'); ?>
<? if (data.norecord === true) { ?>
<p>No signature has previously been saved</p>
<? } ?>
<form action="<?= data.url ?>" method="get">
<fieldset id="name"><legend>Contact</legend>
<label for="name">Name<input type="text" id="name" disabled="disabled" name="name" value="<?= data.name ?>" /> This is set elsewhere, any errors report to IT</label>
<div class="chunk">
<label class='radio' for="aam"><input id="aam" type="radio" name="company" value="aam" checked="checked" /><span class="chunk">Allies and Morrison<br />Architects</span></label>
<label class='radio' for="amup"><input id="amup" type="radio" name="company" value="amup" /><span class="chunk">Allies and Morrison<br />Urban Practitioners</span></label>
</div>
<p>Which Offices do you operate from?</p>
<label class='radio' for="london"><input id="london" type="checkbox" name="address" value="london" checked="checked" />Southwark Street</label>
<label class='radio' for="doha"><input id="doha" type="checkbox" name="address" value="doha" />Doha</label>
<label class='radio' for="liverpool"><input id="liverpool" type="checkbox" name="address" value="liverpool" />Liverpool</label>
</fieldset>
<fieldset>
<legend>Optional Details</legend>
<label for="pa">PA<input id="pa" name="pa" type="text" /> If you have a PA/Secretary, include their name here.</label>
<label class="radio" for="pato">PA to:<input id="pato" name="delegators" type="text" /></label> Add all Partners and Directors to whom you are a PA/Secretary (separated by commas)
<label for="direct">Direct Line<input id="direct" type="text" name="directline" /> (or direct line to PA)</label>
<label for="mobile">Mobile<input id="mobile" type="text" name="mobile" /> If you use your mobile phone for work</label>
</fieldset>
<fieldset id="title"><legend>Title</legend>
<label class='radio' for="partner"><input id="partner" type="radio" name="title" value="Partner" />Partner</label>
<label class='radio' for="director"><input id="director" type="radio" name="title" value="Director" />Director</label>
<label class='radio' for="assdirector"><input id="assdirector" type="radio" name="title" value="Associate Director" />Associate Director</label>
<label class='radio' for="seniorass"><input id="seniorass" type="radio" name="title" value="Senior Associate" />Senior Associate</label>
<label class='radio' for="ass"><input id="ass" type="radio" name="title" value="Associate" />Associate</label>
<label class='radio' for="none"><input id="none" type="radio" name="title" value="" checked="checked" />None</label>
</fieldset>
<div id="buttons">
<button name="fn" value="Build Signature">Save These Settings</button>
</div>
</form>
<style>
* {
font-family: 'trebuchet ms', sans-serif;
font-size: 13px;
}
label input {
margin-left: 1em;
}
label {
display: block;
margin-top: 0.5em;
}
label.radio, .chunk {
display: inline-block;
position: relative;
}
#name .radio input[type="radio"] {
position: relative;
top: -1em;
margin-right: 1ex;
}
.chunk {
font-weight: bold;
}
.chunk .radio {
float: left;
}
fieldset {
border: none;
margin: 2em 0;
background: #fafafa;
padding: 0 1em 1em;
}
legend, p {
font-size: 0.75em;
font-weight: bold;
}
legend {
position:relative;
top: -1em;
color: #d10000;
}
#name p, #name div {
padding-top: 1em;
}
#buttons {
margin-top: 2em;
}
[dir="ltr"] {
border-top: 1px dashed #ccc;
border-bottom: 1px dashed #ccc;
padding: 2em 0;
}
button {
border: none;
color: #333;
background: #ccc;
-webkit-appearance: none;
margin-left: 1em;
padding: 5px 20px;
border-radius: 1ex;
}
</style>
<div style="margin-top:1em">
<div>85 Southwark Street</div>
<div><span>London</span>&nbsp;<span>SE1 0HX</span></div>
</div>
<div><span>telephone</span>&nbsp;<span>020 7921 0100</span></div>
<div style="margin-top:1em">
<div>85 Southwark Street</div>
<div><span>London</span>&nbsp;<span>SE1 0HX</span></div>
</div>
<div><span>telephone</span>&nbsp;<span>020 7921 0100</span></div>
<div style="margin-top:1em">
<div>Ground Floor, Al Mirqab Tower</div>
<div><span>Cornice, Doha</span>&nbsp;<span>PO Box 24863</span></div>
</div>
<div><span>telephone</span>&nbsp;<span>(+974) 495 4604</span></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment