Google app script - find and replace text in google document

I am using the following code to replace text in google docs:

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

      

Everything works fine, but this time I will need to put the text on top of the image, so basically I put %sample_placeholder%

on Text Box

while inserting the image on Insert > Drawing

. The above code doesn't work anymore.

Any ideas on finding and replacing text in text boxes?

Thanks in advance!


Is there access to this workaround?

Additional information: The text to be placed in the image comes from user input. The image is actually a rectangular tagged label (use for labeling things) with space on it where text can be placed.


The idea is that a Google document with an image and text on the image (let's call the text with a text placeholder) will serve as a template. This Google Docs template is restored by Google Apps Script and should replace the text placeholder with user input and saved as a separate file.

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

+3


source to share


1 answer


Since there is no script available to google pictures, it is not possible to add text above the image. There is one possible workaround you could try.

If there are only a few possible combinations of images and text, one solution is to create an image for each possible combination and place it in a folder. Then you just get the image with the relevant text and paste it.

I've used this before (with 20-30 image combinations). Here are some of the code I used before I put it on top with you:

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. The name of the image must match what is in your ex folder: "image1_Text1.png", "image1_Text2.png", etc. Just make sure your image names are easy to understand without looking at the image.

The hardest part is finding a suitable insertion point for the image, as document structures quickly become complex. To help with this, I wrote a small script utility to get the elements of the document's children in order to 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);}
}

      

While this is a workaround, it allows you to create the effect you want if you have a finite number of potential image and text combinations.

+2


source







All Articles