0

Im trying to have a function called after any real time edits has occurred on the google doc. The final objective is to have all subsequent text typed in the document follow a particular format regardless of the contextual format of the new position(s) of the cursor. The following code does apply formatting to the element where the cursor is at when the function is called, but does not do so when cursor position is changed unless the function is called again.

function formattext() {
  var doc = DocumentApp.getActiveDocument();

  var style = {};

  style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
  DocumentApp.HorizontalAlignment.RIGHT;
  style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
  style[DocumentApp.Attribute.FONT_SIZE] = 23;
  style[DocumentApp.Attribute.BOLD] = true;

  // Get the element at the cursor position
  var cursor = doc.getCursor();
  var element = cursor.getElement();

  //set format of the text to be aligned right, font size 23, calibri and bolded
  element.setAttributes(style);
}

With a onEdit reserved function name the code should work as I require it to (whenever new text is typed), but onEdit is not available for google docs, only google sheets based on the API.

Thus I need some way to have an onEdit equivalent for google docs that monitors changes in real time.

Attempted workarounds

Installable triggers (API) only has onEdit() available for google spreadsheet not google docs, where

ScriptApp.newTrigger().forDocument().onEdit().create();

does not work but the one below does (note its for spreadsheet but I need for google docs)

ScriptApp.newTrigger().forSpreadsheet().onEdit().create();

Seperately, I tried using while loops so that I can run the function once and hopefully the while block will have the attributes persistently apply wherever the cursor moves. Below is the code I used.

function formattext() {
  var doc = DocumentApp.getActiveDocument();
  var style = {};

  style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
  DocumentApp.HorizontalAlignment.RIGHT;
  style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
  style[DocumentApp.Attribute.FONT_SIZE] = 23;
  style[DocumentApp.Attribute.BOLD] = true;

  var toggle = true;

  while (toggle){
    var cursor = doc.getCursor();
  
    // Get the element at the cursor position
    var element = cursor.getElement();
    element.setAttributes(style);
  }
}

In this case the script only applies the new attributes does not apply unless the script is forcibly stopped. Furthermore, the attributes only apply to the position where the cursor originally was when the function is called. I am not too sure why this is the case but this is less relevant to the question (this just shows workarounds I tried)

I have not tried time based triggers to check changes every so often and execute the function on the changed aspects of the document because it doesnt seem like it would be efficient.

Previously suggested solutions

There was a question many years ago here (Google Docs Add On onEdit), but the answer does not provide a real time update that I require

Other answers also may be outdated (this links to a depreciated API) and not relevant anymore, so I decided to post this question again in hopes that a better solution now exists

  • In your situation, as a workaround, is this thread and my post useful? https://stackoverflow.com/q/70266017 and https://tanaikech.github.io/2021/12/01/pseudo-onedit-trigger-for-google-document-using-google-apps-script/ – Tanaike Dec 18 '22 at 12:53

0 Answers0