11 Replies Latest reply on Feb 8, 2017 4:57 AM by Laubender

    Convert Cell To Graphic Cell using Javascript for InDesign

    sidd32925467

      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;

        • 1. Re: Convert Cell To Graphic Cell using Javascript for InDesign
          Laubender Adobe Community Professional & MVP

          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

          • 2. Re: Convert Cell To Graphic Cell using Javascript for InDesign
            Loic.Aigon Adobe Community Professional

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

            myCell.place(testImage);

             

            Always double check what you are working with exactly.

            Loic

            • 3. Re: Convert Cell To Graphic Cell using Javascript for InDesign
              Laubender Adobe Community Professional & MVP

              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

              • 4. Re: Convert Cell To Graphic Cell using Javascript for InDesign
                Laubender Adobe Community Professional & MVP

                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

                • 5. Re: Convert Cell To Graphic Cell using Javascript for InDesign
                  sidd32925467 Level 1

                  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;

                  • 6. Re: Convert Cell To Graphic Cell using Javascript for InDesign
                    Laubender Adobe Community Professional & MVP

                    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

                    • 7. Re: Convert Cell To Graphic Cell using Javascript for InDesign
                      sidd32925467 Level 1

                      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 )

                      • 8. Re: Convert Cell To Graphic Cell using Javascript for InDesign
                        Laubender Adobe Community Professional & MVP

                        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

                        • 9. Re: Convert Cell To Graphic Cell using Javascript for InDesign
                          Laubender Adobe Community Professional & MVP

                          "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

                          • 10. Re: Convert Cell To Graphic Cell using Javascript for InDesign
                            sidd32925467 Level 1

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

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

                            • 11. Re: Convert Cell To Graphic Cell using Javascript for InDesign
                              Laubender Adobe Community Professional & MVP

                              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