Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Table column alignment for numbers

Return to Member Forum

  • Author
    Posts
    • #83308
      Tamás Nagy
      Participant

      Hi guys,

      I’m brand new here, and maybe I’ll gain the “noob of the year” prize because of this post, but now I’ll try to upload a JavaScript code I made this afternoon :)

      This code is intended to solve a relatively common problem (at least for me): the numbers in a table column are supposed to be aligned to the right but alltogether they must leave equal free spaces on the left and right sides of the table column.

      This script decides which table cells are to be modified and calculates the neccessary right insets for all the selected table cells to create correct number alignments within the selection.

      The Javascript code is as follows:

      ———————————————————————————————–

      if (app.activeDocument.selection.length==0) exit();
      if (!app.activeDocument.selection[0].hasOwnProperty(“cells”)) exit();
      var myCtemp=app.activeDocument.selection[0].cells;
      if (myCtemp.length==0) exit();

      app.doScript(Igazit,ScriptLanguage.JAVASCRIPT,undefined,UndoModes.FAST_ENTIRE_SCRIPT,”Táblázat oszlop”);

      function Igazit(){
      var myWo=[], myWt=[], myCells=[], myOi, myTextw;
      for (var i=0; i<1000; i++) myWt[i]=0;

      for (var i=0; i<myCtemp.length; i++) {
      if (myCtemp[i].rowType!=RowTypes.BODY_ROW) continue;
      myCells.push(myCtemp[i]);
      myOi=myCtemp[i].parentColumn.index;
      myWo[myOi]=myCtemp[i].width;
      myTextw=myCtemp[i].texts[0];
      myTextw=myTextw.endHorizontalOffset-myTextw.horizontalOffset;
      myWt[myOi]=Math.max(myTextw,myWt[myOi]);
      };

      for (var i=0; i<myCells.length; i++){
      myOi=myCells[i].parentColumn.index;
      myTextw=(myWo[myOi]-myWt[myOi])/2;
      myCells[i].leftInset=0;
      myCells[i].texts[0].justification=Justification.RIGHT_ALIGN;
      myCells[i].rightInset=myTextw;
      };
      };

      ———————————————————————————————–

      Well, it’s my first, so please do not kill me for it :)

    • #83311
      David Blatner
      Keymaster

      You may need to put your code inside pre or code tags here on the forum.

    • #83312
      Tamás Nagy
      Participant

      Sorry?

      I mean I see it’s mean but I don’t know how to be professional :)

    • #83316
      Tamás Nagy
      Participant

      Did you mean some HTML-like <mycode> </mycode> pairs? Pls show me an example :)

    • #83319
      David Blatner
      Keymaster

      I’m sorry, I was just trying to be helpful. Yes, I mean adding html tags.

      For example
           if you use the pre tags
           you can do indents and it comes out looking
      more like code. :-)
      
    • #83796
      Tamás Nagy
      Participant

      Ok, let’s try it again :)

      With the pre tag:

      if (app.activeDocument.selection.length==0) exit();
      if (!app.activeDocument.selection[0].hasOwnProperty("cells")) exit();
      var myCtemp=app.activeDocument.selection[0].cells;
      if (myCtemp.length==0) exit();
      
      app.doScript(AlignNums,ScriptLanguage.JAVASCRIPT,undefined,UndoModes.FAST_ENTIRE_SCRIPT,"Table Numbers");
      
      function AlignNums(){
      var myWo=[], myWt=[], myCells=[], myOi, myTextw;
      for (var i=0; i<1000; i++) myWt[i]=0;
      
      for (var i=0; i<myCtemp.length; i++) {
          if (myCtemp[i].rowType!=RowTypes.BODY_ROW) continue;
          myCells.push(myCtemp[i]);
          myOi=myCtemp[i].parentColumn.index;
          myWo[myOi]=myCtemp[i].width;
          myTextw=myCtemp[i].texts[0];
          myTextw=myTextw.endHorizontalOffset-myTextw.horizontalOffset;
          myWt[myOi]=Math.max(myTextw,myWt[myOi]);
          };
      
      for (var i=0; i<myCells.length; i++){
          myOi=myCells[i].parentColumn.index;
          myTextw=(myWo[myOi]-myWt[myOi])/2;
          myCells[i].leftInset=0;
          myCells[i].texts[0].justification=Justification.RIGHT_ALIGN;
          myCells[i].rightInset=myTextw;
          };
      };
      

      By the way: how could I delete/modify my earlier posts?

    • #84346
      Anonymous
      Inactive

      Your code works just fine if one selects the column with numbers. I would expand the code so you only need to place cursor in the cell and it would treat this column as the one to work on. Better yet, if you continuously dealing with tables that have first column with numbers, you could modify script to automatically go to the first column every time. Do you need any help with your code?
      Michael

Viewing 6 reply threads
  • You must be logged in to reply to this topic.
Forum Ads