I’m working with a Google Docs document that has various content including images and lists. There’s a horizontal line graphic in the document that’s causing problems.
Whenever I run my script to duplicate content from one document to another, it fails with an “Action not allowed” message specifically when it tries to process the line element. I checked the element type and it shows up as a PARAGRAPH but doesn’t have any nested elements inside.
Here’s the script I’m using to transfer the content:
function transferContent() {
var originalDoc = DocumentApp.getActiveDocument().getBody();
var destinationDoc = DocumentApp.openById('1ABC123-sample-document-id');
var elementCount = originalDoc.getNumChildren();
for (var i = 0; i < elementCount; i++) {
var docBody = destinationDoc.getBody();
var currentElement = originalDoc.getChild(i).copy();
var elementType = currentElement.getType();
Logger.log(elementType);
if (elementType == DocumentApp.ElementType.PARAGRAPH) {
docBody.insertParagraph(0, currentElement);
}
else if (elementType == DocumentApp.ElementType.LIST_ITEM) {
docBody.insertListItem(0, currentElement.copy());
}
}
destinationDoc.saveAndClose();
}
Does anyone know how to handle this line element issue or work around the copying restriction?
The horizontal line elements are indeed stored as paragraphs but contain special attributes that restrict copying operations. I ran into this same roadblock about six months ago when migrating content between project documents. The key insight is that these line graphics have internal properties that aren’t accessible through the standard Apps Script API, which triggers the ‘Action not allowed’ error. Rather than fighting the copy restriction, I found success by detecting these elements first and recreating them manually in the destination document. Check the paragraph’s attributes using getAttributes() before copying - line graphics typically have specific styling properties that regular text paragraphs don’t possess. When you encounter one, skip the copy operation entirely and use insertHorizontalRule() at the appropriate position instead. This bypasses the restriction completely while maintaining the visual layout of your original document.
I’ve encountered this exact issue before when working with documents containing horizontal lines or other special formatting elements. The problem is that horizontal line graphics are technically stored as paragraphs but they contain special formatting that Google Apps Script cannot properly duplicate through the standard copy methods.
What worked for me was implementing a try-catch block around the element copying operation and handling these problematic elements separately. Instead of trying to copy the line element directly, I skip it and manually insert a new horizontal line using the insertHorizontalRule method on the destination document body.
You might also want to check if the paragraph has any text content before attempting to copy it. Line graphics typically show up as paragraphs with zero text length, so you can filter them out that way and handle them as special cases. This approach has saved me countless headaches when dealing with complex document structures.
try wrapping your copy operation in a try-catch and skip those tricky elements. i faced similar issues, some paragraphs look fine but fail to copy. also, checking if currentElement.getText().length > 0 can help filter out those odd line graphics.