6 Replies Latest reply on Sep 4, 2011 5:34 AM by otohkun

    Getting the selected cells

    otohkun

      I have a question on javascript for InDesign CS5.5.
      When I select a part of a table, app.activeDocument.selection[0]
      returns a cell, say C. I think the `cells' property of C indicates
      the selected cells. So I usually get the selected cells by

       

        for(var i=0 ; i<app.activeDocument.selection[0].cells.length ; ++i) {
          myCellItem = app.activeDocument.selection[0].cells[i];
          ...

       

      This works expectedly in most cases. But some kind of merged cells
      cannot be validly obtained. For example, the following code returns

      an error. If anyone knows the reason, please teach me it.

       

      // make a document containing a table of type 3*2

        var myDoc=app.documents.add() ;
        var myTF=myDoc.pages[0].textFrames.add() ;
        myTF.geometricBounds=["50mm","50mm","150mm","150mm"] ;
        var myTable=myTF.tables.add() ;
        myTable.bodyRowCount=3 ;
        myTable.columnCount=2 ;

      // merge the (1,1) cell with the (2,1) cell
        myTable.columns[1].cells[1].merge(myTable.columns[1].cells[2]) ;

      // select the 1-2 rows
        myTable.rows[1].select() ;
        myTable.rows[2].select(SelectionOptions.ADD_TO) ;

      // write something into selected cells
        for(var i=0; i<app.activeDocument.selection[0].cells.length ; ++i)
         app.selection[0].cells[i].contents=""+i ; // <-- when i=2, this fails!

        • 1. Re: Getting the selected cells
          John Hawkinson Level 5

          Try modifying an .insertionPoint instead of the contents itself.

          • 2. Re: Getting the selected cells
            otohkun Level 1

            John,

             

            Thanks for reply.

            But my problem is not to write something to a cell.

             

            12
            35
            4

             

            If we select the cells 3,4 and 5 in the above table,

            I think the InDesign script engine cannot make

            a right information on the sellected cells.

            In our selection, the third object " .selection[0].cells[2] ",

            which should be the cell 5, seems broken.

             

            So I want to know an alternative method to get

            which cell we are selecting.

             

            Regards

            • 3. Re: Getting the selected cells
              John Hawkinson Level 5

              If we select the cells 3,4 and 5 in the above table,

              I think the InDesign script engine cannot make

              a right information on the sellected cells.

              In our selection, the third object " .selection[0].cells[2] ",

              which should be the cell 5, seems broken.

              Yes, this is definitely broken. Notice that the object is not valid!:

               

              app.selection[0].columns[1].isValid
              Result: false
              app.selection[0].cells[2].isValid
              Result: false
              

               

              I still don't understand what you want to do though! Why do you have the selection involved? It seems strange. What are you really trying to accomplish?

               

              You can use this:

               

              app.selection[0].parent.columns[1].cells[1].contents
              Result: 5
              • 4. Re: Getting the selected cells
                otohkun Level 1

                John,

                 

                Thanks again.

                 

                I was just trying to write a script which moves the selected rows downward/upward in a table.

                To make it available to tables with merged cells, I wanted to know the 'rowSpan's of each selected cells.

                 

                Regards

                • 5. Re: Getting the selected cells
                  John Hawkinson Level 5
                  I was just trying to write a script which moves the selected rows downward/upward in a table.

                  To make it available to tables with merged cells, I wanted to know the 'rowSpan's of each selected cells.

                  So are you stuck, or do you have a solution?

                   

                  The reason you see this behavior is that this what your selection really is -- a ranged plural object:

                  >> app.selection[0].toSpecifier()
                  (/document[@id=1]//text-frame[@id=225]/table[@id=230]/cell[@name="0:1"]
                  to /document[@id=1]//text-frame[@id=225]/table[@id=230]/cell[@name="0:2"])
                  

                   

                  But you can certainly get the rowSpan of the entire Selection:

                   

                  >> app.selection[0].rowSpan
                  2
                  

                   

                  I would tend to think you would want to do something like this:

                  app.selection[0].characters.everyItem().move(
                    LocationOptions.BEFORE,
                    app.selection[0].parentRow.parent.
                      rows[app.selection[0].parentRow.index-1]);
                  

                   

                  That is, moving the contents of the selection to come before the row prior to the selection.

                  That doesn't quite work, though, it merges it with the table.

                  And there is no .move() method that operates on cells or rows. Still, food for thought...

                  1 person found this helpful
                  • 6. Re: Getting the selected cells
                    otohkun Level 1

                    John,


                    > But you can certainly get the rowSpan of the entire Selection:
                    >
                    > >> app.selection[0].rowSpan
                    > 2

                     

                    Thank you very much!

                     

                    app.selection[0].rowSpan
                    app.selection[0].parentRow -- maybe meaning the 1st row in selection

                     

                    These are enough to write my script. Very simple solution!

                     

                    My script (moving downward version) is something like this:

                    1) var N = the rowSpan of the selected rows.
                    2) Find the 1st row `R' below the selected rows whose bottomEdges make
                       a line of table width. (This part is a little bit complicated.)
                    3) Insert new N rows under R.
                    4) Cut the selected rows and paste them on the new rows.

                     

                    Regards