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

InDesign Javascript convert text to table

New Here ,
Jun 08, 2025 Jun 08, 2025

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

TOPICS
Scripting
874
Translate
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

correct answers 1 Correct answer

Community Expert , Jun 09, 2025 Jun 09, 2025

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 
...
Translate
Community Expert ,
Jun 08, 2025 Jun 08, 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.

Translate
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
Mentor ,
Jun 08, 2025 Jun 08, 2025

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

Happy programming …

DirkBecker_0-1749394511825.png

 

Translate
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
New Here ,
Jun 08, 2025 Jun 08, 2025

Hi Eugene. Thank you for a very quick reply. I'll send you that as soon as I am back home by the computer. 
Cheers,

 

Geir

Translate
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 ,
Jun 08, 2025 Jun 08, 2025

Hi @geirb9262873 , Maybe something like this:

 

Screen Shot 1.png

 

Screen Shot.png

 

//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()
}
Translate
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
New Here ,
Jun 09, 2025 Jun 09, 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

Translate
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
New Here ,
Jun 09, 2025 Jun 09, 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 

Translate
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 ,
Jun 09, 2025 Jun 09, 2025

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()
}
Translate
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
New Here ,
Jun 09, 2025 Jun 09, 2025
LATEST

Hi Rob,

 

I just tried it and it works!

Thank you so much. Smart trick with the reverse loop.

This save me a lot of work.

Each documents is 15-20 pages full of tables and I have tens of documents.

 

With kind regards,

 

Geir

Translate
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
New Here ,
Jun 09, 2025 Jun 09, 2025

Hello again,

 

Actually, it turns out that the script affects the para-styles of the various headings above the tables, so that gives me a job to change all the headings back to the original para-tag.

 

Cheers,

 

Geir

Translate
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 ,
Jun 09, 2025 Jun 09, 2025

Can you share a sample doc?

Translate
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 ,
Jun 09, 2025 Jun 09, 2025

Sample was shared with me @geirb9262873 let me know if ok to send on

Translate
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
New Here ,
Jun 09, 2025 Jun 09, 2025

Hi Eugene,

 

Yes, you can send on the document I shared with you.

 

Cheers,

Geir

Translate
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
New Here ,
Jun 09, 2025 Jun 09, 2025

Here is the WeTransfer link to the IDML file. I included the InDesign file, as well.

 

https://we.tl/t-C3jB189C67

 

Cheers,

 

Geir

Translate
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