google-apps-scriptgoogle-docsgoogle-docs-api

Error 'Action Not Allowed' When Copying Content with a Vertical Line in Google Docs


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


Solution

  • 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.

    Modified script:

    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();
    }
    

    Note:

    Reference: