Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

[CS3 JS] Finding paragraph containing table

Community Expert ,
Jan 06, 2009 Jan 06, 2009
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
TOPICS
Scripting
545
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 06, 2009 Jan 06, 2009
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2009 Jan 06, 2009
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2009 Jan 06, 2009
LATEST
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines