Home > OS >  How to align text vertically in a text box using google app script for slides
How to align text vertically in a text box using google app script for slides

Time:10-03

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);
}

CodePudding user response:

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:

  • Related