使用应用脚本检测Google文档中的PAGE_BREAK元素

问题描述 投票:3回答:1

Problem statement:

我有一个包含N页的谷歌文档。要求是将每个页面转换为单独的Google文档。

Solution tried:

尝试通过解析正文来查找PAGE_BREAK元素,并在检测到PAGE_BREAK时创建范围,复制内容,然后创建新的Google文档。

Issue:

创建了一个示例文档,其中第1页上有一行文本,第2页上有一行文本。解析文档时,无法检测到PAGE_BREAK元素。我期待内容流向第2页时,应该有PAGE_BREAK,在这种情况下应该是PARAGRAPH Element的子项。以下是我尝试过的Google应用脚本代码段示例:

var activeDocument = DocumentApp.getActiveDocument();
var body = activeDocument.getBody();

function resetDoc() {
  body.clear(); 
  // When PAGE BREAK is added via script, I am able to detect the PAGE_BREAK element using findElement method only.
  // body.appendParagraph("Page 1");
  // body.appendPageBreak();
  // body.appendParagraph("Page 2");
}


function init() {
  const tree = extractTree(body);
  Logger.log(tree);
}


function extractTree(element) {
  const node = {
    element: element,
  };
  if (element.getNumChildren) {
    var numChildren = element.getNumChildren();

    var children = [];

    for (var i = 0; i < numChildren; i++) {
      var child = element.getChild(i);
      var found = findBreak(element);
      if(found)
      {
        Logger.log("Found page break at" + i );
      }
      var childNode = extractTree(child);
      Logger.log(child.getType());
      children.push(childNode);
    }

    node["children"] = children;
  }

  return node;
};


function findBreak(element) {
  var searchType = DocumentApp.ElementType.PAGE_BREAK;
  var breakElement = body.findElement(searchType);
  if(breakElement) {
    Logger.log("Found page break");
    return true;
  } else {
    Logger.log("No page break");
    return false;
  }
}

关于如何解决这个问题的任何建议。

日志:

[19-04-12 15:46:32:636 IST] TEXT
[19-04-12 15:46:32:637 IST] PARAGRAPH
[19-04-12 15:46:32:638 IST] PARAGRAPH
[19-04-12 15:46:32:640 IST] PARAGRAPH
[19-04-12 15:46:32:642 IST] PARAGRAPH
[19-04-12 15:46:32:643 IST] PARAGRAPH
[19-04-12 15:46:32:645 IST] PARAGRAPH
[19-04-12 15:46:32:647 IST] PARAGRAPH
[19-04-12 15:46:32:648 IST] PARAGRAPH
[19-04-12 15:46:32:650 IST] PARAGRAPH
[19-04-12 15:46:32:651 IST] PARAGRAPH
[19-04-12 15:46:32:653 IST] PARAGRAPH
[19-04-12 15:46:32:655 IST] PARAGRAPH
[19-04-12 15:46:32:656 IST] PARAGRAPH
[19-04-12 15:46:32:658 IST] PARAGRAPH
[19-04-12 15:46:32:660 IST] PARAGRAPH
[19-04-12 15:46:32:662 IST] PARAGRAPH
[19-04-12 15:46:32:663 IST] PARAGRAPH
[19-04-12 15:46:32:665 IST] PARAGRAPH
[19-04-12 15:46:32:666 IST] PARAGRAPH
[19-04-12 15:46:32:668 IST] PARAGRAPH
[19-04-12 15:46:32:670 IST] PARAGRAPH
[19-04-12 15:46:32:671 IST] PARAGRAPH
[19-04-12 15:46:32:673 IST] PARAGRAPH
[19-04-12 15:46:32:675 IST] PARAGRAPH
[19-04-12 15:46:32:676 IST] PARAGRAPH
[19-04-12 15:46:32:678 IST] PARAGRAPH
[19-04-12 15:46:32:680 IST] PARAGRAPH
[19-04-12 15:46:32:682 IST] PARAGRAPH
[19-04-12 15:46:32:684 IST] PARAGRAPH
[19-04-12 15:46:32:685 IST] PARAGRAPH
[19-04-12 15:46:32:687 IST] PARAGRAPH
[19-04-12 15:46:32:689 IST] PARAGRAPH
[19-04-12 15:46:32:690 IST] PARAGRAPH
[19-04-12 15:46:32:692 IST] PARAGRAPH
[19-04-12 15:46:32:693 IST] PARAGRAPH
[19-04-12 15:46:32:695 IST] PARAGRAPH
[19-04-12 15:46:32:697 IST] PARAGRAPH
[19-04-12 15:46:32:699 IST] PARAGRAPH
[19-04-12 15:46:32:701 IST] PARAGRAPH
[19-04-12 15:46:32:702 IST] PARAGRAPH
[19-04-12 15:46:32:704 IST] PARAGRAPH
[19-04-12 15:46:32:705 IST] PARAGRAPH
[19-04-12 15:46:32:706 IST] PARAGRAPH
[19-04-12 15:46:32:708 IST] TEXT
[19-04-12 15:46:32:709 IST] PARAGRAPH
[19-04-12 15:46:32:710 IST] PARAGRAPH
[19-04-12 15:46:32:711 IST] PARAGRAPH
[19-04-12 15:46:32:712 IST] {children=[{children=[{element=Text}], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}, {children=[{element=Text}], element=Paragraph}, {children=[], element=Paragraph}, {children=[], element=Paragraph}], element=DocumentBodySection}

[19-04-12 15:46:32:706 IST] PARAGRAPH应该是PAGE_BREAK,但它是一个PARAGRAPH。

示例Google文档: https://docs.google.com/document/d/1bs_Jcfs-n1VEx65Ew5buBpsf_JCHgX0A7NHYIY8mAqw/edit?usp=sharing

参考链接: 1. Google app脚本文档 https://developers.google.com/apps-script/reference/document/page-break

javascript google-apps-script google-docs google-docs-api add-on
1个回答
0
投票

首先,我不确定是否正确理解任务,因为一般情况下有N个可视页面并不意味着有N-1个显式分页符。我建议您只想使用显式分页符,因为您已经尝试过查找它们。

在这种情况下,用于复制文档片段的最有用的单元(对象)是Paragraph。以下函数获取所有文档段落并检查每个段落以包含PAGE_BREAK元素。如果找到PAGE_BREAK,则表示一页的结尾和另一页的开头。当然,我们此时应该创建一个新的目标文档以继续复制。

function copyPartsByPageBreaks() {
  var activeDoc = DocumentApp.getActiveDocument();
  var pars = activeDoc.getBody().getParagraphs();
  var pageIndex = 0;
  var targetBody = DocumentApp.create('PageBreak.' + pageIndex).getBody();
  while (pars.length > 0) {
    var p = pars.shift();
    targetBody.appendParagraph(p.copy());
    if (p.findElement(DocumentApp.ElementType.PAGE_BREAK) != null) {
      pageIndex++;  // Prepare a new target place for coping
      targetBody = DocumentApp.create('PageBreak.' + pageIndex).getBody();
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.