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.
I believe your goal is as follows.
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. Ref This library can manage the named range on Google Document.
When this library is used, the sample script for achieving your goal is as follows.
Please copy and paste the following script to the script editor of Google Document. And, please install the library.
const name = "sampleName"; // Sample name of named range.
const getAllNamedRanges_ = doc => {
const obj = DocNamedRangeApp.getActiveDocument(doc).getAllNamedRanges();
const ar = Object.entries(obj);
if (ar.length == 0) return null;
return ar.reduce((o, [k, v]) => (o[v.name] = v.rangeElements, o), {});
}
// 1. First, set named range to the selected cotent.
function setNamedRange() {
const doc = DocumentApp.getActiveDocument();
DocNamedRangeApp.getActiveDocument(doc).setNamedRangeFromSelection(name);
}
// 2. When you want to update the text of the named range, please use this function.
function updateTextOfNamedRange() {
const upadteText = "###"; // Please set the upate text.
const doc = DocumentApp.getActiveDocument();
const obj = getAllNamedRanges_(doc);
if (!obj[name]) throw new Error("Please set named range.");
obj[name][0].getElement().asText().replaceText(".*", upadteText);
}
setNamedRange()
. By this, the selected text is set as the named range.updateTextOfNamedRange()
. By this, the text of the named range is updated.When this script is used, the following sample situation is obtained.
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() {