Copy link to clipboard
Copied
Hi,
I am looking for help moving text from one table field to another and have around 80 tables to modify in one document.
The text I want to move has it's own para style which will hopefully help with the script.
Here is an example of what I currently have:
Column 1 | Column 2 | Column 3 |
---|---|---|
empty field | 1234 (with paragraph style 'partno' followed by a return) ABCD (with paragraph style 'desc') | qty |
empty field | 123456 (with paragraph style 'partno' followed by a return) ABCD (with paragraph style 'desc') | qty |
empty field | 12345 (with paragraph style 'partno' followed by a return) ABCD (with paragraph style 'desc') | qty |
Here is an example of the finished result I would like to achieve:
Column 1 | Column 2 | Column 3 |
---|---|---|
1234 | ABCD (description with paragraph style 'desc') | qty |
123456 | ABCD (description with paragraph style 'desc') | qty |
12345 | ABCD (description with paragraph style 'desc') | qty |
I am just discovering the amazing things that can be done using scripts in indesign and how efficient they make laborious tasks.
Appreciate any help I receive.
thanks
David
Hi,
Uwe is totally right! … Be careful on what you ask and what you provide! …
Once again, this could work on your file sample but at your own risks! [if safer, think about paying a scripter to achieve such a script!]
...var myRows = app.activeDocument.stories.everyItem().tables.everyItem().rows.everyItem().getElements(),
R = myRows.length, r;
for ( r = 0; r < R; r++ ) {
try {
if ( myRows
.cells[1].paragraphs[0].appliedParagraphStyle.name == "partno" ) { myRows
.cells[1].paragrap
Copy link to clipboard
Copied
Hi,
You should be able to use code something like this, I have made it more verbose than is probably needed as you say you are just discovering what can be done, and I wanted to make it as easy to follow as possible.
// get active document
var myDoc = app.activeDocument;
// get all tables from that document
var myPageItems = myDoc.pageItems.everyItem();
var myTables = myPageItems.tables.everyItem().getElements();
// for each table
for ( var i = 0; i < myTables.length; i++)
{
// get all cells
var curTable = myTables;
var curTableCells = curTable.cells;
// for each cell
for ( var j = 0; j < curTableCells.length; j++)
{
var curCell = curTableCells
// get all text ranges - a text range is made everytime a style is changed
var curTextStyleRanges = curCell.textStyleRanges;
for ( var k = 0; k < curTextStyleRanges.length; k++)
{
var curTextStyleRange = curTextStyleRanges
// check the name of the text ranges appliedParagraphStyle
if ( curTextStyleRange.appliedParagraphStyle.name === "partno")
{
// as your example the empty cell is always the one before the cell we currently have
var jMinusOne = j - 1;
var newDestCell = curTableCells [jMinusOne];
// only copy the cells contents that match our range
newDestCell.contents = curTextStyleRange.contents;
// set the new contents to have the same paragraph style
newDestCell.textStyleRanges[0].appliedParagraphStyle = myDoc.paragraphStyles.itemByName("partno");
// delete the contents from the original cell
curTextStyleRange.remove();
}
}
}
}
Hope this helps
Malcolm
Copy link to clipboard
Copied
Hi Malcolm, thanks for helping out.
I ran your script and get the following error:
Copy link to clipboard
Copied
HI,
Can you provide a sample document so that we can see what the issues is, it is probably just some graphic object or other non text item, which doesn't have the properties required by the scripts.
Regards
Malcolm
Copy link to clipboard
Copied
Hi Malcolm, thank you.
here is a url to download a sample spread. - hopefully this shines some light on the issue.
I do hope it is a simple thing to resolve. I also believe we are in opposite hemispheres but if I receive a reply - which may be late evening for me - I will hopefully be able to check on any feedback I receive.
thanks Again for your help
Copy link to clipboard
Copied
Hi David,
no wonder Michel's script is tripping. Your sample InDesign document presents a totally different structure in the table cells compared with what you presented in your initial post.
The best bet—as seen from the sample doc—is to inspect the fill color of cell 1 and cell 2 of a given row. If it is the same and if it is "Titanium" and if the first cell is empty, then inspect cell 2. If cell two contains two paragraphs and if paragraph one consists of numbers only, then move paragraph one to the empty cell 1 and finally get rid of the pargaraph sign at the end of the paragraph.
That would be a kind of algorithm that could work. However it could fail silently or do the wrong thing, if anywhere in your document is an exception. Maybe a cell is not filled with right fill color or white space sneaked in in the cell that should be empty or in the paragraph that should contain only numbers. Other exceptions are possible as well… 🙂
Regards,
Uwe
Copy link to clipboard
Copied
Oh, and another thing: Your sample document has no paragraph style named "desc". Another thing, that could go wrong, if a script is working with that to identify the right cell.
Regards,
Uwe
Copy link to clipboard
Copied
Hi Uwe,
Thanks for the honest feedback.
I guess my thinking was a script would look for text styled partno in
column 2, check the row it’s in then copy ( cut) the partno text Into
column 1 of the corresponding row. Then find the next instance of partno...
but i have no idea about scripting hence asking for some help.
Even if a script helped with 90% it would save me time.
Thanks
David
Copy link to clipboard
Copied
Hi,
Uwe is totally right! … Be careful on what you ask and what you provide! …
Once again, this could work on your file sample but at your own risks! [if safer, think about paying a scripter to achieve such a script!]
var myRows = app.activeDocument.stories.everyItem().tables.everyItem().rows.everyItem().getElements(),
R = myRows.length, r;
for ( r = 0; r < R; r++ ) {
try {
if ( myRows
.cells[1].paragraphs[0].appliedParagraphStyle.name == "partno" ) { myRows
.cells[1].paragraphs[0].characters.itemByRange(0, myRows .cells[1].paragraphs[0].characters.length-2).move(LocationOptions.after, myRows .cells[0].insertionPoints[0]); myRows
.cells[1].paragraphs[0].contents = ""; }
} catch(e) {}
}
Best,
Michel
Copy link to clipboard
Copied
Hi Michel, thank you for your help with the script, it works well and has saved a lot of tedious work.
I did find that some text in the document had a paragraph style and character style which I think cause the script to falter but after removing the character style and making sure the para style was not is a folder all was good.
thanks again
David
Copy link to clipboard
Copied
Hi David,
if you like to move text, would you like the formatting of the text stay the same after it is moved?
If that's the case one would use method move() with object Text and would not use property contents.
Regards,
Uwe
Copy link to clipboard
Copied
A simplistic and short writing of a way working in such a sample [transfer of the first para of each cell of the second column to the corresponding cell of the first column]!
var myRows = app.activeDocument.stories.everyItem().tables.everyItem().rows.everyItem().getElements(),
R = myRows.length, r;
for ( r = 0; r < R; r++ ) {
myRows
.cells[1].paragraphs[0].characters.itemByRange(0, myRows .cells[1].paragraphs[0].characters.length-2).move(LocationOptions.after, myRows .cells[0].insertionPoints[0]); myRows
.cells[1].paragraphs[0].contents = ""; }
Best,
Michel, from FRIdNGE
Copy link to clipboard
Copied
Hi Michel, thank you your help.
I ran your script and get the following error:
Copy link to clipboard
Copied
Hi Uwe,
you are right, the paragraph formatting of the 1st para in column 2 would remain the same once it was moved to column 1.
thanks for your help
David
Copy link to clipboard
Copied
Hi David,
then the approach of Michel's script is the right one.
But maybe there are some exceptions in one of your tables in the document.
E.g. some empty cells in column two. Or some graphic cells here and there.
To make that script run without errors some if statements should be included.
But for that it would be perfect to see into the original InDesign document.
Regards,
Uwe