-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdocsAppScript
42 lines (40 loc) · 1.63 KB
/
gdocsAppScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* The onOpen function runs automatically when the Google Docs document is
* opened. Use it to add custom menus to Google Docs that allow the user to run
* custom scripts. For more information, please consult the following two
* resources. This adds a "Utilities" money with the option to "Insert Date"
*
* Extending Google Docs developer guide:
* https://developers.google.com/apps-script/guides/docs
*
* Document service reference documentation:
* https://developers.google.com/apps-script/reference/document/
*/
function onOpen() {
// Add a menu with some items, some separators, and a sub-menu.
DocumentApp.getUi().createMenu('Utilities')
.addItem('Insert Date', 'insertAtCursor')
.addToUi();
}
/**
* Inserts the date at the current cursor location in boldface.
*/
function insertAtCursor() {
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
// Attempt to insert text at the cursor position. If insertion returns null,
// then the cursor's containing element doesn't allow text insertions.
var date = (new Date).toLocaleDateString('default', {weekday: 'long', day: 'numeric', month: 'long', year: 'numeric'}); // use your local (browser) date format with month name
var element = cursor.insertText(date);
var style = {};
style[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.HEADING2;
if (element) {
element.setBold(true);
element.setAttributes(style);
} else {
DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
}
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}