Learn new Google Workspace Skills
A client wanted a way to insert today's date automatically into his slides so his students could know today's date. Right now there is not a function in Slides to insert date, so I have documented here the work around to accomplish this.
It is done with a spreadsheet, using the =TODAY() formula in a cell, then copying the cell or multiple cells and then pasting the table (series of cells) using the 'link to spreadsheet' option. This will allow you to update the date on your slide each day by clicking the update button that appears on the pasted table in the slide.
//This creates the menu
function onOpen() {
var ui = DocumentApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Utilities')
.addItem('Insert Date', 'insertAtCursor')
.addToUi();
}
//Inserts the date at the current cursor location in boldface and italic.
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 = Utilities.formatDate(new Date(), "GMT", "MM-dd-yyyy"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(date);
if (element) {
element.setBold(true);
element.setItalic(true);
} else {
DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
}
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
This is an autodate doc example. You can also make a copy of this document, and it will contain the script.