0

我尝试将 Google 驱动器中文档的元数据复制到另一个。像页面尺寸、查看器、编辑器和页眉/页脚的内容。但是标题不会被复制。有人知道为什么吗?

附加信息:appendElementToDoc 仅搜索附加方法并调用它们,例如 appendParagraph。这对于复制文档的内容非常有用。

/**
 * Copy elements to new doc
 */
function appendElementToDoc(bulk, element) {

  var tName = underscoreToCamelCase(element.getType() + "");  
  Logger.log(tName + "");
  try {
    bulk["append" + tName](element);
  }
  catch(err) {    
    Logger.log(err + "");
  }   
  return bulk;
}

/**
 * Transform typename to functio name
 */
function underscoreToCamelCase(type) {

  type = type.toLowerCase();
  var tName = type.charAt(0).toUpperCase() + type.slice(1);

  var parts = tName.split("_");
  if(parts.length == 2) {
    tName = parts[0] + parts[1].charAt(0).toUpperCase() + parts[1].slice(1);
  }

  return tName;
}

/**
 * Getting bulk document to insert content
 *
 * @param Document
 * @return Document
 */
function getBulkDocument(template) {

  var bulk = DocumentApp.create("Bulk Letter");
  bulk.setMarginBottom(template.getMarginBottom());
  bulk.setMarginLeft(template.getMarginLeft());
  bulk.setMarginRight(template.getMarginRight());
  bulk.setMarginTop(template.getMarginTop());

  bulk.setPageHeight(template.getPageHeight());
  bulk.setPageWidth(template.getPageWidth());

  var header = bulk.addHeader(); 
  for (var i = 0; i < template.getHeader().getNumChildren(); i++) {
      appendElementToDoc(header, template.getHeader().getChild(i).copy());
  }

  var footer = bulk.addFooter();
  for (var i = 0; i < template.getFooter().getNumChildren(); i++) {
      appendElementsToDoc(footer, template.getFooter().getChild(i).copy());
  } 

  var editors = template.getEditors();
  for (var i = 0; i < editors.length; i++) {
    bulk.addEditor(editors[i])
  }

  var viewers = template.getViewers();
  for (var i = 0; i < editors.length; i++) {
    bulk.addViewer(viewers[i])
  }

  //move to folder
  DocsList.getFileById(bulk.getId()).addToFolder(DocsList.getFolder("tmp"));

  return bulk;
}
4

1 回答 1

0

在以正确的方式调用 getBulkDocument 之后,我确实使用固定的 documentID 调用了该函数。但最好在此之前使用它:

if(!bulk.getFooter()) {
  var footer = bulk.addFooter();
} else {
  var footer = bulk.getFooter();
}

完整的例子,复制将在我的博客上......

于 2013-02-15T16:35:02.683 回答