3 Replies Latest reply on Jan 6, 2009 1:32 PM by frameexpert

    [CS3 JS] Finding paragraph containing table

    frameexpert Level 4
      I have a table object and want to find the paragraph that contains the table. Then I want to return the contents of the paragraph previous to the table anchor. I am using this, but when I attempt to display the contents of the paragraph, I get "Object is invalid". Thanks for any help you can provide.


      // Get the paragraph inside the table.
      var oPgf = app.selection[0].paragraphs[0];
      // Make sure the paragraph is in a cell.
      if (oPgf.parent.constructor.name === "Cell") {
        // Get the parent cell and table objects.
        var oCell = oPgf.parent;
        var oTbl = oCell.parent;

        // Get the paragraph containing the table.
        var oPgf2 = oTbl.storyOffset.paragraphs[0];
        // Get the story object of the paragraph.
        var oStory = oPgf.parentStory;
        // Get the paragraph's index.
        var iPos = oPgf2.index;

        // Display the contents of the paragraph before the table anchor.
        alert (oStory.paragraphs[iPos-1].contents);
      }


      Rick Quatro
      585-659-8267
        • 1. Re: [CS3 JS] Finding paragraph containing table
          Level 1
          That's because the index is a count of the characters in the story. You need:

          alert(oStory.characters[iPos-1].paragraphs[0].contents);

          Dave
          • 2. Re: [CS3 JS] Finding paragraph containing table
            frameexpert Level 4
            OK, I think I get it. How do I get the index of the paragraph so I can loop through the paragraph's previous to the table? Thanks.

            Rick
            • 3. Re: [CS3 JS] Finding paragraph containing table
              frameexpert Level 4
              I found a previous post that is helping me with this.

              // Paragraph inside of table.
              
              var oPgf = app.selection[0].paragraphs[0];
              // Make sure the paragraph is inside a table
              if (oPgf.parent.constructor.name === "Cell") {
                var oCell = oPgf.parent;
                var oTbl = oCell.parent;
                // Get the paragraph containing the table anchor.
                var oPgf2 = oTbl.storyOffset.paragraphs[0];
                // Loop from the table anchor to the top of the story.
                while (oPgf2) {
                  $.writeln(oPgf2.constructor.name);
                  oPgf2 = oPgf2.parentStory.paragraphs.previousItem(oPgf2);
                }
              }


              This basically works, except this line gives an "Object is invalid" error when it gets to the beginning of the story:

              oPgf2 = oPgf2.parentStory.paragraphs.previousItem(oPgf2);
              


              I can use a try statement, but I am wondering why the loop doesn't exit gracefully without an error. Thanks.

              Rick