4

I'm trying to replace text inside a textbox of a slide while retaining the text formatting of the previous text. Is there a way for this to work using google apps scripts methods?

I tried converting the Google Slide to a PDF then to DOC to get the HTML value so that I can somehow retain the text formatting. While the conversions work, I'm stuck at the part where I have to replace the text within the TextBox while retaining the original text formatting.

So far this is what I have:

function replacePresentationContent(presentationCopyId, slideId, shapeId, content) {
  var presentationCopy = SlidesApp.openById(presentationCopyId);
  var slidesCopy = presentationCopy.getSlides();

  for (var i = 0; i < this.getSlidesCount(presentationCopy); i++) {
    var slideCopy = slidesCopy[i];
    var slidesCopyId = slideCopy.getObjectId();
    var shapesCopy = slideCopy.getShapes();

    if (slidesCopyId === slideId) {
      for (var j = 0; j < shapesCopy.length; j++) {
        if (shapesCopy[j].getObjectId() === shapeId) {
         var textRange = shapesCopy[j].getText();
           textRange.setText(content);
        }
      }
    }
  }
}

2 Answers2

3

You can do this by manipulating the text of the shape in the following way:

var shape = slide.getShapes()[0]; //Change this to get the Shape you want
shape.getText().setText("The new text you want here, but with the same formatting!");

Hope this helps!

ZektorH
  • 2,680
  • 1
  • 7
  • 20
  • Thanks for the answer, but I already tried this. This only gets the text formatting of the first word and doesn't retain the other formatting of the other words in the same textbox. – Justin Amargo Oct 14 '19 at 09:38
3

so I found that you can use the function getRuns() to isolate the formatted text. Hope this helps.