• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Split a Table rows into separate page

Participant ,
Feb 06, 2023 Feb 06, 2023

Copy link to clipboard

Copied

Hi, 

It is necessary to split a single table into multiple tables on multiple pages in order to do this. To split the table, I used the following script. Could you please assist me with this?

 

var myDocument = app.activeDocument;
for(var i=myDocument.textFrames.length-1; i>=0; i--)
{
for(var j=myDocument.textFrames[i].tables.length-1; j>=0; j--)
{
var Get_Table = myDocument.textFrames[i].tables[j];
var columnlength = myDocument.textFrames[i].tables[j].columns.length;
var rowlength = myDocument.textFrames[i].tables[j].rows.length;
var numRowsFirstPage =rowlength / 2;
var numRowsSecondPage = rowlength - numRowsFirstPage;
myDocument.pages.add();
myDocument.pages[0].select();
for (var k = 0;k< numRowsFirstPage;k++) {
myDocument.textFrames[i].tables[j].rows[k].move(LocationOptions.AT_BEGINNING, doc.pages[0]);
}
myDocument.pages[1].select();
for (var l = numRowsFirstPage; l < rowlength; l++) {
  myDocument.textFrames[i].tables[j].rows[l].move(LocationOptions.AT_BEGINNING, doc.pages[1]);
}
}
}
-Monisha
TOPICS
How to , Scripting

Views

1.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 06, 2023 Feb 06, 2023

Copy link to clipboard

Copied

Did you try your script? If you did, what happened?

 

It looks as if you want to make sure that on a spread a table breaks in such a way that the left- and right-hand pages contain (approximately) an equal number of rows. Is that correct? If not, please explain in some detail and shows us some before and after screenshots.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 06, 2023 Feb 06, 2023

Copy link to clipboard

Copied

Hi @Peter Kahrel ,

 

A single page of my InDesign document used to contain 59 rows after flowing the table. Therefore, I decided to separate the equal number of rows onto separate pages. I used the above script to split the table. In using the script, I encountered an error. I have attached a screenshot of the error details. Could you please guide me on this?

-Monisha

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied

The row object doesn't have a move() function, you could have checked that in some object model viewer.

The simplest way would be to make the frame on the left-hand page shorter, add a page, add a frame on the page, then thread the frame with the table to the new frame. Then the table will flow onto the new frame.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied

Hi @Peter Kahrel ,

 

Can you please elaborate the process which you are saying. I can't able to understand what you are saying.

-Monisha

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied

Hi,

I have modified the above script in which I can able to get the total number of columns but I cannot able to get the content values of the cells. I have attached the used script.

 

var myDocument = app.activeDocument;
for(var i=myDocument.textFrames.length-1; i>=0; i--)
{
  for(var j=myDocument.textFrames[i].tables.length-1; j>=0; j--)
  {
    var Get_Table = myDocument.textFrames[i].tables[j];
    var columnLength = Get_Table.columns.length;
    var rowLength = Get_Table.rows.length;
    var numRowsFirstPage = Math.ceil(rowLength / 2);
    var numRowsSecondPage = rowLength - numRowsFirstPage;
    
    myDocument.pages.add();
    myDocument.pages[0].select();
    var firstPageTable = myDocument.textFrames.add();
    firstPageTable.textFramePreferences.textColumnCount = columnLength;
    for (var k = 0; k < numRowsFirstPage; k++) {
      var currentRow = Get_Table.rows[k];
    for (var l = 0; l < columnLength; l++) {
         firstPageTable.cells.add().contents = currentRow.cells[l].contents;

}
    }
  
    
    myDocument.pages[1].select();
    var secondPageTable = myDocument.textFrames.add();
    secondPageTable.textFramePreferences.textColumnCount = columnLength;
    for (var m = numRowsFirstPage; m < rowLength; m++) {
      var currentRow = Get_Table.rows[m];
      for (var n = 0; n < columnLength; n++) {
        secondPageTable.cells.add().contents = currentRow.cells[n].contents;
      }
    }
  
  }
}

 

-Monisha

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied

You have a page with a text frame, and there's a table in that text frame.

Now add a page, add a text frame on that page, and thread that frame to the frame that holds the table.

Select the frame with the table, and make it shorter by pulling the bottom up.

As you shorten the table's frame you'll see bits of it appearing in the added frame.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 08, 2023 Feb 08, 2023

Copy link to clipboard

Copied

Hi @Peter Kahrel 

 

Based on your suggestion, I have modified my script. Now I'm getting an error saying that "remainingRows[j].remove();" is invalid. Please find attached the error screenshot and sample exported output file. Please find attached the error screenshot and sample exported output file. Additionally, I have to add a page after every page receives the data and also need to fit the data in the added page.

 

 

var myDocument = app.activeDocument;
var originalFrame = myDocument.textFrames[0];
var originalTable = originalFrame.tables[0];
var originalBounds = originalFrame.geometricBounds;
var originalTableHeight = originalBounds[2] - originalBounds[0];

// Add a new page
myDocument.pages.add();
var newPage = myDocument.pages[1];

// Add a new text frame on the new page
var newFrame = newPage.textFrames.add();

// Thread the new frame to the original frame
newFrame.previousTextFrame = originalFrame;

// Shorten the original frame
originalFrame.geometricBounds = [originalBounds[0], originalBounds[1], originalBounds[0] + originalTableHeight/2, originalBounds[3]];
// Split the rowlength 
var getroecount = originalTable.rows.length/2;
var getroundOffValue = [];
//~ getroundOffValue = getroecount.Split(".");
//~ alert(getroundOffValue[0]);
// Get the rows of the original table that are now in the new frame
var remainingRows = [];
for (var i = 29; i < originalTable.rows.length; i++) {
 remainingRows.push(originalTable.rows[i]);
}
// Remove the rows from the original table
for (var j = 0;j < remainingRows.length; j++) {
  remainingRows[j].remove();
}

// Create a new table in the new frame and add the remaining rows
var newTable = newFrame.tables.add();
newTable.columnCount = originalTable.columnCount;
newTable.bodyRowCount = remainingRows.length;
for (var i = 0; i < remainingRows.length; i++) {
  for (var j = 0; j < originalTable.columnCount; j++) {
    newTable.rows[i].cells[j].contents = remainingRows[i].cells[j].contents;
  }
}

 

 

MonishaRajendran_0-1675858057963.png

 

-Monisha

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 08, 2023 Feb 08, 2023

Copy link to clipboard

Copied

You shouldn't remove any rows. After you threaded the new frame to the first frame and shortened the first frame, (about) half the table should have appeared in the new frame.

Try it manually and see what happens.

Anyway, it goes like this:

var ruler = app.documents[0].viewPreferences.rulerOrigin;
app.documents[0].viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;

var myDocument = app.activeDocument;
var originalFrame = myDocument.textFrames[0];
var originalTable = originalFrame.tables[0];

// Make the original frame half the table's height
var gb = originalFrame.geometricBounds;
gb[2] = gb[0] + originalTable.height/2;
originalFrame.geometricBounds = gb;

// Add a page and a frame, and make the frame
// half the table's height

var newFrame = myDocument.pages.add().textFrames.add ({
	geometricBounds: gb,
});

// Thread the frames. The table is now in two frames
originalFrame.nextTextFrame = newFrame;

// The calculations are rough, so the table could
// have become overset. Fix that.
newFrame.fit (FitOptions.FRAME_TO_CONTENT);

// Restore the ruler origin
app.documents[0].viewPreferences.rulerOrigin = ruler;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

Hi @Peter Kahrel 

 

I have modified the above script to split multiple tables. But now I need to set the static row value. Based on that row values the table gets flown on that particular page. I have attached the output file for reference and also the script which I have used.

var ruler = app.documents[0].viewPreferences.rulerOrigin;
app.documents[0].viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
var myDocument = app.activeDocument;
var addPageLocation = 0;  
for(var i=myDocument.textFrames.length-1; i>=0; i--)
{
    var gettextFrame = myDocument.textFrames[i];
for(var j=myDocument.textFrames[i].tables.length-1; j>=0; j--)
{
    var table = myDocument.textFrames[i].tables[j];
    var columnlength = myDocument.textFrames[i].tables[j].columns.length;
    var rowlength = myDocument.textFrames[i].tables[j].rows.length + 1;
//~     alert(rowlength/2);
//~     alert(columnlength);
    var rowValue = myDocument.textFrames[i].tables[j].columns[0].contents;   
//~     alert(rowValue);
    //alert(AddedColumnLength);
    var tableRows = myDocument.textFrames[i].tables[j].columns.everyItem().getElements();
    columnlength = myDocument.textFrames[i].tables[j].columns.length - 1;
    alert(gettextFrame.geometricBounds)
    var gb = gettextFrame.geometricBounds + table.height/2;
    gettextFrame.geometricBounds = gettextFrame.geometricBounds;
    myDocument.pages.add(LocationOptions.AFTER, myDocument.pages[addPageLocation] );

    //you will need to add text frame as rectangles are used for graphics or pdfs

    var tf = myDocument.pages[addPageLocation+1].textFrames.add();

    //var gb = [125.891, 25, 230.458, 88];
    var gb2 = [1.5,0.5,5,5];
gettextFrame.nextTextFrame = tf;
gettextFrame.fit (FitOptions.FRAME_TO_CONTENT);
tf.fit (FitOptions.FRAME_TO_CONTENT);
    addPageLocation = addPageLocation+2;
    }
}
app.documents[0].viewPreferences.rulerOrigin = ruler;
-Monisha

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

Your sample document, does that show its state brfore you run the script, or is that how you want it to look. The four frames that contain tables are individual frames. It's now not clear to me what you want to achieve. Maybe do a desired output file manually.

 

> But now I need to set the static row value.

 

Not sure what you mean here.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 15, 2023 Feb 15, 2023

Copy link to clipboard

Copied

Hi @Peter Kahrel,

I want to split the rows of the tables based on getting the static values in the pop-up. As of now I can able to shrink and then the remaining rows are displayed on the next page. But now I need to add the page based on the row flow from the previous page. Hereby I have attached the latest script and the sample files for reference. "Sample document.indd" is my sample output file my actual InDesign document "Actual Document.indd"

 

 

 

 

var myDialog = app.dialogs.add({name:"Split Tables by Height"});
with(myDialog.dialogColumns.add()){
    with(dialogRows.add()){
        with(dialogColumns.add()){
            staticTexts.add({staticLabel:"Minimum Table Height:"});
        }
        with(dialogColumns.add()){
            var staticValueField = measurementEditboxes.add({editValue:10});
        }
    }
}
if(myDialog.show() == true){
    var ruler = app.documents[0].viewPreferences.rulerOrigin;
    app.documents[0].viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
    var myDocument = app.activeDocument;
    var addPageLocation = 0;  
    for(var i=myDocument.textFrames.length-1; i>=0; i--)
    {
        var getTextFrame = myDocument.textFrames[i];
        for(var j=myDocument.textFrames[i].tables.length-1; j>=0; j--)
        {
            var table = myDocument.textFrames[i].tables[j];
            var minTableHeight = staticValueField.editValue;
            var numRows = table.rows.length;
            var numCols = table.columns.length;
            var currentRowIndex = 0;
            var currentColIndex = 0;
            var currentPage = getTextFrame.parentPage;
            var currentPageIndex = app.activeDocument.pages[0].documentOffset;

            while (currentRowIndex < numRows) {
                var tableRows = table.rows.everyItem().getElements();
                var numRowsLeft = numRows - currentRowIndex;
                var rowsToSplit = Math.min(numRowsLeft, minTableHeight);
                var gb = getTextFrame.geometricBounds;
                gb[2] = gb[0] + (table.headerRowCount + rowsToSplit) * table.rows[0].height;
                if (gb[2] > currentPage.bounds[2]) {
                    myDocument.pages.add(LocationOptions.AFTER, myDocument.pages[addPageLocation] );
                    getTextFrame = myDocument.pages[addPageLocation+1].textFrames.add();
                    currentColIndex = 0;
                    addPageLocation = addPageLocation+1;
                }
                var tf = getTextFrame;
                if (currentColIndex > 0) {
                    tf = getTextFrame.nextTextFrame;
                }
                for (var row = currentRowIndex; row < currentRowIndex + rowsToSplit; row++) {
                    for (var col = 0; col < numCols; col++) {  
                        var cell = table.cell(row, col);
                        var newCell = cell.duplicate(LocationOptions.AFTER, cell);
                        newCell.contents = cell.contents;
                        newCell.appliedCellStyle = cell.appliedCellStyle;
                        newCell.autoGrow = false;
                        newCell.overflows = false;
                    }
                }
                tf.geometricBounds = gb;
                currentRowIndex += rowsToSplit;
                currentColIndex++;
            }
        }
    }
    app.documents[0].viewPreferences.rulerOrigin = ruler;
}
-Monisha

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2023 Feb 15, 2023

Copy link to clipboard

Copied

I'm terribly sorry, but at each turn of the conversation I get more confused. In your 'Actual Document.indd' the first seven rows form a single table, the following eight rows are in separate tables and in separate text frames -- placed in such a way to create the illusion of a single table.

 

> But now I need to add the page based on the row flow from the previous page. 

 

I don't understand this. Could you elaborate?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 15, 2023 Feb 15, 2023

Copy link to clipboard

Copied

LATEST

Hi @Peter Kahrel 

 

 Using my Script I need to split the 9 columns after giving the static row count which I have done the static changes.

-Monisha

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines