Google script InlineDrawing class

2019-09-07 17:01发布

问题:

I'm trying to insert InlineDrawing into table cell.

According to documentation you can add InlineDrawing to Paragraph.

so

tr = table.appendTableRow();

var tc1 = tr.appendTableCell();

var tc1.appendParagraph( how to fit new InlineDrawing here? ); 

How to get new instance of inlineDrawing ? How to work with this object ? I can't find any online reference about this.

Thank you for your help.

回答1:

Here you go: (credit: https://gist.github.com/bennettscience/cd51762de17c860d6930)

// Don't forget your global variables up top.
// Search through the page elements. Paragraphs are top-level, which is why I start with those.
if( type == DocumentApp.ElementType.PARAGRAPH ){

  // Look for child elements within the paragraph. Inline Drawings are children.
  if(element.asParagraph().getNumChildren() !=0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {

    // For whatever reason, drawings don't have their own methods in the InlineDrawing class. This bit copies and adds it to the bottom of the doc.
    var drawing = element.asParagraph().copy();
    body.appendParagraph(drawing);
  }
}


回答2:

Yes, I've also noticed Google's documentation is lacking in many areas. You have to use the copy() method to get a copy of it. See an example code snippet here: How do I export InlineDrawing as an image in Document?. In that, he uses child.getParent().copy() to get the paragraph with the InlineDrawing. There's only one other reference I found (http://googlestyle.client.jp/document_services/class_inlinedrawing.html). See the removeFromParent section, where he uses getImages to get a list of handles to images in the document.