Created
August 24, 2017 20:36
-
-
Save raywu/52ab3328b6dbba0962a1b9442c6badea to your computer and use it in GitHub Desktop.
replace date string in Google Documents using Apps Script
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
var monthNames = [ | |
"January", "February", "March", "April", "May", "June", | |
"July", "August", "September", "October", "November", "December" | |
]; | |
function onOpen() { | |
var ui = DocumentApp.getUi(); | |
// Or FormApp or SpreadsheetApp. | |
ui.createMenu('Macro') | |
.addItem('Update last modified date', 'replaceFormattedDate') | |
.addToUi(); | |
} | |
function replaceDate(regex, replacement) { // replace all | |
var body = DocumentApp.getActiveDocument().getBody(); | |
return body.replaceText(regex, replacement); | |
} | |
function replaceFormattedDate() { | |
var regex = "[a-zA-z]+\\s\\d{1,2},\\s\\d{4}", // escape '\', and matches 'monthName dd, yyyy' format | |
formattedDate = DocumentApp.getActiveDocument().getBody().findText(regex), | |
d, | |
dd, | |
monthName, | |
yyyy, | |
date, | |
element; | |
if (formattedDate) { | |
d = new Date(); | |
dd = d.getDate(); | |
dd = pad(dd, 2) | |
monthName = monthNames[d.getMonth()]; // Months are zero based, same with array | |
yyyy = d.getFullYear(); | |
date = monthName + ' ' + dd + ', ' + yyyy; | |
replaceDate(regex, date); | |
} else { | |
DocumentApp.getUi().alert('Cannot find a date format that matches "[a-zA-z]+\\s\\d{1,2},\\s\\d{4}" in the document.'); | |
} | |
} | |
function pad(str, max) { | |
str = str.toString(); | |
return str.length < max ? pad("0" + str, max) : str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment