Discussion:
How to copy a table cell content in a text frame?
(too old to reply)
H***@adobeforums.com
2007-02-01 15:59:29 UTC
Permalink
Hello,

I work with InDesign CS1 and VB script. I have a table which has a lot of rows and columns. Now I want to copy a cell content in a text frame. It works, when I select the content and copy/paste it.

Unfortunately this copy/paste method does not work, when I run InDesign in background mode. Is there another copy method?

Thanks for your help.

Harald
O***@adobeforums.com
2007-02-01 16:21:58 UTC
Permalink
Hi Harald--

Copy/paste is an unreliable way to move data around, so let's avoid it. Why not do something like this (JS form):



//Given a reference to a cell "myCell"
//and a reference to a text frame "myTextFrame":
myTextFrame.contents = myCell.texts.item(0).contents;

Thanks,

Ole
H***@adobeforums.com
2007-02-02 08:48:51 UTC
Permalink
Hello Ole,

my problem is, that your code only works for text. But in my cells are also sometimes images or formatted text.

What can I do?

Thank you for your help,

Harald
D***@adobeforums.com
2007-02-02 12:32:01 UTC
Permalink
About the only solution that comes to mind is:

1. Duplicate the text frame that contains the source cell of the table.
2. Locate the duplicated cell in that duplicate text frame.
3. Move the contents of that cell to the target cell in the original table.
4. Delete the duplicated text frame.

In CS1, to get the parent text frame of a cell in a table, use:

myCell.texts[0].parentTextFrame;

Dave
H***@adobeforums.com
2007-02-02 13:11:16 UTC
Permalink
Thank you for your response!

But I have a question:
"2. Locate the duplicated cell in that duplicate text frame"

Is there a cell in this duplicated text frame?
How can I access it and move?

Harald
D***@adobeforums.com
2007-02-02 13:28:22 UTC
Permalink
Assuming that you duplicated the right text frame, the one holding the cell you care about, then yes, there is a cell in the table in that text frame.

This is a whole lot easier if your tables fit entirely in a single text frame. Then it is easy to find the source cell in the duplicated table because it has the same row/column coordinates as before.

But if this is going to split the table, things are more complicated. You'll need to find a way of communicating or calculating the effect of the split.

And, bear in mind that you don't want to move the cell, you want to move the text of the cell (any images "in the cell" are actually inline graphics, so they'll move along with the text).

I'm not trying to suggest this is easy, but it can be done.

Dave
H***@adobeforums.com
2007-02-02 15:03:41 UTC
Permalink
There is a problem with: myCell.texts[0].parentTextFrame;

This expression gives "Nothing", if the cell is too small to show the text. Is there any workaround?

Harald
D***@adobeforums.com
2007-02-02 15:11:55 UTC
Permalink
Ah, I forgot about that. About all I can suggest is that if that happens, you try an adjacent cell on the same row until you find one that isn't overset. If they're all overset, then I don't know what to suggest.

Dave
H***@adobeforums.com
2007-02-02 15:29:00 UTC
Permalink
...try an adjacent cell on the same row...

I don't understand, because I want the content of a specific cell, not from the adjacent cell.

Harald
D***@adobeforums.com
2007-02-02 15:34:58 UTC
Permalink
Yes, but right now you're trying to find the parent text frame and all the cells in a row have the same parent text frame.

myCell.texts[0] will give you the text of the cell whether or not it is overset, but overset text doesn't have a parentTextFrame.

Dave
D***@adobeforums.com
2007-02-02 15:55:25 UTC
Permalink
myCell.texts[1] gives you a reference to the contents of the cell whether or not it's overset. Sorry, I keep lapsing into JavaScript.

Dave
H***@adobeforums.com
2007-02-02 15:50:48 UTC
Permalink
Sorry, I am confused.

OK, I can get a parentTextFrame from an adjacent cell.
But how can I get the content of the overset cell?
H***@adobeforums.com
2007-02-02 16:09:50 UTC
Permalink
OK, now step by step:

1. myCell1 has an overset
2. I try to find a cell without overset in the row -> myCell2
3. Then I get the parent text frame from this adjacent cell
4. myCell2.texts[0].parentTextFrame

But myCell1.texts[0].parentTextFrame gives me still "Nothing".

Harald
D***@adobeforums.com
2007-02-02 16:39:59 UTC
Permalink
Yes it is.

myCell1.texts[0]

is a reference to the text in the cell whether or not it is overset.

myCell1.texts[0].contents

is the contents of that text converted to a JavaScript string.

You don't want that if you plan to move the contents to another cell. You must use something like:

myCell2.contents = ""
myCell1.texts[0].move(LocationOptions.before, myCell2.texts[0].insertionPoints[0]

You'll have to translate that into VB.

Dave
H***@adobeforums.com
2007-02-02 16:41:57 UTC
Permalink
Thank you for your help!
I will try it and come back.
D***@adobeforums.com
2007-02-02 16:26:55 UTC
Permalink
Right. But myCell1.texts[0] gives you a reference to the overset text. You get nothing because overset text can't have a parentTextFrame -- I seem to be saying this in every message.

Dave
H***@adobeforums.com
2007-02-02 16:33:20 UTC
Permalink
OK, then it is not possible to get the content of an cell with overset text. Not good for me...
H***@adobeforums.com
2007-02-05 10:00:54 UTC
Permalink
Hello Dave,

it works! Thank you very much for your help!

Because we use the MOVE method, the origin cell is empty.
Is there also a COPY method?

Harald
D***@adobeforums.com
2007-02-05 12:11:25 UTC
Permalink
No, that's why I said to start out by duplicating the parent text frame.

Dave
H***@adobeforums.com
2007-02-05 13:17:05 UTC
Permalink
OK, but then we are back to a point I have not understand.
myCell.texts[0].parentTextFrame

This expression gives "Nothing", when the cell has an overset.
Then you suggest, to check an adjacent cell.

But how can I get the parentTextFrame of my overset cell?
Can you give me a piece of Javascript code?

Thank you

Harald
Martin Fischer
2007-02-05 13:39:51 UTC
Permalink
What's making me confused is the question, if a cell’s texts[0] could have a parent text frame.

You can place a text frame inside a cell.
So texts[0] of the cell will have a text frame as it's child.

And you can look for the parent of a table (parent of cell) and may get a text frame.

But what should be the parent text frame of a texts[0]of a cell?

edit:
Oh I see.
In InDesign CS2 there is an object text frame as parentTextFrames[0] of a cell’s texts[0]. Is this the room where the texts[0]-object is in? Or is it the text frame where the table (parent of cell) is in?

Martin
H***@adobeforums.com
2007-02-05 14:14:12 UTC
Permalink
Hello Dave,

again, thank you for your help!

Harald
D***@adobeforums.com
2007-02-05 13:56:11 UTC
Permalink
You can't directly get it if the cell is completely overset. If it is only partially overset, then:

myCell.insertionPoints[0].parentTextFrame will get it.

If it is completely overset, then get the row-number from the name of the cell and iterate through all the cells in that row until you find one that isn't overset. It's parent text frame is the same as your original cell.

If they're all overset, then I don't know what to advise.

Dave
D***@adobeforums.com
2007-02-09 12:14:55 UTC
Permalink
Harald,

When we had this discussion, I completely forgot about the method I just posted here: Dave Saunders, "[JS] Copying text, retaining formatting" #2, 9 Feb 2007 4:05 am </cgi-bin/webx?14@@.3bc31b75/1>

This works on a slightly different principle:

1. It moves the text to a new frame.
2. It duplicates that text frame.
3. It puts one copy back into the original location.
4. It puts the other copy where you tell it to put it.
5. It deletes both text frames it added.

I just tested it on a document where I created a table, put some text in the first cell and forced it all overset, then with a script that consists only of the method followed by:

myStory = app.documents[0].stories[0];
myTable = myStory.tables[0];
myTable.cells[0].texts[0].duplicate(LocationOptions.atEnd, myStory);

I successfully copied the overset contents of that first cell to the end of the story.

Sorry I didn't think of this last week.

Dave

Continue reading on narkive:
Loading...