
It is called a text box because it contains text and can be placed anywhere on the page without disturbing the other text, or breaking the flow of the text. A textbox is a drawn box that can be of any orientation and size and contains some text. When you use this script, please select a text and run the script.Today’s article is about how to insert a text box in google docs. When these points are reflected in the script, how about the following sample script? Sample script: In order to replace the selected text, it is required to use getSelection().I think that the reason for your current issue of TypeError: Cannot read properties of null (reading 'insertText') is due to this. In the case of var currentPosition = doc.getCursor() and currentPosition.insertText(generatedText.toString()), the selected text cannot be used.



Get the body of the document and the prompt for the OpenAI API requestĬonst prompt = doc.getSelection().getRangeElements().getElement().asText().getText() Get the active Google Docs document and the user-selected textĬonst doc = DocumentApp.getActiveDocument() Ĭonst userText = doc.getSelection().getRangeElements().getElement().asText().getText() I guess that this is due to the fact that selection is nothing to do with cursor position. TypeError: Cannot read properties of null (reading 'insertText') The script will work fine but I cannot update the current selection, as I get the following error I'm trying to write an app script to replace the current text selection with a new AI generated text, via menu item.
