如何检查书签是否包含 table?
How to check if a bookmark contains a table?
是否可以确定何时 Bookmark
contains a TextTable
? I thought about looking through XTextRange
对象,但是文档没有指定任何关于比较范围的内容。
所以从下面的代码开始:
XBookmarksSupplier xBookmarksSupplier =
(XBookmarksSupplier)UnoRuntime.queryInterface(
XBookmarksSupplier.class, xComponent);
XNameAccess xNamedBookmarks = xBookmarksSupplier.getBookmarks();
Object bookmark = xNamedBookmarks.getByName("TextAndTable");
XTextContent xBookmarkContent = (XTextContent)UnoRuntime.queryInterface(
XTextContent.class, bookmark);
XTextRange xTextRange = xBookmarkContent.getAnchor();
你好像在问如何判断xTextRange
指定的范围内是否有一个或多个文本表格。
为此,我通常使用View Cursor。速度慢,但是普通的文本游标无法遍历不同的文本对象,所以如果范围内有文本表格或其他对象,就会失败。
这是我的Pythonclass,也许你可以适应Java:
def differentPlaces(oCurs1, oCurs2):
"""Test using compareRegion to see if two cursors are in different places.
If compareRegion fails, such as after a nested table, return False.
"""
try:
oText = oCurs1.getText()
return oText.compareRegionEnds(oCurs1, oCurs2) != 0
except IllegalArgumentException:
logger.info("Could not compare region.")
return False
class RangeCompare:
"""Compare the viewcursor to a text range (a location).
Can be useful when traversing a cursor over a range.
The range is expected not to be modified.
"""
def __init__(self, rangeEnd, viewCursor):
self.oVC = viewCursor
self.rangeEnd = rangeEnd
self.endX = -1
self.endY = -1
def getCoords(self):
if self.endY > -1:
return
# remember where we were, because we'll need to use the viewcursor
originalVC = self.oVC.getText().createTextCursorByRange(self.oVC)
self.oVC.gotoRange(self.rangeEnd, False)
self.endX = self.oVC.getPosition().X
self.endY = self.oVC.getPosition().Y
self.oVC.gotoRange(originalVC, False)
def compareVC(self):
"""Compare the viewcursor to the range.
Assume we are travelling with the viewcursor.
See if it is up to the end yet or not.
The comparison is done by checking the physical position of the cursor.
Returns -1 if the VC location is less than self.rangeEnd, 0 if it is
the same, and 1 if it is greater.
Returns -2 if they are on the same line but not in the same spot, and
it's not certain which location is greater.
"""
self.getCoords()
curX = self.oVC.getPosition().X
curY = self.oVC.getPosition().Y
if curY < self.endY:
logger.debug("%d < %d", curY, self.endY)
return -1
elif curY > self.endY:
logger.debug("%d > %d", curY, self.endY)
return 1
elif curY == self.endY:
if curX == self.endX:
if differentPlaces(self.oVC, self.rangeEnd):
# There is probably a word-final diacritic that doesn't
# change the position, so we're not to the end yet.
logger.debug("Including word-final diacritic.")
return -2
# We're at the end.
logger.debug(
"VC same loc as text range (%d, %d).", curX, curY)
return 0
else:
logger.debug("Probably haven't gone far enough.")
return -2
用 rangeCompare = RangeCompare(xTextRange.getEnd(), xViewCursor)
之类的东西实例化 class。
将视图光标移动到 xTextRange
的开头。然后一直循环调用xViewCursor.goRight(1, False)
。每次检查光标是否位于 TextTable 中。在 rangeCompare.compareVC() > 0
.
时停止
是否可以确定何时 Bookmark
contains a TextTable
? I thought about looking through XTextRange
对象,但是文档没有指定任何关于比较范围的内容。
所以从下面的代码开始:
XBookmarksSupplier xBookmarksSupplier =
(XBookmarksSupplier)UnoRuntime.queryInterface(
XBookmarksSupplier.class, xComponent);
XNameAccess xNamedBookmarks = xBookmarksSupplier.getBookmarks();
Object bookmark = xNamedBookmarks.getByName("TextAndTable");
XTextContent xBookmarkContent = (XTextContent)UnoRuntime.queryInterface(
XTextContent.class, bookmark);
XTextRange xTextRange = xBookmarkContent.getAnchor();
你好像在问如何判断xTextRange
指定的范围内是否有一个或多个文本表格。
为此,我通常使用View Cursor。速度慢,但是普通的文本游标无法遍历不同的文本对象,所以如果范围内有文本表格或其他对象,就会失败。
这是我的Pythonclass,也许你可以适应Java:
def differentPlaces(oCurs1, oCurs2):
"""Test using compareRegion to see if two cursors are in different places.
If compareRegion fails, such as after a nested table, return False.
"""
try:
oText = oCurs1.getText()
return oText.compareRegionEnds(oCurs1, oCurs2) != 0
except IllegalArgumentException:
logger.info("Could not compare region.")
return False
class RangeCompare:
"""Compare the viewcursor to a text range (a location).
Can be useful when traversing a cursor over a range.
The range is expected not to be modified.
"""
def __init__(self, rangeEnd, viewCursor):
self.oVC = viewCursor
self.rangeEnd = rangeEnd
self.endX = -1
self.endY = -1
def getCoords(self):
if self.endY > -1:
return
# remember where we were, because we'll need to use the viewcursor
originalVC = self.oVC.getText().createTextCursorByRange(self.oVC)
self.oVC.gotoRange(self.rangeEnd, False)
self.endX = self.oVC.getPosition().X
self.endY = self.oVC.getPosition().Y
self.oVC.gotoRange(originalVC, False)
def compareVC(self):
"""Compare the viewcursor to the range.
Assume we are travelling with the viewcursor.
See if it is up to the end yet or not.
The comparison is done by checking the physical position of the cursor.
Returns -1 if the VC location is less than self.rangeEnd, 0 if it is
the same, and 1 if it is greater.
Returns -2 if they are on the same line but not in the same spot, and
it's not certain which location is greater.
"""
self.getCoords()
curX = self.oVC.getPosition().X
curY = self.oVC.getPosition().Y
if curY < self.endY:
logger.debug("%d < %d", curY, self.endY)
return -1
elif curY > self.endY:
logger.debug("%d > %d", curY, self.endY)
return 1
elif curY == self.endY:
if curX == self.endX:
if differentPlaces(self.oVC, self.rangeEnd):
# There is probably a word-final diacritic that doesn't
# change the position, so we're not to the end yet.
logger.debug("Including word-final diacritic.")
return -2
# We're at the end.
logger.debug(
"VC same loc as text range (%d, %d).", curX, curY)
return 0
else:
logger.debug("Probably haven't gone far enough.")
return -2
用 rangeCompare = RangeCompare(xTextRange.getEnd(), xViewCursor)
之类的东西实例化 class。
将视图光标移动到 xTextRange
的开头。然后一直循环调用xViewCursor.goRight(1, False)
。每次检查光标是否位于 TextTable 中。在 rangeCompare.compareVC() > 0
.