首页 > 解决方案 > 是否可以在 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);

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

提前致谢。

标签: javascriptgoogle-apps-scriptgoogle-apigoogle-docs-api

解决方案


关于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 文档正文中的第一张图片之前插入换行符。

笔记:

  • 例如,在内联图像后添加换行符时,可以使用DocumentApp.getActiveDocument().getBody().getImages()[0].getParent().asParagraph().appendText("\n").

参考:


推荐阅读