1

I am trying to place a lot of tables into my document, one by one, but i can't place more than 1 table, does anybody knows how to place each table on one page or textframe? I am using xml nodes like this:

< ?xml version="1.0" encoding="UTF-8"?>< tables>< containt>

< label>TABLE 1</label>

< table frame="none">

    < tgroup cols="3">

        < colspec colname="col1" colnum="1" colwidth="50%"/>

        < colspec colname="col2" colnum="2" colwidth="25%"/>

        < colspec colname="col3" colnum="3" colwidth="25%"/>

        < thead>

            < row>

                < entry>TEST</entry>

                < entry>TEST</entry>
                < entry>TEST</entry>

            < /row>

        < /thead>

        < tbody>

            < row>

                < entry>1</entry>

                < entry>2</entry>

                < entry>3</entry>

            < /row>

        < /tbody>

    < /tgroup>

< /table> 

< /containt>    

 < containt>

    < label>TABLE 2</label>

    < table frame="none">

         < tgroup cols="3">

            < colspec colname="col1" colnum="1" colwidth="50%"/>

            < colspec colname="col2" colnum="2" colwidth="25%"/>

            < colspec colname="col3" colnum="3" colwidth="25%"/>

            < thead>

                < row>

                    < entry>TEST</entry>

                    < entry>TEST</entry>

                    < entry>TEST</entry>

                < /row>

            < /thead>

            < tbody>

                < row>

                    < entry>1</entry>

                    < entry>2</entry>

                    < entry>3</entry>

                < /row>
            < /tbody>
        < /tgroup>
    < /table> 
< /containt>    < /tables>

There is all i have:

var fileRef = File.openDialog("Select XML file");
var importPrefs = app.xmlImportPreferences;
importPrefs.importCALSTables = true;
importPrefs.importStyle = XMLImportStyles.MERGE_IMPORT;

var docRef = app.documents.add(true, "Letter");

docRef.importXML(fileRef);
var rootElement = docRef.xmlElements.item(0);
var children = rootElement.xmlElements;

for(var i = 0; i < children.length; i++){   
    var pageRef = docRef.pages.item(0); // when i try to use i, i get an error
    var contentElement = rootElement.xmlElements.item(i);
    var frameRef = pageRef.placeXML(contentElement,[".5 in", ".5 in"]);
    
    var tableRef = frameRef.tables.item(0);
    tableRef.width = "7 in";
    tableRef.columns.item(0).width = "3 in";
    tableRef.columns.item(1).width = "2 in";
    tableRef.columns.item(2).width = "2 in";
    tableRef.clearTableStyleOverrides();
    myFrames = docRef.textFrames;
    tableOverflow();
}

function tableOverflow(){
    for(var f = 0; f<= myFrames.length -1; f++){
        while(myFrames[f].overflows == true){
            var myNewPage = docRef.pages.add();
            myMargin = myNewPage.marginPreferences,
            myGeometricBounds = [myMargin.top, myMargin.left, docRef.documentPreferences.pageHeight - myMargin.bottom, docRef.documentPreferences.pageWidth - myMargin.right ];
            var oldRuler = docRef.viewPreferences.rulerOrigin;
            docRef.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;
            with(docRef.pages[-1].textFrames.add()){
                geometricBounds = myGeometricBounds;
                previousTextFrame = docRef.pages[-2].textFrames[0];
            }
            docRef.viewPreferences.rulerOrigin = oldRuler;
        }
    }
}

Could someone help me please?

1 Answer 1

0

I'd try a different approach. Instead of place the tables and then worry about the overflows, I'd try to place one table per page and then thread the textframes from the end to the beginning. Then check again the pages to remove blank textframes and/or blank pages.

//=============================================================
//  Luis Felipe Corullón
//  Contato: lf@corullon.com.br
//  Site: http://scripts.corullon.com.br
//=============================================================

var fileRef = File.openDialog("Select XML file" , "XML document:*.xml");
var importPrefs = app.xmlImportPreferences;
importPrefs.importCALSTables = true;
importPrefs.importStyle = XMLImportStyles.MERGE_IMPORT;

var docRef = app.documents.add(true, "Letter");

docRef.importXML(fileRef);
var rootElement = docRef.xmlElements.item(0);
var children = rootElement.xmlElements;

for(var i = 0; i < children.length; i++){
    if (i==0) var pageRef = docRef.pages.item(0);
    else var pageRef = docRef.pages.add();
     
    var contentElement = rootElement.xmlElements[i];
    var frameRef = pageRef.placeXML(contentElement,[".5 in", ".5 in"]);
    
    var tableRef = frameRef.tables.item(0);
    tableRef.width = "7 in";
    tableRef.columns.item(0).width = "3 in";
    tableRef.columns.item(1).width = "2 in";
    tableRef.columns.item(2).width = "2 in";
    tableRef.clearTableStyleOverrides();
}

myFrames = docRef.textFrames;
tableOverflow();

function tableOverflow() {
    var i=docRef.pages.length-1;
    while (i > 0) {
        docRef.pages[i].textFrames[0].previousTextFrame = docRef.pages[i-1].textFrames[0];
        i--;
    }
    var i=docRef.pages.length-1;
    while(i > 0) {
        if (docRef.pages[i].textFrames[0].contents == "") docRef.pages[i].textFrames[0].remove();
        if (docRef.pages[i].textFrames.length == 0) docRef.pages[i].remove();
        i--;
    }
}
1
  • It's looks great! All tables was place in the documente, but the overflow doesn't work. I get this error: "Invalid value for set property previousTextFrame, waiting TextFrame, TextPath or NothingEnum enumerator, but received nothing. " at line 40. Do you have any idea? Thank you very much!
    – Giovanna
    Aug 28, 2020 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.