google app script - search and replace text on google document

5.4k Views Asked by At

I'm using the following code for replacing a text on google docs:

var sampleDoc = DocumentApp.openById(copyId),
var docBody = sampleDoc.getBody();  
docBody.replaceText('%sample_placeholder%','some text');

All is working fine but this time I'll be needing to put a text on top of an image, so basically I put the %sample_placeholder% on a Text Box during insert of image on Insert > Drawing. The code above no longer works.

Any ideas on how to search and replace text on text boxes?

Thanks in advance!


Any workaround available for this?

Additional info: The text to be put on the image is coming from a user input. The image is actually a rectangular label tag box (use for labeling things) with space on it's middle where the text can be put into.


The idea is that the Google Document with image and text on image (let's call the text a text placeholder) will serve as a template. This Google Document template is being recopied by Google Apps Script and should replace the text placeholder with the input coming from user and saved as separate file.

It looks something like this (I was given an image created by a designer), and text will be put on the middle of the image. enter image description here

2

There are 2 best solutions below

7
On

Since there is no script access to google drawings, it is impossible to add text above an image. There is one possible workaround which you could try though.

If there are only a few possible image/text combinations, one solution is to create an image for each possible combination and host it in a folder. Then, you simply get the image with the appropriate text and insert it.

I've used this myself before (with a total of 20-30 image combinations). Here's some of the code I've used before pieced together with yours from above:

function getImage(imageName, copyId){
  var sampleDoc = DocumentApp.openById(copyId);
  var docBody = sampleDoc.getBody();
  var imageDest = //pick element which can accept image (table cell, paragraph, etc);

  imageFolder = DriveApp.getFolderById('Your_folder_ID_here');
  image = imageFolder.getFilesByName(imageName).next().getAs('image/png');
  imageDest.insertInlineImage(0, image);
}

Note: Image name should match what is located in your folder ex: "image1_Text1.png", "image1_Text2.png", etc. Just make sure your image names a are easy to understand without having to look at the image.

The most difficult part is finding the appropriate insertion point for the image as document structures get tricky quickly. To help with this I wrote a small utility script to get a document's children elements to help better understand the underlying structure. Feel free to use it:

function getDocElements() {
  var doc = DocumentApp.openById(copyId),
      body = doc.getBody(),
      numElements = doc.getNumChildren(),
      elements = [];

  for (var i = 0; i < numElements; ++i){
      var element = doc.getChild(i),
          type = element.getType();
    Logger.log(i + " : "+type);}
}

Although this is a workaround, it does allow you to create the desired effect as long as you have a finite number of potential image/text combinations.

0
On

Instead of DocumentApp use SlideApp and use the ReplaceAllText function. After that saveAndClose() the document.