Add a paragraph or table etc. at Cursor

434 views Asked by At

I have a function for adding the contents of a separate google document at the cursor point within the active document, but I haven't been able to get it to work. I keep getting the "Element does not contain the specified child element" exception, and then the contents gets pasted to the bottom of the document rather than at the cursor point!

function AddTable() {
  //here you need to get document id from url (Example, 1oWyVMa-8fzQ4leCrn2kIk70GT5O9pqsXsT88ZjYE_z8)
  var FileTemplateFileId = "1MFG06knf__tcwHWdybaBk124Ia_Mb0gBE0Gk8e0URAM"; //Browser.inputBox("ID der Serienbriefvorlage (aus Dokumentenlink kopieren):");
  var doc = DocumentApp.openById(FileTemplateFileId);
  var DocName = doc.getName();

  //Create copy of the template document and open it
  var docCopy = DocumentApp.getActiveDocument();
  var totalParagraphs = doc.getBody().getParagraphs(); // get the total number of paragraphs elements
  Logger.log(totalParagraphs);
  var cursor = docCopy.getCursor();
  var totalElements = doc.getNumChildren();
  var elements = [];
  for (var j = 0; j < totalElements; ++j) {
    var body = docCopy.getBody();
    var element = doc.getChild(j).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);
    }
    //    ...add other conditions (headers, footers...
  }
  Logger.log(element.editAsText().getText());
  elements.push(element); // store paragraphs in an array
  Logger.log(element.editAsText().getText());

  for (var el = 0; el < elements.length; el++) {
    var paragraph = elements[el].copy();
    
  var doc = DocumentApp.getActiveDocument();
  var bodys = doc.getBody();
  var cursor = doc.getCursor();
  var element = cursor.getElement();
  var container = element.getParent();
  try {
    var childIndex = body.getChildIndex(container);
    bodys.insertParagraph(childIndex, paragraph);
  } catch (e) {
    DocumentApp.getUi().alert("There was a problem: " + e.message);
  }
  
  }
}

1

There are 1 answers

0
Tanaike On BEST ANSWER
  • You want to copy the objects (paragraphs, tables and lists) from the document of 1MFG06knf__tcwHWdybaBk124Ia_Mb0gBE0Gk8e0URAM to the active Document.
  • You want to copy the objects to the cursor position on the active Document.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification points:

  • In your script, appendParagraph, appendTable and appendListItem are used at the 1st for loop. I think that the reason that the copied objects are put to the last of the document is due to this.
  • var body = docCopy.getBody(); can be put to the out of the for loop.
  • In your case, I think that when the 1st for loop is modified, 2nd for loop is not required.

When above points are reflected to your script, it becomes as follows.

Modified script:

function AddTable() {
  var FileTemplateFileId = "1MFG06knf__tcwHWdybaBk124Ia_Mb0gBE0Gk8e0URAM";
  var doc = DocumentApp.openById(FileTemplateFileId);
  var docCopy = DocumentApp.getActiveDocument();
  var body = docCopy.getBody();
  var cursor = docCopy.getCursor();
  var cursorPos = docCopy.getBody().getChildIndex(cursor.getElement());
  var totalElements = doc.getNumChildren();
  for (var j = 0; j < totalElements; ++j) {
    var element = doc.getChild(j).copy();
    var type = element.getType();
    if (type == DocumentApp.ElementType.PARAGRAPH) {
      body.insertParagraph(cursorPos + j, element);
    } else if (type == DocumentApp.ElementType.TABLE) {
      body.insertTable(cursorPos + j, element);
    } else if (type == DocumentApp.ElementType.LIST_ITEM) {
      body.insertListItem(cursorPos + j, element);
    }
  }
}
  • It seems that DocName is not used in your script.

References:

If I misunderstood your question and this was not the result you want, I apologize. At that time, can you provide the sample source Document? By this, I would like to confirm it.