如何将图像放在右边距?

How do I place an image at the right margin?

我正在尝试使用脚本将图像放在 google 文档的右侧边缘。我得到了左偏移量的计算结果:

body.getPageWidth()-body.getMarginRight()-body.getMarginLeft()-myImage.imageWidth

这始终将图像放在边距内 5/8 英寸处。我想这可能与环绕图像周围的自动边距有关?不过,我在 PositionedImage 文档中找不到控制它的方法。

我在计算左偏移量时是否遗漏了额外的元素?还有什么我不允许的吗?我可以使用其他方法来放置图像吗?

图像是否已经是 PositionedImage - 是否已移至 PositionedImage?或者有选择吗?如果是InlineImage; Then you can getParent() and setAttributes()LEFT_TO_RIGHT。它将重新定位到右边距。

function imgRight() {
  var doc = DocumentApp.getActiveDocument()
  var body = doc.getBody()
  // Get the first inline image in the doc.body
  var img_container = body.getImages()[0].getParent()

  // Set the ContainerElement to right_to_left
  img_container.setAttributes({LEFT_TO_RIGHT:false})
}

编辑和更新: PositionedImage.LeftOffset 的计算问题是 image.getWidth() returns the width in pixels and the margins and page size in points. Convert the img pixels to points 和偏移计算看起来像:

var body = DocumentApp.getActiveDocument().getBody();
var body_attributes = body.getAttributes();

var img = body.getParagraphs()[0].getPositionedImages()[0];
var img_width = img.getWidth();
var img_in_points = img_width * 72/96;
var current_img_offset = img.getLeftOffset();

var offset = body_attributes.PAGE_WIDTH - body_attributes.MARGIN_RIGHT - body_attributes.MARGIN_LEFT  - img_in_points + current_img_offset;