我可以将一个文档的内容复制到另一文档的特定位置吗? (Google Apps脚本)

时间:2018-07-09 18:45:37

标签: google-apps-script google-docs

我有一个文档,上面写着“在此处粘贴文件”,我想将另一个文档的内容粘贴(插入,附加,无论需要什么内容)到那个位置,以保留原始文档的格式。

我修改了一些规范代码来进行复制,如果有帮助,请粘贴到此处。

我认为我可能需要拆分原始文档,然后使用我的代码创建包含以下内容的文档

原始1部分 东西 原文的第二部分

这很丑陋,所以我希望有人知道更好的方法!

 function copyFromTo(fromDocID,toDocID) {
  var toDoc = DocumentApp.openById(toDocID);
  var body = toDoc.getBody();


    var fromDocBody = DocumentApp.openById(fromDocID).getBody(); 
    Logger.log(fromDocBody.getAttributes());
    var totalElements = fromDocBody.getNumChildren();
    var latestElement;
    for( var j = 0; j < totalElements; ++j ) {
      var element = fromDocBody.getChild(j).copy();
      var attributes = fromDocBody.getChild(j).getAttributes();
      // Log attributes for comparison
      Logger.log(attributes);
      Logger.log(element.getAttributes());
      var type = element.getType(); 
      if (type == DocumentApp.ElementType.PARAGRAPH) {
        if (element.asParagraph().getNumChildren() != 0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
          var pictattr = element.asParagraph().getChild(0).asInlineImage().getAttributes();
          var blob = element.asParagraph().getChild(0).asInlineImage().getBlob();
          // Image attributes, e.g. size, do not survive the copy, and need to be applied separately
          latestElement = body.appendImage(blob);
          latestElement.setAttributes(clean(pictattr));
        }
        else latestElement = body.appendParagraph(element);
      }
      else if( type == DocumentApp.ElementType.TABLE )
        latestElement = body.appendTable(element);
      else if( type == DocumentApp.ElementType.LIST_ITEM )
        latestElement = body.appendListItem(element);
      else
        throw new Error("Unsupported element type: "+type);
      // If you find that element attributes are not coming through, uncomment the following
      // line to explicitly copy the element attributes from the original doc.
      //latestElement.setAttributes(clean(attributes));
    }
  }



/**
 * Remove null attributes in style object, obtained by call to
 * .getAttributes().
 * https://code.google.com/p/google-apps-script-issues/issues/detail?id=2899
 */
function clean(style) {
  for (var attr in style) {
    if (style[attr] == null) delete style[attr];
  }
  return style;
}

1 个答案:

答案 0 :(得分:1)

为此,您需要遍历“ toDoc”的每一段,直到找到所需的文本为止。然后,找出哪个元素是父元素(可以是正文或表)以及要替换的段落的相对索引。

/*This function receives the paragraph element that will replace the 
  placeholder text 'Paste STUFF here' and uses the current ActiveDocument as the destination*/
function copyIntoPaceholder(copiedParagraph) {
  var toDoc = DocumentApp.getActiveDocument();
  var body = toDoc.getBody();
  var paragraphs = body.getParagraphs();
  var paragraph;
  for(var p = 0; p <paragraphs.length; p++) { //iterate over every paragraph
    paragraph = paragraphs[p];
    if(paragraph.getText() === "Paste STUFF here") { //finds the text to be replaced
      var container = paragraph.getParent(); //finds the parent of the paragraph to be replaced
      var paragraphIndex = container.getChildIndex(paragraph) //gets the index of the paragraph to be replaced in its parent.
      paragraph.removeFromParent() //deletes the placeholder paragraph from the document
      container.insertParagraph(paragraphIndex, copiedParagraph) //insert the new paragraph at the desired location
      break
    }
  }
}