是否可以在 Google Doc 中的图像前添加换行符?

Is it possible to add line breaks before an image in Google Doc?

当前正在检索这样的图像:

var body = DocumentApp.getActiveDocument().getBody();
  
var styles = {};
styles[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
  
var imgs = body.getImages();
imgs[0].getParent().setAttributes(styles);

我可以添加一些属性来在图像之前放置换行符吗?或者获取图像索引并添加换行符。

提前致谢。

关于Is there some attribute I can add to place line breaks before image?,不幸的是,我找不到直接实现您的目标的属性。所以在这种情况下,我想提议maybe get the index of image and add line breaks.。当这反映到脚本中时,它变成如下。

示例脚本:

var body = DocumentApp.getActiveDocument().getBody();
var image = body.getImages()[0];
var index = body.getChildIndex(image.getParent());
body.insertParagraph(index, "\n")
  • 当您 运行 脚本时,会在 Google 文档正文的第一个图像之前插入一个换行符。

注:

  • 比如在inline image后面加换行时,可以使用DocumentApp.getActiveDocument().getBody().getImages()[0].getParent().asParagraph().appendText("\n").
  • 的脚本

参考文献: