I am facing challenges in substituting multi-line text sections within a Google Document through Apps Script. Despite researching and trying out various solutions, I’m still stuck. I have multiple sections of text in my document and I identify these sections using specific tag labels. For instance, I want to replace the block that appears between “||TAG_LABEL” and “||”. Here’s an example block:
||TAG_LABEL
Sample text example goes here.
More details to follow.
||
My understanding is that I need to provide a string for the replaceText method. I have verified that the value for textToReplace is a string, yet it isn’t functioning as expected and the text remains unchanged.
Here’s the code I’ve been working with:
function replaceTextInDocument() { const doc = DocumentApp.getActiveDocument(); const body = doc.getBody(); const label = “TAG_LABEL”; const pattern = “\|{2} (.?)" + label + "[\s\S]?\|{2}”; body.replaceText(pattern, “”); }
I appreciate any assistance you can provide!