3

I am working on a project that requires text to be aligned vertically in a text box on a google slide. I have the following function that aligns the text horizontally to the left (START) position of the box. How can i set the box to also align vertically to the center?

function insertHazardTxt (mySlide, txtStats, txtWording) {
  var shape = mySlide.insertShape(SlidesApp.ShapeType.TEXT_BOX, txtStats.left, txtStats.top, txtStats.width, txtStats.height);
  var textRange=shape.getText();
  var paragraphs=textRange.getParagraphs();
  var insertedText = textRange.appendText(txtWording);
  insertedText.getTextStyle()
    .setFontFamily("Libre Franklin")
    .setFontSize(txtStats.size)
    .setBold(true) 
  . setForegroundColor(255,255,255)
  paragraphs[0].getRange().getParagraphStyle().setParagraphAlignment(SlidesApp.ParagraphAlignment.START);
}
Kos
  • 4,890
  • 9
  • 38
  • 42

1 Answers1

2

I believe your goal is as follows.

  • You want to set the vertical alignment of the internal text in a shape on Google Slides using Google Apps Script.

In this case, how about using setContentAlignment? When this is reflected in your script, it becomes as follows.

From:

var textRange=shape.getText();

To:

var textRange = shape.setContentAlignment(SlidesApp.ContentAlignment.MIDDLE).getText();
  • In this modification, the vertical alignment of the internal text in the shape is the center. In the current stage, there are TOP, MIDDLE, and BOTTOM.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @Brian Planz Thank you for replying and testing it. I'm glad your issue was resolved. Thank you, too. – Tanaike Oct 02 '22 at 22:50