Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
983 views
in Technique[技术] by (71.8m points)

google apps script - How do you tell whether text fits within its bounding shape?

I am doing a merge of data from a google spreadsheet to a slides presentation using placeholder shapes and apps script, something along the lines of this example.

I have the merge working well. The first issue I am having though is that the text is not bound by the shape that contains it. If the text does not fit within the bounding shape it simply overflows:

text overflowing boundaries of textbox

That wouldn't be such a problem if I could then somehow tell programmatically that the text has overflowed. As far as I can tell though I can only get the height of the bounding shape. Which doesn't change even though it now "contains" text that covers a much greater height on the slide. I don't seem to be able to get the height of the actual text.

Is there any way to tell what the height of the textrange is, or that it has overflowed, so that I can then either adjust some feature of the text (the font size, or the length of the actual text, etc) so that the text is bound by the textbox? Or at least alert the user that the text has overflowed.

The PowerPoint equivalent of this would be textrange.boundheight if that helps describe the property I am looking for, with a similar property I would then hopefully be able to implement something similar to the AutoSize feature.

The merge of text to a slide without some way to fit the text to the placeholder shapes doesn't seem that useful to me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

enter image description here

As of January 2021, you can set auto fit setting in Google Slides editor.

When you click inside shape, you can see new Auto-fit setting, set to "Do not autofit" by default: enter image description here

You can change it to "Shrink text on overflow":

enter image description here

Or another option: "Resize shape to fit text":

enter image description here

Update (July 2021): there new autofit properties of Shape element:

autofitType

The autofit type of the shape. If the autofit type is AUTOFIT_TYPE_UNSPECIFIED, the autofit type is inherited from a parent placeholder if it exists. The field is automatically set to NONE if a request is made that might affect text fitting within its bounding text box. In this case the fontScale is applied to the fontSize and the lineSpacingReduction is applied to the lineSpacing. Both properties are also reset to default values.

fontScale - readonly

The font scale applied to the shape. For shapes with autofitType NONE or SHAPE_AUTOFIT, this value is the default value of 1. For TEXT_AUTOFIT, this value multiplied by the fontSize gives the font size that is rendered in the editor.

lineSpacingReduction - readonly

The line spacing reduction applied to the shape. For shapes with autofitType NONE or SHAPE_AUTOFIT, this value is the default value of 0. For TEXT_AUTOFIT, this value subtracted from the lineSpacing gives the line spacing that is rendered in the editor.

P.S. autofitType may be not marked as readonly in documentation, but there seems to be a bug which currently prevents an easy way to set it (to any other value than NONE), see https://issuetracker.google.com/issues/189153562#comment5 and https://developers.google.com/apps-script/reference/slides/autofit (just getAutofitType() available)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...