I have a Google Docs file (link: Google Docs) that contains images and list items. One of the images is a vertical line.
When I try to copy the content of this Google Doc into a new document, I encounter an error that says, "Action not allowed" whenever the vertical line element is processed. Upon inspecting the element, it appears to be a PARAGRAPH element, and it doesn't contain any child elements.
Any insights or solutions to resolve this issue would be greatly appreciated. The code i used to copy over the docs is as follows:
function myFunction() {
var sourceDoc = DocumentApp.getActiveDocument().getBody();
var targetDoc = DocumentApp.openById('1sZm8Z-p4_x2JBmTJZurKvDjWq8hmDi5YmxS68ld6atY');
var totalElements = sourceDoc.getNumChildren();
for (var j = 0; j < totalElements; j++) { //Reversed the loop to make sure paragraphs/lists are still in order when inserted to the target sheet.
var body = targetDoc.getBody()
var element = sourceDoc.getChild(j).copy();
var type = element.getType();
Logger.log(type);
if (type == DocumentApp.ElementType.PARAGRAPH) {
body.insertParagraph(0, element); //Always insert at the top
}
else if (type == DocumentApp.ElementType.LIST_ITEM) {
body.insertListItem(0, element.copy()); //Always insert at the top
}
}
targetDoc.saveAndClose()
}
Any help is appreciated
I guessed that when I saw your provided Document, in your situation, the positioned drawings (the vertical lines) might be the reason for your current issue. If you want to run your showing script without the error, it is required to skip the positioned drawings. Because in the current stage, the positioned drawings cannot be managed by Document service (DocumentApp) and Docs API.
When this is reflected in your showing script, it becomes as follows.
Before you use this script, please enable Docs API at Advanced Google services. Ref Because in the current stage, the positioned drawings can be detected by only Docs API.
function myFunction() {
var srcDoc = DocumentApp.getActiveDocument();
var sourceDoc = srcDoc.getBody();
var obj = Docs.Documents.get(srcDoc.getId());
var targetDoc = DocumentApp.openById('1sZm8Z-p4_x2JBmTJZurKvDjWq8hmDi5YmxS68ld6atY');
var totalElements = sourceDoc.getNumChildren();
for (var j = 0; j < totalElements; j++) {
var body = targetDoc.getBody()
var element = sourceDoc.getChild(j).copy();
var type = element.getType();
Logger.log(type);
if (type == DocumentApp.ElementType.PARAGRAPH && !obj.body.content[j + 1].paragraph.hasOwnProperty("positionedObjectIds")) {
const p = obj.body.content[j + 1].paragraph;
body.insertParagraph(0, (element.asParagraph().getPositionedImages().length == 0 && p.hasOwnProperty("positionedObjectIds")) ? p.elements.map(e => e.textRun.content).join("") : element);
} else if (type == DocumentApp.ElementType.LIST_ITEM) {
body.insertListItem(0, element);
}
}
targetDoc.saveAndClose();
}
In your script, each element is inserted by body.insertParagraph(0, element)
and body.insertListItem(0, element)
. In this case, the content is put in reverse order. From a comment Always insert at the top
in your showing script, I guessed that this might be your expected result. But, if you want to put each element in the same order from the top of the document, how about the following modification? In this modification, the copied content is put in order from the top of the document.
function myFunction3() {
var srcDoc = DocumentApp.getActiveDocument();
var sourceDoc = srcDoc.getBody();
var obj = Docs.Documents.get(srcDoc.getId());
var targetDoc = DocumentApp.openById('1sZm8Z-p4_x2JBmTJZurKvDjWq8hmDi5YmxS68ld6atY');
var totalElements = sourceDoc.getNumChildren();
for (var j = totalElements - 1; j >= 0; j--) {
var body = targetDoc.getBody()
var element = sourceDoc.getChild(j).copy();
var type = element.getType();
Logger.log(type);
if (type == DocumentApp.ElementType.PARAGRAPH) {
const p = obj.body.content[j + 1].paragraph;
body.insertParagraph(0, (element.asParagraph().getPositionedImages().length == 0 && p.hasOwnProperty("positionedObjectIds")) ? p.elements.map(e => e.textRun.content).join("") : element);
} else if (type == DocumentApp.ElementType.LIST_ITEM) {
body.insertListItem(0, element);
}
}
targetDoc.saveAndClose();
}
If you want to append the content, please modify body.insertParagraph(0,
and body.insertListItem(0,
to body.appendParagraph(
and body.appendListItem(
for the 1st modified script.
This modified script is for your provided Document. When you change the document, this script might not be able to be used. Please be careful about this.