【问题标题】:Is it possible to add line breaks before an image in Google Doc?是否可以在 Google Doc 中的图像前添加换行符?
【发布时间】:2020-09-02 05:15:51
【问题描述】:

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

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

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

提前致谢。

【问题讨论】:

    标签: javascript google-apps-script google-api google-docs-api


    【解决方案1】:

    关于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")的脚本。

    参考资料:

    【讨论】:

    • 感谢您的回答。我试过这个,它在图​​像之后而不是之前添加了一个换行符。
    • @Diego 感谢您的回复。我带来的不便表示歉意。不幸的是,我无法复制您的情况。在我的环境中,当将图像作为内联图像放置并运行脚本时,会在图像之前插入换行符。因此,为了正确了解您的情况,您能否提供示例 Google 文档来复制您当前的问题?借此,我想确认一下。你能做到吗?
    • 我实际上让它与您的第一段代码一起运行。谢谢!
    • @Diego 感谢您的回复和测试。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 2014-10-20
    相关资源
    最近更新 更多