How to delete blank pages in a multipage document?

后端 未结 2 1866
臣服心动
臣服心动 2021-01-24 16:32

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 Henriqu

2条回答
  •  旧巷少年郎
    2021-01-24 16:55

    I modified the script so that it gives me constant results and wrote an analyse function to get the structure of the document. I found it's probably a better idea to put all this in an answer rather than edit my first question because it's a bit long. 1° the simplified script :

    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

    2° the analyse function :

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

    and finally the result of this analyse :

    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, ]
    

    (the ' * ' are paragraphs added by the script, the 'NOT_FOUND' are the tables) So, now I know that pagebreaks are shown as paragraphs, that the doc creates by itself paragraphs between tables (which causes the blank pages) but even when I try to remove unwanted paragraphs using element.removeFromParent(), I keep having these blank pages... I'm a bit lost right now ;-) Sorry for being so long.

提交回复
热议问题