如何删除多页文档中的空白页? [英] How to delete blank pages in a multipage document?

查看:220
本文介绍了如何删除多页文档中的空白页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:添加一个答案,因为编辑本来会很长(请参阅答案2)

关于文档合并的前篇文章我结束了一个工作脚本(感谢Henrique;)但我仍然遇到一个小问题:最终的合并文档有时会包含空白页(取决于其他文档内容),我想删除它们。
我找不到一个简单的方法来做到这一点。
脚本如下所示:

 函数mergeDocs(docIDs){//参数docIDs是Doc ID的数组
var baseDocname = DocumentApp.openById(docIDs [0])。getName(); //典型名称= 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(); //创建一个firstelement的副本,并从系列中为它建立新的basedocname构建(保留边距等)
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(); (var j = 0; 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
抛出新错误(根据文档,这种类型不能出现在主体中:+ type);
}
body.appendPageBreak(); //如果内容长度太短避免打破页面布局


code>

'PageBreak'会导致(有时)一个空白页面,我知道(!),但有必要保持一个完美的页面布局(我用这个文档打印标签)。
在这里是指向典型示例的链接
p>

解决方案

好的,我不认为API上有任何东西可以告诉哪个页面是一个元素。所以,解决这个问题会很棘手:)

蝙蝠的权利,我想到了一种内部的方法。我的意思是,你知道哪个网页给你带来麻烦。如果它总是相同的(例如你有一个固定数量的标签),你可以循环计算分页符并删除不合格的分页。



但如果不是这样可能的,这是我的猜测,至少你知道你的布局。您可以测试一下,看看有多少标签完全符合页面,然后计算您的标签,以便在发生时可以跳过追加分页符。这看起来像一个更好的解决方案。

然后再次,根据您的布局,这可能是不可能的或太难。所以,我能想到的最后一件事是检查Document DOM,以查看页面中是否存在页面中断时是否发生任何特定模式。由于这很奇怪,我猜Google Docs可能会在这个页面上自动插入一个空白段落,所以它不是无子,或者类似的东西,甚至可能是一个属性,我不知道。我所知道的是,这需要大量的努力,通过调查深入了解Document DOM的工作原理。如果你不这样做,我将来可能不得不面对未来,因为我将文档作为这样的模板很多。当我这样做时,我会更新我的答案,如果你还没有在我面前做过):

EDIT : added an answer because edit would have been to long (see answer2)

Following a former post about document merging I ended up with a working script (Thanks Henrique ;) but I still have one small issue : the final 'merged' document contains sometimes blank pages (depending of other docs content) that I would like to remove. I cannot find an easy way to do this. The script goes like this :

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
  }
}

The 'PageBreak' causes (sometimes) a blank page , I know that(!), but it is necessary to keep a perfect page layout (I'm printing labels with this doc). here is a link to a typical example

解决方案

Well Serge, I don't think there's anything on the API to tell which page an element belongs. So, solving this will be tricky :)

Right of the bat, I think of an "inside" approach. I mean, you know which page is giving you trouble. If it is always the same (e.g. you have a fixed number of labels), you could just loop counting the page breaks and remove the bad one.

But if that's no possible, which is my guess, at least you know your layout. You could test to see how many labels fit a page exactly and then count your labels, so that when it happens, you skip appending the page-break. That looks like a better solution.

Then again, depending on your layout, that might not be possible or just too difficult. So, the last thing I can think of is to check the Document DOM to see if any specific pattern happens when a page-break is alone on a page. Since that's kind of weird, I guess Google Docs probably automatically inserts an empty paragraph on this page, so it's not "childless", or something like it, maybe even a property, I don't know. What I know is that this will require a good amount of effort, doing an investigation to understand deeply how the Document DOM works. If you don't do it, I'll probably have to in the future as I work with document as templates like this a lot. When I do I'll update my answer, if you haven't done it before me :)

这篇关于如何删除多页文档中的空白页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆