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

Convert Cell To Graphic Cell using Javascript for InDesign

New Here ,
Feb 07, 2017 Feb 07, 2017

Copy link to clipboard

Copied

Hi all,

I've searched the internet and forums but can't find an answer.

How do you convert a table cell to a Graphics cell using javascript?

I would like to produce a table (as the code below) but convert one of the cells to place an image in.

Is this possible?

var myDocument = app.documents.add(); 

var myTextFrame = myDocument.pages.item(0).textFrames.add({geometricBounds:["0mm", "0mm", "200mm", "100mm"]}); 

var myTable = myTextFrame.insertionPoints[-1].tables.add(); 

myTable.columnCount = 6; 

myTable.bodyRowCount = 7; 

myTable.rows.everyItem().autoGrow = false;

myTable.columns[0].width = '4mm'; 

myTable.columns[1].width = '2mm';

myTable.columns[2].width = '1.5mm';

myTable.columns[3].width = '2mm';

myTable.columns[4].width = '60mm';

myTable.columns[5].width = '2mm';

//myTable.rows[1].cells[1].height = '10mm'; 

myTable.rows[0].height = '4mm';

myTable.rows[1].height = '2mm';

myTable.rows[2].height = '1.5mm';

myTable.rows[3].height = '120mm';

myTable.rows[4].height = '4mm';

myTable.rows[5].height = '5.5mm';

myTable.rows[6].height = '5.5mm';

var testImage = File("/Vision On 3/Users/visionon3/Desktop/testImage.pdf");

var myCell = myTable.rows[3].columns[4];

myCell.place(testImage);

myTable.rows.everyItem().bottomEdgeStrokeColor = "Black";

myTable.rows.everyItem().bottomEdgeStrokeWeight = 0;

myTable.rows.everyItem().topEdgeStrokeColor = "Black";

myTable.rows.everyItem().topEdgeStrokeWeight = 0;

myTable.columns.everyItem().leftEdgeStrokeColor = "Black";

myTable.columns.everyItem().leftEdgeStrokeWeight = 0;

myTable.columns.everyItem().rightEdgeStrokeColor = "Black";

myTable.columns.everyItem().rightEdgeStrokeWeight = 0;

TOPICS
Scripting

Views

1.6K

Translate

Translate

Report

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 , Feb 08, 2017 Feb 08, 2017

"preserve data" means e.g. to use an image that is already placed to an insertion point (anchored graphic frame with image) as the contents of a cell after it is converted to a graphic cell. Since you will place a new image, I set the argument to false with the code of Case 1. With Case 2 this is different, because there you first place the image and then do the conversion.

I can also tell, that with "preserve date" set to true there is always a small performance penalty.

We could avoid this using

...

Votes

Translate

Translate
Community Expert ,
Feb 07, 2017 Feb 07, 2017

Copy link to clipboard

Copied

What is your version of InDesign?

I did not test with the code posted by you, but it seems that it will exactly do what you want.
Provided the cell is empty.

Here a link to InDesign ExtendScript DOM documentation compiled by Gregor Fellenz:

InDesign ExtendScript API (12.0)

InDesign ExtendScript API (12.0)

Regards,
Uwe

Votes

Translate

Translate

Report

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
People's Champ ,
Feb 07, 2017 Feb 07, 2017

Copy link to clipboard

Copied

var myCell = myTable.rows[3].columns[4];

myCell.place(testImage);

Always double check what you are working with exactly.

Loic

Votes

Translate

Translate

Report

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 ,
Feb 08, 2017 Feb 08, 2017

Copy link to clipboard

Copied

Hi Loic,

I should not answer when in a hurry.

Btw: object Cell has no method place().

So we both were wrong.

The OP wants to do a graphic cell.

So I would suggest something with converting cells. See also my posted link to method convertCellType() of object Cell .

Case 1: Convert, do not preserve data

As target use the graphic frame that is added to a cell when it is converted to a graphic cell.

I'm doing this using the generic object pageItem :

var myCell = myTable.rows[3].cells[4];

myCell.convertCellType( CellTypeEnum.GRAPHIC_TYPE_CELL , false )

myCell.pageItems[0].place(testImage);

Or, because we can assume the cell is empty and the cell type is text type by default, because the OP added the table and is not working with an existing table:

Case 2: Place in insertion point of cell, convert and preserve data

var myCell = myTable.rows[3].cells[4];

myCell.insertionPoints[0].place(testImage);

myCell.convertCellType( CellTypeEnum.GRAPHIC_TYPE_CELL , true );

But case 2 is only working if the cell was empty.

If not the second argument will not preserve the image.

Result will be an empty graphic cell.

So I would go for case 1.

Regards,
Uwe

Votes

Translate

Translate

Report

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 ,
Feb 08, 2017 Feb 08, 2017

Copy link to clipboard

Copied

Note:

The snippet in Case 1 will also work if the target cell is already a graphic cell.

No need to check the cell type before.

And it's ok using pageItems[0].place() with a graphic cell, because:

A graphic cell could also contain an oval or a polygon.

It could even contain a polygon with only two path points.

And with such an object place() would fail.

Bottom line:

I would always use convertCellType() before placing an image to prevent surprises.

Regards,
Uwe

Votes

Translate

Translate

Report

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 ,
Feb 08, 2017 Feb 08, 2017

Copy link to clipboard

Copied

That's great - thank you for your help.

I changed the script slightly to be able to choose the image to import as below.

Works a treat!

var myDocument = app.documents.add(); 

var myTextFrame = myDocument.pages.item(0).textFrames.add({geometricBounds:["0mm", "0mm", "200mm", "100mm"]}); 

var myTable = myTextFrame.insertionPoints[-1].tables.add(); 

myTable.columnCount = 6; 

myTable.bodyRowCount = 7; 

myTable.rows.everyItem().autoGrow = false;

myTable.columns[0].width = '4mm'; 

myTable.columns[1].width = '2mm';

myTable.columns[2].width = '1.5mm';

myTable.columns[3].width = '2mm';

myTable.columns[4].width = '60mm';

myTable.columns[5].width = '2mm';

//myTable.rows[1].cells[1].height = '10mm'; 

myTable.rows[0].height = '4mm';

myTable.rows[1].height = '2mm';

myTable.rows[2].height = '1.5mm';

myTable.rows[3].height = '120mm';

myTable.rows[4].height = '4mm';

myTable.rows[5].height = '5.5mm';

myTable.rows[6].height = '5.5mm';

// Choose image to import

myfile = new File;

myfile = myfile.openDlg();

if (myfile == null) {

    myDialog.destroy();

    app.activeDocument.close(SaveOptions.no);

    exit();

    }

fpath = myfile.absoluteURI;

var myCell = myTable.rows[3].cells[4]; 

myCell.insertionPoints[0].place(new File(fpath)); 

myCell.convertCellType( CellTypeEnum.GRAPHIC_TYPE_CELL , true );

myTable.rows.everyItem().bottomEdgeStrokeColor = "Black";

myTable.rows.everyItem().bottomEdgeStrokeWeight = 0;

myTable.rows.everyItem().topEdgeStrokeColor = "Black";

myTable.rows.everyItem().topEdgeStrokeWeight = 0;

myTable.columns.everyItem().leftEdgeStrokeColor = "Black";

myTable.columns.everyItem().leftEdgeStrokeWeight = 0;

myTable.columns.everyItem().rightEdgeStrokeColor = "Black";

myTable.columns.everyItem().rightEdgeStrokeWeight = 0;

Votes

Translate

Translate

Report

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 ,
Feb 08, 2017 Feb 08, 2017

Copy link to clipboard

Copied

Hm.
Why did you choose code from case 2 over Case 1 ?
I'd recommend: First convert, then place.

And that is shown in Case 1.

Regards,
Uwe

Votes

Translate

Translate

Report

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 ,
Feb 08, 2017 Feb 08, 2017

Copy link to clipboard

Copied

I knew the cell would always be empty as it will be a new table every time so I used the second case.

I tried both and both worked.

Could you explain though please why one line of code says "false" and the other one "true"?

Case 1 -

( CellTypeEnum.GRAPHIC_TYPE_CELL , false )

Case 2-

( CellTypeEnum.GRAPHIC_TYPE_CELL , true )

Votes

Translate

Translate

Report

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 ,
Feb 08, 2017 Feb 08, 2017

Copy link to clipboard

Copied

Check the link to the DOM documentation I gave above.
There you'll find the full explanation.

The second argument is boolean ( true or false )

and tells the method to "preserve data", yes or no.

Regards,
Uwe

Votes

Translate

Translate

Report

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 ,
Feb 08, 2017 Feb 08, 2017

Copy link to clipboard

Copied

"preserve data" means e.g. to use an image that is already placed to an insertion point (anchored graphic frame with image) as the contents of a cell after it is converted to a graphic cell. Since you will place a new image, I set the argument to false with the code of Case 1. With Case 2 this is different, because there you first place the image and then do the conversion.

I can also tell, that with "preserve date" set to true there is always a small performance penalty.

We could avoid this using the code shown with Case 1. That's a faster method.

As some tests are showing "preserve data" will not preserve anything if you convert a cell with an anchored image plus some text. The result will be an "empty" graphic cell.

If a text cell contains e.g. a anchored polygon with two path points only and you will convert the cell with "preserve data" set to true, you can easily leave a cell in a permanent overflow. A very strange state for a graphic cell.

Regards,
Uwe

Votes

Translate

Translate

Report

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 ,
Feb 08, 2017 Feb 08, 2017

Copy link to clipboard

Copied

LATEST

Here an example for a text cell you cannot convert to a graphic cell in the UI.

The contents is an anchored polygon with two path points:

TextCell-with-anchored-polygon-with-two-path-points.png

The menu command to convert text cells to graphic cells is grayed out.
Cell is selected:

Text-Cell-with-anchored-polygon-ConvertToGraphicCell-GrayedOut.png

However, by scripting you could try to exactly do that.

Convert to graphic cell and "preserve data" ( if possible )

app.selection[0].convertCellType( CellTypeEnum.GRAPHIC_TYPE_CELL , true );

The result is an overflowing cell with cell type GRAPHIC_TYPE_CELL .

If I select the first insertion point of the next cell and use the left arrow key to go back I am encounter this very strange scenario.

A selected object in the graphic cell that is not showing in the UI despite the fact that its stroke weight is 8 Pt and the stroke color is red.

Result-permanent-overflow.png

I bet, that's something the developers of graphic cells never had in mind but implicitly can be done by scripting.

Can we copy the cell if we selected it? No.

Error message in my German InDesign: "In Grafikzellen dürfen keine orthogonalen Linien enthalten sein".

"Graphic cells must not contain orthogonal graphic lines."

Can we copy the whole table? Yes.

Can we copy parts of the table? Yes, if this particular cell is not selected.

Bottom line:

I would be very careful using true as second argument with method convertCellType().

Avoid it at best.

Regards,
Uwe

Votes

Translate

Translate

Report

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 ,
Feb 08, 2017 Feb 08, 2017

Copy link to clipboard

Copied

Thanks for the info - I think I understand now!!!!

I will use Case 1 going forward with this little project.

Votes

Translate

Translate

Report

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