Skip to main content
geirb9262873
Known Participant
June 8, 2025
Answered

InDesign Javascript convert text to table

  • June 8, 2025
  • 2 replies
  • 912 views

Hello,

 

I have a document where I have blocks of text that are supposed to become tables.

I have tabs (\t) to separate between colums and newline (\r) to separate between rows.

For the moment, I to to go through the document manually, select the text blocks and convert them to tables. 

All the lines of text that are supposed to become tables have the paragraph tag "Body-for-Table".

In between there are other parts of text, such as headings, sub-headnings and so on.

 

The number of lines in each block of "Body-for-Table" can vary from one to 20 or so.

 

Is it possible to make a javascript that goes through the document and converts each text block "Body-for-Table" to a table? My Table Style is called "Table-pratiqué-etc".

 

Best regards from

 

Geir

Correct answer rob day

Hi again Rob,

I found out that if I put in a newline after each of the text blocks with para-tag "Body-for-Table" and give this new line another para-tag, such as "Body", for example, then the result comes out nicely.  Then, the various headings and sub-headings between the tables remain intact. If you can find a way around this, it would be marvellous.

 

Cheers,

 

Geir 


Got the sample from @Eugene Tyson —does this work?:

 

//an array of texts with "Body-for-Table" para style
var ra = getTextStyleSearch("Body-for-Table");

var t, ln;
//reverse loop
for (var i = ra.length-1; i > -1; i--){
    try {
        //get the characters without the last return
        ln = ra[i].characters.itemByRange(0,ra[i].characters.length-2);
        ln.convertToTable();
    }catch(e) {}  
    
}; 


/**
* Search for a paragraph styles 
* @ param fp the name of a paragrah style to search for String
* @ return result array
*/
function getTextStyleSearch(fp){
    app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
    app.findTextPreferences.appliedParagraphStyle = fp;
    return app.activeDocument.findText()
}

2 replies

rob day
Community Expert
Community Expert
June 8, 2025

Hi @geirb9262873 , Maybe something like this:

 

 

 

//an array of texts with "Body-for-Table" para style
var ra = getTextStyleSearch("Body-for-Table");

var t;
for (var i = 0; i < ra.length; i++){
    t = ra[i].convertToTable("\t","\r");
    
    //assign a table style here?

    //remove empty row at end?
    t.rows.lastItem().remove();
};   

/**
* Search for a paragraph styles 
* @ param fp the name of a paragrah style to search for String
* @ return result array
*/
function getTextStyleSearch(fp){
    app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
    app.findTextPreferences.appliedParagraphStyle = fp;
    return app.activeDocument.findText()
}
geirb9262873
Known Participant
June 9, 2025

Hi Rob,

 

That was magic. It works! 

I managed to change the table style of all the tables by adding to a another script that swaps the columns.

 

Thank you very much!

 

Cheers,

 

Geir

geirb9262873
Known Participant
June 9, 2025

Hi again Rob,

I found out that if I put in a newline after each of the text blocks with para-tag "Body-for-Table" and give this new line another para-tag, such as "Body", for example, then the result comes out nicely.  Then, the various headings and sub-headings between the tables remain intact. If you can find a way around this, it would be marvellous.

 

Cheers,

 

Geir 

Community Expert
June 8, 2025

Can you drop an idml sample document of what you need. I could try for you but would like to see what you're working with.

Legend
June 8, 2025

@Eugene Tyson The convertToTable action also has a matching method in the Text types.

Happy programming …