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
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 :)
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<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° 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.