2 Replies Latest reply on Jul 12, 2010 12:34 PM by Tom Tomasko

    Problem creating multiple tables in one story

    Tom Tomasko Level 1

       

      Hello,

      I have a script that imports text from a .txt file and then makes a table out of it. That works fine. However, at a later time I will want to import some more text from a different .txt file and also make that into a table. And I’ll want the ability to do that a number of times.

       

      All of the tables are in the same story.

       

      The first table is created at the same time the ID document is created. When I run the script again to add a second table the script detects that the document exists and presumes there already is a table. The script then goes to a function that creates a second table. All this works fine as well.

      However, I cannot then add any more tables without subsequent tables being added into the second table, which is quite messy.

       

      Below is a boiled down script that just concentrates on the problem. I have written it so that it will add a second and third table to the first. What I need to do is to collapse the functions secondTable() and thirdTable() into one function. If you comment out the thirdTable() and run the script with the secondTable() function twice you will see the problem I have.

       

      To make this work, just open up a new ID document, one page, with one text frame.

       

      Thanks for any help,

      Tom

       

      var myDoc = app.activeDocument;

      var myStory = myDoc.stories.everyItem();

      firstTable();

      secondTable();

      //secondTable();

      thirdTable();

       

      //***functions*****
      function firstTable(){
          var lastInsertionPointIndex = myStory.insertionPoints.lastItem().index;
      myStory.insertionPoints.item(lastInsertionPointIndex).contents = "Cell one \tCell two\rCell three \tCell four";
          var lastChar = myStory.characters.lastItem();
          var firstChar = myStory.characters.item(-lastInsertionPointIndex);
          var theTextRange = myStory.texts.itemByRange(firstChar, lastChar)
          theTextRange.convertToTable("\t","\r");
      }//end firstTable

       

      function secondTable(){
          var lastInsertionPointIndex = myStory.insertionPoints.lastItem().index;
      myStory.insertionPoints.item(lastInsertionPointIndex).contents = "Cell one \tCell two\rCell three \tCell four";
          var lastInsertionPointIndex = myStory.insertionPoints.lastItem().index;
          var lastChar = myStory.characters.lastItem();
          var firstChar = myStory.characters.item(-lastInsertionPointIndex+1);
          var theTextRange = myStory.texts.itemByRange(firstChar, lastChar)
          theTextRange.convertToTable("\t","\r");
      }//end secondTable

       

      function thirdTable(){
          var lastInsertionPointIndex = myStory.insertionPoints.lastItem().index;
      myStory.insertionPoints.item(lastInsertionPointIndex).contents = "Cell one \tCell two\rCell three \tCell four";
          var lastInsertionPointIndex = myStory.insertionPoints.lastItem().index;
          var lastChar = myStory.characters.lastItem();
          var firstChar = myStory.characters.item(-lastInsertionPointIndex+2);
          var theTextRange = myStory.texts.itemByRange(firstChar, lastChar)
          theTextRange.convertToTable("\t","\r");
      }//end thirdTable

        • 1. Re: Problem creating multiple tables in one story
          Shonkyin Level 3

          Hi Tom,

           

          Please check below code.

           

          var myDoc = app.activeDocument;
          
          var myStory = myDoc.stories[0];
          
          firstTable();
          
          secondTable();
          
          secondTable();
          
          thirdTable();
          
           
          
          //***functions*****
          function firstTable(){
              var lastInsertionPointIndex = myStory.insertionPoints.lastItem().index;
               var firstChar = myStory.characters.item(lastInsertionPointIndex);
               myStory.insertionPoints.item(lastInsertionPointIndex).contents = "Cell one \tCell two\rCell three \tCell four";
              var lastChar = myStory.characters.lastItem();
              var theTextRange = myStory.texts.itemByRange(firstChar, lastChar)
              theTextRange.convertToTable("\t","\r");
          }//end firstTable
          
           
          
          function secondTable(aa){
              var lastInsertionPointIndex = myStory.insertionPoints.lastItem().index;
               var firstChar = myStory.characters.item(lastInsertionPointIndex);
               myStory.insertionPoints.item(lastInsertionPointIndex).contents = "Cell one \tCell two\rCell three \tCell four";
              var lastInsertionPointIndex = myStory.insertionPoints.lastItem().index;
              var lastChar = myStory.characters.lastItem();
              var theTextRange = myStory.texts.itemByRange(firstChar, lastChar)
              theTextRange.convertToTable("\t","\r");
          }//end secondTable
          
           
          
          function thirdTable(){
              var lastInsertionPointIndex = myStory.insertionPoints.lastItem().index;
               var firstChar = myStory.characters.item(lastInsertionPointIndex);
               myStory.insertionPoints.item(lastInsertionPointIndex).contents = "Cell one \tCell two\rCell three \tCell four";
              var lastInsertionPointIndex = myStory.insertionPoints.lastItem().index;
              var lastChar = myStory.characters.lastItem();
              var theTextRange = myStory.texts.itemByRange(firstChar, lastChar)
              theTextRange.convertToTable("\t","\r");
          }//end thirdTable
          

           

           

          Hope it will work for you.

           

          Shonky

          • 2. Re: Problem creating multiple tables in one story
            Tom Tomasko Level 1

            Shonky,

             

            Yes! Thank you.

             

            I don't have the time right now to dechiper the changes you made, but I knew it would be something minor like moving some lines around.

             

            Since secondTable() and thirdTable() are the same, I can delete the the latter.

             

            If I first run the script to make the first and second tables and then run the script again with the firstTable() function commented out, it just adds another able, exactly what I want. So I will be able to plug this into the much larger script (which exports XML, then imports the .xml file as text to another document, uses GREP to strip all the contents except the tag name and attributes such as the page, paragraph and line numbers where the original text was tagged, and then makes that into a table. The result is a table of errors and where they occured in the original document).

             

            So now I can make one document that has multiple tables.

             

            I do have one minor question left. Shonky, you and others enclose your scripts in these notes in a text field. I have never been able to figure out how to do that.

             

            Tom