Home > Mobile >  Google Docs Apps Script Replace Dynamic Text Field onOpen
Google Docs Apps Script Replace Dynamic Text Field onOpen

Time:08-25

I am getting into Google Apps Script to automate a few basic tasks.

I have a document in which I would like keep the following text up to date within the body of the document, replacing the old text with the new text, onOpen(e):

Revision ID 256 - last updated Wed Aug 24 2022 14:51.

I am comfortable using replaceText() to replace predictable "{{placeholders}}"

But of course once the placeholder is replaced, it can no longer be replaced the next time the document is opened.

The text I want to replace will be different each time.

I wondered if there was a way to 'name' or identify a table or text field, or get some sort of immutable ID for an element, therefore being able to reliably replace the entire text of the same part of the document each time.

Does anyone have any hints or ideas on this?

Obviously the Google Docs interface provides this information to the user anyway, but the target is actually a PDF export of the document, and this will allow the reader to check they have the most up to date copy.

Thank you.

CodePudding user response:

I believe your goal is as follows.

  • You want to update the text on Google Document without using the placeholder.
  • You want to achieve this using Google Apps Script.

In this case, how about using the named range? But, I had thought that when I tried to use the named range on Google Document, it might be a bit difficult. So, I have created a Google Apps Script library. enter image description here

Note:

  • When you want to update the text of the named range when the Google Document is opened, please modify it as follows.

    • From

        function updateTextOfNamedRange() {
      
    • To

        function onOpen() {
      

References:

  • Related