Sarmad Gardezi

Sarmad Gardezi

a freelance developer

Insert Date in Google Docs Using Google Apps Scripts

G

oogle Apps Scripts are best only code tools to automate your work with Gmail, Google Docs, Sheets and Forms. Google Apps Scripts helps you to perform specific tasks using codes.

People face many problems on their work when it comes to Data entry type of assignment. Like if you want to place some date on a regular inventory list and you need to type the date when ever you update the list, Its pretty much hard right?

Well i just figure out this problem and now you don’t need to write date by your own. All you need to do is to write a Google Script that can fetch the current date in to document. Lets see the Quick demo of Google Docs. File

Her is the sample of inventory list. Now i need to place the date of every product. If there are 50+ products then its very hard to perform that task you can use the plugin to do so.

Insert Date in Using Google Script

Im sharing the step by step guide through which you can insert date in your list using the Google Apps Script.

  • Open the Google Docs file or You can Make a Copy of my file in your Google Drive.
  • Then go to Tools>Script Editor and paste the script given below.

/**
* Function is Developed by Sarmad Gardezi
* 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('Insert Date')
.addItem('today', '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 = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(date);
if (element) {
element.setBold(true);
} else {
DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
}
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}

  • After that Save the file. Now Run the Script. If you are not a GSuite Member then you need to authorize the unverified application.
  • Click here and see you can Accept unverified application.
  • After that go back to the Google Docs file and on the Menu bar you’ll date Insert Date option right after Help.

Now place the cursor where you want to insert the Date, then press Today to insert Current date. You can also use Arrow keys to switch to the next column and my pressing Today to insert the date.

Remember: For every sheet you need to place the script assign to Google Doc sheet. You can’t use one script on multiple sheet to do so you need to Copy and Paste the Script on every sheet you to perform the task. You can Also watch this Video Tutorial to get Complete Guide to use the Add-on.

You can Learn Google Apps Script or Check More Amazing Google Apps Scripts.