Skip to content

Instantly share code, notes, and snippets.

@andreyka26-git
Created July 25, 2022 09:36
Show Gist options
  • Save andreyka26-git/106ffc3bca0985295dfbfa6019ef6245 to your computer and use it in GitHub Desktop.
Save andreyka26-git/106ffc3bca0985295dfbfa6019ef6245 to your computer and use it in GitHub Desktop.
Google Docs Add today's date with heading
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('Services')
.addItem('Add Day', 'addNewDay')
.addToUi();
}
function formatTwoDigit(n) {
return n < 10 ? '0' + n : '' + n;
}
function addNewDay() {
let cursor = DocumentApp.getActiveDocument().getCursor();
if (!cursor) {
return;
}
let date = new Date();
let dateStr = formatTwoDigit(date.getDate()) + '.' + (formatTwoDigit(date.getMonth() + 1)) + '.' + date.getFullYear();
let element = cursor.insertText(dateStr);
if (element) {
element.setBold(true);
let parent = element.getParent();
let style = {};
style[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.HEADING4;
parent.setAttributes(style);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment