在 Cursor 处添加段落或 table 等

Add a paragraph or table etc. at Cursor

我有一个功能可以在活动文档中的光标点处添加一个单独的 google 文档的内容,但我无法使用它。我一直收到 "Element does not contain the specified child element" 异常,然后内容被粘贴到文档底部而不是光标点!

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

  • 您想将对象(段落、表格和列表)从 1MFG06knf__tcwHWdybaBk124Ia_Mb0gBE0Gk8e0URAM 的文档复制到活动文档。
  • 您想将对象复制到活动文档上的光标位置。
  • 您想使用 Google Apps 脚本实现此目的。

如果我的理解是正确的,这个答案怎么样?请将此视为几个可能的答案之一。

修改点:

  • 在您的脚本中,appendParagraphappendTableappendListItem 用于第一个 for 循环。我认为复制的对象放在文件最后的原因是由于这个。
  • var body = docCopy.getBody();可以放在for循环之外。
  • 在你的情况下,我认为当第一个for循环被修改时,第二个for循环就不需要了。

当以上几点反映到你的脚本中,就会变成下面这样。

修改后的脚本:

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);
    }
  }
}
  • 您的脚本中似乎没有使用 DocName

参考文献:

如果我误解了您的问题而这不是您想要的结果,我深表歉意。届时能否提供样例源文件?借此,我想确认一下。