Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Last active August 10, 2016 02:46
Show Gist options
  • Select an option

  • Save theY4Kman/18ef5cfd27765f7cd353b4efeae32a1a to your computer and use it in GitHub Desktop.

Select an option

Save theY4Kman/18ef5cfd27765f7cd353b4efeae32a1a to your computer and use it in GitHub Desktop.
A Google Apps standalone script which automatically creates a new diary entry with a prefilled header, and clears out old, unfilled entries.
// To install, copy script over to new project at scripts.google.com.
// Create two folders, Diary and Archive (one may be inside the other).
// Configure the constants below (you can find the folder IDs in your address bar at drive.google.com)
// Create a new time-driven trigger set to Day timer (Resources > Current project's triggers > Add a new trigger)
// For diary entries
var DIARY_FOLDER_ID = '<configure me>';
// For empty diary entries cleaned out.
// Note: they're not deleted, due to concern of runaway deletion with no backup
var DIARY_ARCHIVE_FOLDER_ID = '<configure me>';
function dailyCron() {
clearEmptyEntries();
createDiaryEntry();
}
function clearEmptyEntries() {
var diaryFolder = DriveApp.getFolderById(DIARY_FOLDER_ID);
var archiveFolder = DriveApp.getFolderById(DIARY_ARCHIVE_FOLDER_ID)
var entries = diaryFolder.getFiles();
while (entries.hasNext()) {
var file = entries.next();
if (_isEntryEmpty(file)) {
Logger.log('Archiving empty entry %s (%s)', file.getName(), file.getId());
_moveFile(file, diaryFolder, archiveFolder);
}
}
}
function _isEntryEmpty(file) {
// An entry is considered empty if its last update
// was within 1 second of its creation time.
var delta_ms = file.getLastUpdated() - file.getDateCreated();
return delta_ms <= 1000;
}
function createDiaryEntry() {
var date = new Date;
var doc = _createDiaryEntry(date);
_fillDiaryEntry(doc, date);
Logger.log('Created new diary entry %s (%s)', doc.getName(), doc.getId());
return doc;
}
function _createDiaryEntry(date) {
var title = _formatDate(date);
var doc = DocumentApp.create(title);
var file = DriveApp.getFileById(doc.getId());
_moveFile(file,
DriveApp.getRootFolder(),
DriveApp.getFolderById(DIARY_FOLDER_ID));
return doc;
}
function _moveFile(file, sourceFolder, destFolder) {
destFolder.addFile(file);
sourceFolder.removeFile(file);
}
function _fillDiaryEntry(doc, date) {
/**
* The header looks like:
* _________________________
* | 2016/08/09 |
* | --------------------- |
* | |
*/
var title = _formatDate(date);
var body = doc.getBody();
// Title
var titlePar = body.insertParagraph(0, title);
var attributes = {};
attributes[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.TITLE;
titlePar.setAttributes(attributes);
// Horizontal rule
body.appendHorizontalRule();
// First paragraph
body.appendParagraph('\n\t');
}
function _formatDate(date) {
var parts = [
date.getFullYear(),
pad(date.getMonth() + 1, 2),
pad(date.getDate(), 2)
];
return parts.join('/');
}
function pad(n, width, z) {
// Source: http://stackoverflow.com/a/10073788/148585
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment