google-apps-scriptpositiongoogle-docsinline-images

How to change InlineImages in Google Docs to PositionedImage?


The new PositionedImage class in Google Apps Script brings up a lot of questions.

  1. How does it work in relation to the current In line/Wrap text/Break text options for images? I assume its not an InlineImage, so does it wrap text?

  2. Does the image retain its hyperlink? Currently, changing an image from inline to wrapped/break removes its hyperlink.

  3. Let's say I wanted to shift a current InlineImage one pixel down from its current position. How would I get the current offset of the InlineImage, convert it to a PositionedImage, then shift to the new offset? How will surrounding text change? Or can it just be shifted relative to the original, without having to get the actual offset?

EDIT: Apparently, there is virtually no way to get coordinates of InlineImages, so the third question will probably be null until GAS changes.


Solution

  • I've just updated an older answer with new info about PositionedImage - it might help answer your questions.

    How does it (PositionedImage) work in relation to the current In line/Wrap text/Break text options for images? I assume its not an InlineImage, so does it wrap text?

    A PositionedImage is indeed not an InlineImage. It is anchored in a paragraph, and the paragraph text can flow around the image in different ways, depending on the PositionedLayout setting for the image. All the options available through the UI are supported programmatically.

    Does the image retain its hyperlink (if changing an InlineImage to a PositionedImage)? Currently, changing an image from inline to wrapped/break (via the UI) removes its hyperlink.

    There is no way to directly and programmatically change an InlineImage to a PositionedImage. Instead, you need to copy the InlineImage blob, delete the InlineImage, and then insert a new PositionedImage into a nearby or new paragraph. (My guess is that this is what happens under the covers when you make the same change via the UI.) Any hyperlink will be lost, and there is no way to attach a hyperlink to a PositionedImage programmatically (yet).

    Let's say I wanted to shift a current InlineImage one pixel down from its current position. How would I get the current offset of the InlineImage, convert it to a PositionedImage, then shift to the new offset? How will surrounding text change? Or can it just be shifted relative to the original, without having to get the actual offset?

    As described above, but the positioning could be tricky. Remember that the InlineImage was a child element of a Body, FooterSection, HeaderSection or TableCell element. The new PositionedImage will be anchored in a Paragraph or ListItem. Immediately, this implies some movement of the image, and can be affected by fonts, line spacing, margins, etc. Further, the offsets that you control are relative to the top-left corner of the anchor element - so we're back to the trickiness of determining where THAT is, relative to where the former InlineImage was.