Skip to content

Instantly share code, notes, and snippets.

@jsdbroughton
Last active August 25, 2017 19:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsdbroughton/505e3b16069f4d41fec1 to your computer and use it in GitHub Desktop.
Save jsdbroughton/505e3b16069f4d41fec1 to your computer and use it in GitHub Desktop.
function exampleEdit() {
var email = "15x.moon@email.com";
var sharedCode = "blahdiblah123";
var imageFile = convertSVG(editSVG(email, sharedCode, null), 'This is a PNG with the sharedCode');
// do whatever with the imageFile - I want to add it to an email to send the recipient address above
}
function editSVG(email, code, newFile, file) {
var props = PropertiesService.getScriptProperties(),
credentialsFolder = props.getProperty('credentialsFolderId'),
templateSVG = props.getProperty('templateSVGKey'),
svg,
tagReplace = {},
pattern = "",
newSVG;
newFile = newFile || 'newfile';
file = file || templateSVG;
svg = DriveApp.getFileById(file).getAs('image/svg+xml').getDataAsString();
tagReplace = {};
tagReplace.email = email || 'email@email.com';
tagReplace.code = code || 'blahdiblah';
Object.keys(tagReplace).forEach(function(tag){
pattern = "{{" + tag + "}}";
svg = svg.replace(pattern, tagReplace[tag]);
});
newSVG = DriveApp.getFolderById(credentialsFolder).createFile(newFile + (new Date()).getTime() + '.svg', svg);
return newSVG;
}
function convertSVG(svg, filename) {
filename = filename || 'newfile';
var props = PropertiesService.getScriptProperties(),
credentialsFolder = props.getProperty('credentialsFolderId'),
ccAPIKey = props('CloudConvertAPIkey'),
folderUrl = '',
process = {},
convertParams = {},
url = '',
fetch,
driveFile,
newFile,
fileBlob = Utilities.newBlob(''),
svgFile = (svg || editSVG()),
svgFile = svg || editSVG();
folderUrl = 'https://googledrive.com/host/' + DriveApp.getFolderById(credentialsFolder).getId();
process = [
{"apikey": ccAPIKey},
{"inputformat": "svg"},
{"outputformat": "png"}
];
convertParams = [
{input: 'download'},
{wait: true},
{file: folderUrl + '/' + svgFile.getName()}
];
url = "https://api.cloudconvert.com/convert?";
url += process.concat(convertParams).reduce(function(p,c,i,arr) {
return ((typeof p == 'string') ? p : (Object.keys(p)[0] + '=' + p[Object.keys(p)[0]])) + '&' + (Object.keys(c)[0] + '=' + c[Object.keys(c)[0]]);
}).replace(/&$/, "");
fetch = UrlFetchApp.fetch(url);
fileBlob = Utilities.newBlob(fetch.getContent());
newFile = Drive.Files.insert({
title: filename + '.png',
mimeType: 'image/png'
}, fileBlob);
driveFile = DriveApp.getFileById(newFile.id);
svgFile.setTrashed(true);
DriveApp.getFolderById(credentialsFolder).addFile(driveFile);
DriveApp.removeFile(driveFile);
return driveFile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment