如何删除多页文档中的空白页?(How to delete blank pages in a mult

2019-07-29 18:41发布

编辑:增加了一个答案,因为编辑会一直长(见ANSWER2)

继有关文件前邮局合并我结束了一个工作脚本(感谢恩里克;),但我仍然有一个小问题:最后的“合并”文件包含有时空白页(取决于其他文档内容),我想删除。 我无法找到一个简单的方法来做到这一点。 该脚本是这样的:

function mergeDocs(docIDs) {  // parameter docIDs is an array of Doc IDs
  var baseDocname = DocumentApp.openById(docIDs[0]).getName();// typical name = IMPRESSION_page_07_07-06-2012__20:57
  var modelDoc = DocsList.getFileById(docIDs[0]);
  var newmodelName=baseDocname.substr(0,11)+'multipage'+baseDocname.substring(18);
  var baseDocId = DocsList.copy(modelDoc,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
  var baseDoc = DocumentApp.openById(baseDocId)
  var body = baseDoc.getActiveSection();
//
  for( var i = 0; i < docIDs.length; ++i ) {
    var otherCopy = DocumentApp.openById(docIDs[i]).getActiveSection();
    var totalElements = otherCopy.getNumChildren();
    for( var j = 0; j < totalElements; ++j ) {
      var element = otherCopy.getChild(j).copy();
      var type = element.getType();
      if( type == DocumentApp.ElementType.PARAGRAPH )
        body.appendParagraph(element);
      else if( type == DocumentApp.ElementType.TABLE )
        body.appendTable(element);
      else if( type == DocumentApp.ElementType.LIST_ITEM )
        body.appendListItem(element);
      else
        throw new Error("According to the doc this type couldn't appear in the body: "+type);
    }
     body.appendPageBreak(); // if content length is too short avoids breaking page layout
  }
}

在“分页符”的原因(有时)一个空白页,我知道(!),但它是必要的,以保持一个完美的页面布局(我用打印这个文档标签)。 这里是一个典型的例子链接

Answer 1:

那么哔叽,我不认为有什么的API来告诉元素属于哪个页。 因此,解决这将是棘手的:)

右蝙蝠的,我认为一个“内部”的办法。 我的意思是,你知道哪个页面给你的麻烦。 如果它始终是相同的(例如你有标签固定数量的),你可以只循环计数分页符并取下坏。

但是,如果这是没有可能的,这是我的猜想,至少你知道你的布局。 你可以测试一下,看看有多少标签适合页面准确,再算上你的标签,这样,当它发生时,你跳过追加分页。 这看起来像一个更好的解决方案。

话又说回来,这取决于你的布局,这也许是不可能的或太困难。 所以,我能想到的最后一件事是检查文档DOM,看是否有特定的模式发生在一个分页是单独一个页面上。 由于这是一种奇怪的,我猜测谷歌文档可能会自动将这个网页上的空段落,所以它不是“无子女”,或类似的东西,甚至是一个财产,我不知道。 我所知道的是,这将需要的努力量好,做调查,以深入理解文档DOM是如何工作的。 如果你不这样做,我可能不得不在未来为我工作的文件作为像这样的很多模板。 当我做我会更新我的答案,如果你没有我以前做过:)



Answer 2:

我修改了脚本,以便它给了我不断取得成果,并写了一个分析函数来获取文档的结构。 我发现它可能是一个更好的主意,把这一切的答案,而不是编辑我的第一个问题,因为这是一个有点长。 1°简化脚本:

function mergeDocs(docIDs) {
  var baseDocname = DocumentApp.openById(docIDs[0]).getName();// typical name = IMPRESSION_page_07_07-06-2012__20:57
  var modelDoc = DocsList.getFileById(docIDs[0]);
  var newmodelName=baseDocname.substr(0,11)+'multipage'+baseDocname.substring(18);
  var baseDocId = DocsList.copy(modelDoc,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
  var baseDoc = DocumentApp.openById(baseDocId)
  var body = baseDoc.getActiveSection();
  var headpara='  *  '
  for (dd=1;dd<baseDoc.getNumChildren();++dd){
  baseDoc.removeChild(baseDoc.getChild(dd))
  }
  for( var i = 0; i < docIDs.length; ++i ) {
    var otherCopy = DocumentApp.openById(docIDs[i]).getActiveSection();
    var totalElements = otherCopy.getNumChildren();

    for( var j = 0; j < totalElements; ++j ) {
      var element = otherCopy.getChild(j).copy();
      var type = element.getType();
      if( type == DocumentApp.ElementType.PARAGRAPH ){
        }
      else if( type == DocumentApp.ElementType.TABLE){
        body.appendTable(element);
          if (i<docIDs.length-1){
              body.appendPageBreak();
              body.appendParagraph(headpara);// add a string '  *  ' to trace it
              }
        }
    }
  }
}

2°的分析功能:

function analyse() {
  var Doc = DocumentApp.openById('1UOr44ju8Li6yCSlmFbMRdimNpR2BjCGjcLkrwG9jW74');
    var totalElements = Doc.getNumChildren();
    var el=[]
    for( var j = 0; j < totalElements; ++j ) {
      var element = Doc.getChild(j);
      var type = element.getType();
Logger.log(j+" : "+type)
       if (type =='PARAGRAPH'){
       el[j]=element.getText()
       };
    }
Logger.log(el)    
}

最后这样的结果分析:

0 : PARAGRAPH
1 : PARAGRAPH
2 : TABLE
3 : PARAGRAPH
4 : PARAGRAPH
5 : PARAGRAPH
6 : TABLE
7 : PARAGRAPH
8 : PARAGRAPH
9 : PARAGRAPH
10 : TABLE
11 : PARAGRAPH
12 : PARAGRAPH
13 : PARAGRAPH
14 : TABLE
15 : PARAGRAPH
[, , NOT_FOUND, , ,   *  , NOT_FOUND, , ,   *  , NOT_FOUND, , ,   *  , NOT_FOUND, ]

(在“*”的段落由脚本补充说,“NOT_FOUND”是表)所以,现在我知道pagebreaks显示为段落,该文档创建单独的表之间的段落(导致空白页),但即使当我尝试删除使用element.removeFromParent(),我一直有这些空白页不想要的段落...我有点失落,现在;-)对不起,这么长。



文章来源: How to delete blank pages in a multipage document?