I have been working on a document that details the changes needed to migrate a system from Java 8 to OpenJDK 11. While writing documents I add notes to myself in-situ, eg "TODO: Buy bread and milk before the next nor'easter." As the document was getting longer and the TODOs were mounting an itch was developing. I wanted a "Table of TODOs."
I use Google Docs and so the itch was scratched using Google's App Script. The script collects the text of all the TODO paragraphs and organizes them under a Table of TODOs heading. The TODO is a normal-styled paragraph that starts with "TODO: ". The Table of TODOs is a heading-styled paragraph that contains only "Table of TODOs". The script organizes the TODO texts in to numbered list items under the heading. The script also adds a Bookmark to each TODO paragraph so that it can be referenced from the numbered list item. To see the example you will need a Google account and to make a copy before you can view the script (under Tools / Script editor). [Update: Placed the script in a gist.]
The script was far more difficult to write than I expected. Google's scripting documentation is sparse and the IDE would randomly stop working. There is noApp Script language documentation (a mixed set of JavaScript versions) and the documentation for the Document API is a bare description of each class. Searching for help was hindered by the generality of any kind of questions I had, eg "google app script api bookmark" was more likely to get results about using URI bookmarks in Chrome. I am sure there are good resources and communities out there, but I have not found them yet. I will keep looking as the Google Suite can be combined into sophisticated applications and I would like to know how to do that.
Showing posts with label google app script. Show all posts
Showing posts with label google app script. Show all posts
Update revision date with Google App Script
I was bitching about what Google Documents is missing for technical documents. Which, in all honestly, is not much, especially, in the light of the revision and collaboration tools it does have -- and had on its first day.
Since I like to have revision dates and numbers in my documents how hard could it be to script their automatic update. Not hard at all. The following script will add an "Update Revision" menu item to a "Scripts" menu and when invoked it will replace all occurances of "Revision YYYY-MM-DD HH:MM" in headers, footers, and the body with the current timestamp.
Since I like to have revision dates and numbers in my documents how hard could it be to script their automatic update. Not hard at all. The following script will add an "Update Revision" menu item to a "Scripts" menu and when invoked it will replace all occurances of "Revision YYYY-MM-DD HH:MM" in headers, footers, and the body with the current timestamp.
function onOpen() {
DocumentApp.getUi().createMenu('Scripts')
.addItem('Update Revision', 'updateRevisionTimestamp')
.addToUi();
};
function updateRevisionTimestamp() {
var doc = DocumentApp.getActiveDocument();
var date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm");
var revisionPattern = "Revision [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}";
var revisionReplacement = "Revision " + date;
var body = doc.getBody();
if ( body ) {
body.replaceText(revisionPattern, revisionReplacement);
}
var footer = doc.getFooter();
if ( footer ) {
footer.replaceText(revisionPattern, revisionReplacement);
}
var header = doc.getHeader();
if ( header ) {
header.replaceText(revisionPattern, revisionReplacement);
}
return doc;
}
Subscribe to:
Posts (Atom)