将具有嵌入式图像和文本格式的ListItem从一个文档复制到另一个文档

时间:2020-08-31 21:02:27

标签: google-apps-script google-docs

为了减少重复劳动的时间,我尝试创建一个Google应用程序脚本,该脚本可按顺序附加各种模板文档。执行此方法的方法是使用Google DocumentApp将多个模板文档中的各种元素附加到一个新文档中。这适用于除列表项之外的所有元素类型。

DocumentApp将列表项从一个GDoc复制到另一个GDoc,但是新项目符号显示时没有可见的字形/项目符号/编号,并且存在各种格式问题。我已尝试按照类似的SO职位的建议手动设置字形类型,但缩进有问题-当您在新项目符号上调整缩进时,它不遵循单击选项卡时的通常移入/移出模式/ shift-tab。简单地使用appendListItem时,不可见的字形结果不一致。

将模板GDocs复制到新创建的GDoc时,不显示字形。当复制到已经存在的GDoc(使用具有未知和不一致的使用历史记录的文档多次测试)时,有时会出现字形。我怀疑问题出在appendListItem()或.copy()上,两者都在脚本中使用:

正在使用的脚本摘录:

thisBody.appendListItem(templateBody.getChild(i).copy());

我已经花费了数小时研究变通办法,并且导致该问题无济于事。看来这是一个尚未解决的已知问题。请指教。我在犯一些错误吗?

恭喜, -J

下面的完整代码:

function appendTemplate(templateID) {
  var thisDoc = DocumentApp.getActiveDocument();
  var thisBody = thisDoc.getBody();
  
  var templateDoc = DocumentApp.openById(templateID); //Pass in id of doc to be used as a template.
  var templateBody = templateDoc.getBody();

  // Insert a Page Break between sections
  thisBody.appendPageBreak();
  
  // Append all body sections
  // Reference: https://stackoverflow.com/questions/54817801/google-docs-script-to-insert-another-document
  for(var i=0; i<templateBody.getNumChildren();i++){ //run through the elements of the template doc's Body.
    switch (templateBody.getChild(i).getType()) { //Deal with the various types of Elements we will encounter and append.
      case DocumentApp.ElementType.PARAGRAPH:
        thisBody.appendParagraph(templateBody.getChild(i).copy());
        break;
      case DocumentApp.ElementType.LIST_ITEM:    
        thisBody.appendListItem(templateBody.getChild(i).copy());
        break;
      case DocumentApp.ElementType.TABLE:
        thisBody.appendTable(templateBody.getChild(i).copy());
        break;
      case DocumentApp.ElementType.INLINE_IMAGE:
        thisBody.appendImage(templateBody.getChild(i).copy());
        break;
    }
  }

0 个答案:

没有答案