I'm writing a function that will duplicate the current document by moving every element into a new document -- it's part of a larger project.
However, when there is a footnote in the document, the function fails, with this error.
Service Documents failed while accessing document with id [id of target doc]
Now, I have no problem with the function not being able to transfer the footnote into a new document -- or at least transferring just the text of the footnote. However, this error stops my entire function from running (none of the elements in the doc get transferred over).
Is it possible to ignore footnotes or convert them to text, and allow the function to transfer all other elements into the new document?
Here's a sample document with a footnote
Here's my code:
function duplicateDocument() {
var currentDoc = DocumentApp.getActiveDocument().getBody();
var targetDoc = DocumentApp.create('New Doc');
var totalElements = currentDoc.getNumChildren();
//Goes through each type of element to preserve formatting
for( var index = 0; index < totalElements; ++index ) {
var body = targetDoc.getBody();
var element = currentDoc.getChild(index).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH ){
body.appendParagraph(element);
}
else if( type == DocumentApp.ElementType.TABLE){
body.appendTable(element);
}
else if( type == DocumentApp.ElementType.LIST_ITEM){
body.appendListItem(element);
} else if( type == DocumentApp.ElementType.BOOKMARK ){
body.appendBookmark(element);
} else if( type == DocumentApp.ElementType.INLINE_IMAGE ){
body.appendImage(element);
} else if( type == DocumentApp.ElementType.HORIZONTAL_RULE ){
body.appendHorizontalRule();
} else if( type == DocumentApp.ElementType.PAGE_BREAK ){
body.appendPageBreak();
}
}
}