With the Google Slides API is there a ways to know the height of the text in a shape?

388 Views Asked by At

I want to have a shape with text where the text fits neatly inside the shape. To do this I need to know how tall the text in a textElement will be inside a given shape, is there a way to do this?

1

There are 1 best solutions below

4
On

Answer:

You can get the information of the font size of text inside a Shape using presentations.get with a field mask.

More Information:

The structure of a Shape with text is as follows (irrelevant fields omitted):

{
  "shapeType": enum (Type),
  "text": {
    "textElements": [
      {
        "textRun": {
          "style": {
            "fontSize": {
              "magnitude": 18,
              "unit": "PT"
            },
          }
        },
      }
    ],
  },
  ...
}

You can get this with the field mask:

slides/pageElements/shape/text/textElements/textRun/style/fontSize

Example Request:

slides.presentations.get({
  "presentationId": "1x1iiFW0TbP3yPXRldYogXLEGT-tYfa79a2X8ZDIq8tU",
  "fields": "slides/pageElements/shape/text/textElements/textRun/style/fontSize"
});

Note: The API does not return if the text wraps across multiple lines or not. This isn't retrievable due to the nature of font sizes. If the font you were using is a monospace font, it may however be possible to work this out based on the width of the Shape and the width of the characters contained within it.

References: