1

How do I set Text Alignment Center in a Shape InsertTextRequest using Google Slides API?

    requests.add(new Request()
            .setCreateShape(new CreateShapeRequest()
                    .setObjectId(elementRandomString)
                    .setShapeType("RECTANGLE")
                    .setElementProperties(new PageElementProperties()
                            .setPageObjectId(this.pageObjectId)
                            .setSize(new Size()
                                    .setHeight(ptHeight)
                                    .setWidth(ptWidth))
                            .setTransform(new AffineTransform()
                                    .setScaleX(1.0)
                                    .setScaleY(1.0)
                                    .setTranslateX(xLocation)
                                    .setTranslateY(yLocation)
                                    .setUnit("PT")))));

    if (shapeModel.textModel != null && shapeModel.textModel.textValue != null) {
        requests.add(new Request()
                .setInsertText(new InsertTextRequest()
                        .setObjectId(elementRandomString)
                        .setText("BOOK")));
    }

The last line, we set the text; want to center Text in the middle.

Center align in rectangle

It was not specified in resources below.

Resources:

https://developers.google.com/slides/how-tos/add-shape

Java Insert Text Request

Kos
  • 4,890
  • 9
  • 38
  • 42
mattsmith5
  • 540
  • 4
  • 29
  • 67

1 Answers1

1

It is not possible to set the alignment using InsertTextRequest. You can update the alignment under paragraph styles using the UpdateParagraphStyleRequest message in a call to batchUpdate.

Sample Request Body:

{
  "requests": [
    {
      "updateParagraphStyle": {
        "objectId": "gc9072fb39b_0_5",
        "style": {
          "alignment": "CENTER"
        },
        "fields": "alignment"
      }
    }
  ]
}

Applicable Java Code:

requests.add(new Request()
        .setUpdateParagraphStyle(new UpdateParagraphStyleRequest()
                .setObjectId(elementRandomString)
                .setFields("*")
                .setStyle(new ParagraphStyle()
                        .setAlignment("CENTER"))));

Output:

Before:

enter image description here

After:

enter image description here

References:

mattsmith5
  • 540
  • 4
  • 29
  • 67
Ron M
  • 5,791
  • 1
  • 4
  • 16
  • Glad to hear that. Thank you for updating it with a sample code as well. I wasn't able to provide one due to lack of environment platform available on my end :) – Ron M Mar 22 '21 at 22:04
  • do you possible know the answer for this also? https://stackoverflow.com/questions/66795794/google-slides-api-able-to-use-hexadecimal-to-set-colors – mattsmith5 Mar 25 '21 at 08:39