Copy link to clipboard
Copied
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
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 Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
@Eugene Tyson The convertToTable action also has a matching method in the Text types.
Happy programming …
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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()
}Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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()
}Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Can you share a sample doc?
Copy link to clipboard
Copied
Sample was shared with me @geirb9262873 let me know if ok to send on
Copy link to clipboard
Copied
Hi Eugene,
Yes, you can send on the document I shared with you.
Cheers,
Geir
Copy link to clipboard
Copied
Here is the WeTransfer link to the IDML file. I included the InDesign file, as well.
Cheers,
Geir
Find more inspiration, events, and resources on the new Adobe Community
Explore Now