Google Apps脚本:复制页眉/页脚

时间:2013-02-15 10:17:39

标签: google-apps-script

我尝试将我的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;
}

1 个答案:

答案 0 :(得分:0)

在调用getBulkDocument之后,我确实调用了具有固定documentID的函数。但最好在此之前使用它:

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

要复制的完整示例将在我的博客上...