TransWikia.com

Shortcut to insert current date in Google Docs

Web Applications Asked on November 24, 2021

For example, in MS Word I can insert the current date by typing current year and dash

2018- // ... and current date appears automatically, like 2018-05-20 Sun

Is there something similar in Google Docs?

3 Answers

It’s possible to insert todays date through a macro.

Open your Google Document and under Tools select Script editor. This opens Google's script editor where it’s possible to create macros for Google Documents.

Paste this script and save it as Date Macro or something: (also available here)

/**
 * 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.
 *
 * 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 = Utilities.formatDate(new Date(), "IST", "yyyy MMM 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.');
  }
}

Now refresh or reopen your document and a new menu item appears: Utilities. Under this menu an item appears called Insert Date. Click that to insert todays date at your cursor position.

To change the format of the date you need to change the “format” used in the script. The format can contain the following characters: yyyy-MM-dd'T'HH:mm:ss'Z'

To clarify, this script merely inserts today's date at the cursor location for the day you execute the utility. That's not precisely the same as the =today() function in Google Sheets, which updates the date to the current date whenever you open the spreadsheet. However, this script will save you the trouble of looking up the date and typing it on the day that you execute the script.

Source: https://webapps.stackexchange.com/a/58981/216802

Can someone mark this question as duplicate, I don't have enough reputaion points as of now.

Answered by vaibhav gupta on November 24, 2021

AFAIK the referred Microsoft Word feature is called AutoText. At this time Google Documents doesn't include an equivalent feature but it could be added something similar by using Google Apps Script or a add-on but it could imply to have a sidebar open since Google Apps Script doesn't include an on edit trigger for Google Documents.

References

Answered by Rubén on November 24, 2021

Google Doc has a place for custom user scripts .To insert the script, open your Google Doc file, go to the Tools -> Script Editor, replace the code with this function:

function onOpen() {
  var doc = DocumentApp.getActiveDocument();
  var footer = doc.getFooter();  //gets the footer
  footer.clear();  //clears all data in footer

  //Get date
  var date = new Date();
  var month = date.getMonth()+1;
  var day = date.getDate();
  var year = date.getFullYear();
  var hour = date.getHours()+1;
  var minute = date.getMinutes()+1;

  footer.appendParagraph(month+'/'+day+'/'+year);  //adds date to footer
 }

If you want to insert current date in footer or header , you can use the script in Google Docs, It clears the footer and inserts current date each time you open the file. for modifying the header you can use this instead of last line:

header.editAsText().replaceText(THE_PATTERN, DATE);

for similar purposes, you can google this solution and find the exact code you want or try modifying this code. I found this useful documentation in Google site:

https://developers.google.com/apps-script/reference/document/text

Class Text: An element representing a rich text region. All text in a Document is contained within Text elements. A Text element can be contained within an Equation, EquationFunction, ListItem, or Paragraph, but cannot itself contain any other element. For more information on document structure, see the guide to extending Google Docs.

var body = DocumentApp.getActiveDocument().getBody();

// Use editAsText to obtain a single text element containing
// all the characters in the document.
var text = body.editAsText();

// Insert text at the beginning of the document.
text.insertText(0, 'Inserted text.n');

// Insert text at the end of the document.
text.appendText('nAppended text.');

// Make the first half of the document blue.
text.setForegroundColor(0, text.getText().length / 2, '#00FFFF');

Answered by Homa Pour Mohammadi on November 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP