Home > front end >  Google Apps Scripts - Copy Inline Drawing from One Document to Another
Google Apps Scripts - Copy Inline Drawing from One Document to Another

Time:12-16

I am trying to copy an entire document from one to another.

I am simply trying to copy an INLINE_DRAWING from one google doc to another but I keep getting this error. If I remove the attempt to append the drawing then there is no errors, but then in my finished document there are no drawings. How do you properly copy inline drawings from one doc to another?

// fromFile & toFile are objects after calling .getBody()
function copyFileContentsFromTo(fromFile,toFile){

    for(var i=0; i<fromFile.getNumChildren();i  ){ //run through the elements of the template doc's Body.
      var child = fromFile.getChild(i);
      switch (fromFile.getChild(i).getType()) { //Deal with the various types of Elements we will encounter and append.
        case DocumentApp.ElementType.PARAGRAPH:
          // Look for child elements within the paragraph. Inline Drawings are children.
          if(child.asParagraph().getNumChildren() !=0 && child.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {
            console.log("Found an Inline Drawing");
            var drawing = child.asParagraph();
            toFile.append(drawing.copy()); // This causes the error!
          }else{
            toFile.appendParagraph(child.copy());
          }
          break;
        case DocumentApp.ElementType.TABLE:
          toFile.appendTable(child.copy());
          break;
        default:
          console.log("Whoops. Did not handle this Element Type when Copy Data from Template to Main: "   fromFile.getChild(i).getType());
          break;
      }
    }
}

I have already search all over and cant find any resources, this is the closest but still yields the error in my screenshot: Google script InlineDrawing class

CodePudding user response:

In your script, it is required to modify as follows.

From:

toFile.append(drawing.copy());

To:

toFile.appendParagraph(drawing.copy());

IMPORTANT:

  • In the current stage, when the paragraph including the inline drawings is copied, please disable V8 runtime. When the above-modified script is used with V8 runtime, an error like Exception: Action not allowed occurs. So please be careful about this. This has already been reported at Google issue tracker. Ref I would ilke to believe this bug can be removed in the future update.

Reference:

  • Related