Last active
August 28, 2024 20:30
-
-
Save potato-robert/28055359d90f2e3fd0822c5f6b9ec6ed to your computer and use it in GitHub Desktop.
Google Docs Resume Customizer
This file contains hidden or 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
| function onOpen() { | |
| DocumentApp.getUi().createMenu('Resume Customizer') | |
| .addItem('Customize Resume', 'openDialog') | |
| .addToUi(); | |
| } | |
| function openDialog() { | |
| var html = HtmlService.createHtmlOutputFromFile('Dialog') | |
| .setWidth(400) | |
| .setHeight(300); | |
| DocumentApp.getUi().showModalDialog(html, 'Customize Resume'); | |
| } | |
| function customizeResume(companyName, keywordsToRemove) { | |
| Logger.log('Customizing Resume...'); | |
| // Get the ID of the active document (the document where the script is run from) | |
| var originalDoc = DocumentApp.getActiveDocument(); | |
| var docId = originalDoc.getId(); | |
| // Get the name of the original document | |
| var originalDocName = DriveApp.getFileById(docId).getName(); | |
| // Create a new name by appending the company name | |
| var newDocName = originalDocName + " " + companyName; | |
| // Make a copy of the document | |
| var newDoc = DriveApp.getFileById(docId).makeCopy(newDocName); | |
| var newDocId = newDoc.getId(); | |
| var doc = DocumentApp.openById(newDocId); | |
| var body = doc.getBody(); | |
| // Replace URLs in the document | |
| // first link is to homepage and also needs a custom greeting added | |
| var urlStringToReplace = 'portfolio_link=resumeContact'; | |
| var urlStringReplacement = urlStringToReplace + '&greeting=' + toUrlEncoded(companyName); | |
| findAndReplaceLinks(urlStringToReplace, urlStringReplacement, body); | |
| // for all links, add the company name to portfolio link for tracking traffic in GA | |
| urlStringToReplace = 'portfolio_link=resume'; | |
| urlStringReplacement = urlStringToReplace + toCamelCase(companyName); | |
| findAndReplaceLinks(urlStringToReplace, urlStringReplacement, body); | |
| // Filter out empty or space-only keywords | |
| var filteredKeywords = keywordsToRemove.filter(function(keyword) { | |
| return keyword.trim().length > 0; // Retain non-empty and non-space keywords | |
| }); | |
| if (filteredKeywords && filteredKeywords.length > 0) removeSkills(body, filteredKeywords); | |
| doc.saveAndClose(); | |
| Logger.log('Resume customized successfully. New Doc ID: ' + newDocId); | |
| return newDocId; | |
| } | |
| // Function to convert a string to camelCase | |
| function toCamelCase(input) { | |
| const words = input.split(' '); | |
| const camelCase = words.map((word, index) => { | |
| /*if (index === 0) { | |
| return word.toLowerCase(); | |
| }*/ | |
| return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); | |
| }).join(''); | |
| return camelCase; | |
| } | |
| // Function to replace spaces with %20 | |
| function toUrlEncoded(input) { | |
| return input.replace(/ /g, '%20'); | |
| } | |
| function processForm(formObject) { | |
| var companyName = formObject.companyName; | |
| var keywordsToRemove = formObject.keywordsToRemove.split(','); | |
| customizeResume(companyName, keywordsToRemove); | |
| } | |
| /** | |
| * Remove skills from the document. | |
| * | |
| * Since my doc has all the skills in the second column of the big table that the Resume uses for formatting, | |
| * I need to look in there, to avoid removing mentions of that skill else-where (such as descriptions inside employment history) | |
| */ | |
| function removeSkills(body, keywordsToRemove) { | |
| // Original approach, removing skills from anywhere in the doc: | |
| // Remove specified keywords from the text while preserving formatting | |
| /*var paragraphs = body.getParagraphs(); | |
| for (var i = 0; i < paragraphs.length; i++) { | |
| var textObj = paragraphs[i].editAsText(); | |
| var text = textObj.getText(); | |
| for (var j = 0; j < keywordsToRemove.length; j++) { | |
| var keyword = keywordsToRemove[j]; | |
| var startIndex = text.indexOf(keyword); | |
| while (startIndex !== -1) { | |
| var endIndex = startIndex + keyword.length - 1; | |
| textObj.deleteText(startIndex, endIndex); | |
| text = textObj.getText(); // Update text after deletion | |
| startIndex = text.indexOf(keyword, startIndex); // Find the next occurrence | |
| } | |
| } | |
| }*/ | |
| // New approach, removing skills only from the 2nd column of the main table: | |
| // Access the first table in the document | |
| var tables = body.getTables(); | |
| if (tables.length > 0) { | |
| var table = tables[0]; // Access the first table | |
| var columnIndex = 1; // Column index to modify (0-based index for the second column) | |
| // Iterate through the rows and replace keywords in the specified column | |
| for (var row = 0; row < table.getNumRows(); row++) { | |
| var cell = table.getCell(row, columnIndex); | |
| var textObj = cell.editAsText(); | |
| var text = textObj.getText(); | |
| for (var j = 0; j < keywordsToRemove.length; j++) { | |
| var keyword = ', ' + keywordsToRemove[j]; | |
| var startIndex = text.indexOf(keyword); | |
| while (startIndex !== -1) { | |
| var endIndex = startIndex + keyword.length - 1; | |
| textObj.deleteText(startIndex, endIndex); | |
| text = textObj.getText(); // Update text after deletion | |
| startIndex = text.indexOf(keyword, startIndex); // Find the next occurrence | |
| } | |
| } | |
| } | |
| } | |
| } | |
| //from https://stackoverflow.com/a/18731628/7907510 | |
| /** | |
| * Get an array of all LinkUrls in the document. The function is | |
| * recursive, and if no element is provided, it will default to | |
| * the active document's Body element. | |
| * | |
| * @param {Element} element The document element to operate on. | |
| * . | |
| * @returns {Array} Array of objects, vis | |
| * {element, | |
| * startOffset, | |
| * endOffsetInclusive, | |
| * url} | |
| */ | |
| function getAllLinks(element) { | |
| var links = []; | |
| element = element || DocumentApp.getActiveDocument().getBody(); | |
| if (element.getType() === DocumentApp.ElementType.TEXT) { | |
| var textObj = element.editAsText(); | |
| var text = element.getText(); | |
| var inUrl = false; | |
| for (var ch=0; ch < text.length; ch++) { | |
| var url = textObj.getLinkUrl(ch); | |
| if (url != null) { | |
| if (!inUrl) { | |
| // We are now! | |
| inUrl = true; | |
| var curUrl = {}; | |
| curUrl.element = element; | |
| curUrl.url = String( url ); // grab a copy | |
| curUrl.startOffset = ch; | |
| } | |
| else { | |
| curUrl.endOffsetInclusive = ch; | |
| } | |
| } | |
| else { | |
| if (inUrl) { | |
| // Not any more, we're not. | |
| inUrl = false; | |
| links.push(curUrl); // add to links | |
| curUrl = {}; | |
| } | |
| } | |
| } | |
| if (inUrl) { | |
| // in case the link ends on the same char that the element does | |
| links.push(curUrl); | |
| } | |
| } | |
| else { | |
| try { | |
| var numChildren = element.getNumChildren(); | |
| for (var i=0; i<numChildren; i++) { | |
| links = links.concat(getAllLinks(element.getChild(i))); | |
| } | |
| } catch (e){ | |
| Logger.log(e); | |
| } | |
| } | |
| return links; | |
| } | |
| //from https://stackoverflow.com/a/18731628/7907510 | |
| /** | |
| * Replace all or part of UrlLinks in the document. | |
| * | |
| * @param {String} searchPattern the regex pattern to search for | |
| * @param {String} replacement the text to use as replacement | |
| * | |
| * @returns {Number} number of Urls changed | |
| */ | |
| function findAndReplaceLinks(searchPattern,replacement,doc) { | |
| var links = getAllLinks(doc); | |
| var numChanged = 0; | |
| for (var l=0; l<links.length; l++) { | |
| var link = links[l]; | |
| if (link.url.match(searchPattern)) { | |
| // This link needs to be changed | |
| var newUrl = link.url.replace(searchPattern,replacement); | |
| var color = link.element.getForegroundColor(link.endOffsetInclusive); | |
| var newLink = link.element; | |
| newLink.setLinkUrl(link.startOffset, link.endOffsetInclusive, newUrl); | |
| //the way links are styled is a mystery to me, sometimes it works to set the original color, sometimes it only works if the color is hardcoded | |
| //in my case, the first link uses a custom color, as opposed to the preset #666666, and applying that seems to work fine. Could be related to the root cause of the issue. | |
| newLink.setForegroundColor(link.startOffset, link.endOffsetInclusive, numChanged == 0 ? color : '#666666'); | |
| newLink.setUnderline(link.startOffset, link.endOffsetInclusive, true); | |
| numChanged++ | |
| } | |
| } | |
| return numChanged; | |
| } |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <body> | |
| <form id="customizeForm"> | |
| <label for="companyName">Company Name:</label><br> | |
| <input type="text" id="companyName" name="companyName" required><br><br> | |
| <label for="keywordsToRemove">Skills to Remove (comma-separated, no spaces):</label><br> | |
| <input type="text" id="keywordsToRemove" name="keywordsToRemove" required><br><br> | |
| <input type="button" id="customizeButton" value="Customize Resume" | |
| onclick="customizeResume()"> | |
| </form> | |
| <script> | |
| function customizeResume() { | |
| var button = document.getElementById("customizeButton"); | |
| button.disabled = true; | |
| button.value = "Processing..."; | |
| google.script.run | |
| .withSuccessHandler(function() { | |
| button.disabled = false; | |
| button.value = "Customize Resume"; | |
| closeDialog(); | |
| }) | |
| .withFailureHandler(function(error) { | |
| button.disabled = false; | |
| button.value = "Customize Resume"; | |
| alert("Error: " + error.message); | |
| }) | |
| .processForm(document.getElementById("customizeForm")); | |
| } | |
| function closeDialog() { | |
| google.script.host.close(); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment