简体   繁体   中英

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

Currently retrieving images like this:

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? Or maybe get the index of image and add line breaks.

Thanks in advance.

About Is there some attribute I can add to place line breaks before image? , unfortunately, I couldn't find the attribute for directly achieving your goal. So in this case, I would like to propose maybe get the index of image and add line breaks. . When this is reflected to the script, it becomes as follows.

Sample script:

var body = DocumentApp.getActiveDocument().getBody();
var image = body.getImages()[0];
var index = body.getChildIndex(image.getParent());
body.insertParagraph(index, "\n")
  • When you run the script, a line break is inserted before the 1st image in the body of Google Document.

Note:

  • For example, when the line break is added after the inline image, you can use the script of DocumentApp.getActiveDocument().getBody().getImages()[0].getParent().asParagraph().appendText("\n") .

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM