3 Replies Latest reply on Mar 29, 2015 1:50 AM by Laubender

    How does columnSpan work? [JS][CC2014]

    raintan33

      Some background info:

      I'm using InDesign CC 2014 latest version

      I have a simple table set up: 6 columns by 5 rows

       

      I want to check if the first cell in the first row is merged, and if it isn't merged, merge the first 4 columns of the first row. So I wrote this javascript:

       

      myDoc = app.activeDocument,

      myPage = myDoc.pages[0],

      myTable = myDoc.stories.everyItem().tables[0];

      var checkMerged = myTable.rows[0].cells[0].columnSpan;

      alert(checkMerged);

      //merge the cells if not already merged

      if (checkMerged === 1){

         myTable.rows[0].cells[0].merge(myTable.rows[0].cells[3]);

          } else {

              alert("Merged");

              }

       

      When the cell is not merged, the alert returns "1,1,1", when it is merged, it returns "4,1,1".

       

      on this page: Adobe InDesign CS6 (8.0) Object Model JS: Cell it says that columnSpan is a readonly number, but it looks like an array.

       

      Please help. I feel like I'm taking crazy pills.

       

      Thanks

        • 1. Re: How does columnSpan work? [JS][CC2014]
          Laubender Adobe Community Professional & MVP

          @raintan33 – there is one assumption you make on everyItem(), that is simply wrong.

          Check, what you really get with the variable "myTable". Is it one table? Yes? Think twice…

           

          Startup your ESTK (ExtendScript Toolkit), connect to your version of inDesign and do the following checks:

           

          var myDoc = app.activeDocument,
          myPage = myDoc.pages[0],
          myTable = myDoc.stories.everyItem().tables[0];
          
          //Some assumptiosn about variable "myTable":
          $.writeln(myTable);
          $.writeln(myTable.constructor.name);
          
          //And what it really is:
          $.writeln(myTable.getElements().constructor.name);
          $.writeln(myTable.getElements().length);
          

           

          All these checks should show you something enlightening in the JavaScript Console of the ESTK (ExtendScript Toolkit).

           

          Read Marc Autret's blog posts on everyItem():
          Indiscripts :: On ‘everyItem()’ – Part 1

          Indiscripts :: On ‘everyItem()’ – Part 2

           

          Come back with some results on that and I can give you further advice, if you need some.

          (And please, don't forget to declare variables with var from the start)

           

          Uwe

          • 2. Re: How does columnSpan work? [JS][CC2014]
            raintan33 Level 1

            @Laubender Thanks for pointing me in the right direction.

             

            When I ran your tests, the console outputs:

             

            $.writeln(myTable);  //[object Table]
            $.writeln(myTable.constructor.name);  //Table
            $.writeln(myTable.getElements().constructor.name);    //Array
            $.writeln(myTable.getElements().length); //1
            
            


            I'm not sure I fully grasped all the concepts on my first read of those articles, though. My understanding is that  everyitem() is returning an array, but the array is not something that can be used because it's only a path to the item and not the actual item (Not 100% sure that's right - need to re-read those articles again).

             

            I changed my code to:

             

            var myDoc = app.activeDocument,
            myPage = myDoc.pages[0],
            myTable = myDoc.stories[0].tables[0];
            
            var checkMerged = myTable.rows[0].cells[0].columnSpan;
            alert(checkMerged);
            //merge the cells if not already merged
            if (checkMerged === 1){
              myTable.rows[0].cells[0].merge(myTable.rows[0].cells[3]);
                } else {
                    alert("Merged");
            }
            
            

             

            And now it works

             

            Thanks again!

            • 3. Re: How does columnSpan work? [JS][CC2014]
              Laubender Adobe Community Professional & MVP

              Re-reading Marc's blog posts about everyItem() is a very good idea.

              I think, the first time I read these, I did not fully understand all the implications of his findings.

               

              The core sentence in the first article is the one in bold:

               

              Quote Marc Autret:

              JavaScript regards app.documents.everyItem() as a single Document object, even though any property or method being invoked on that entity sends a COMMAND that multiple InDesign actual documents will receive.


              In course of time I re-read Marc's articles on everyitem() *several* times, twice a year?, three times a year? more often? to fully grasp the whole picture.

              And every time I re-read it, new ideas and implications will pop up in my head.

               

              Uwe