2016-05-30 51 views
0

是否有可能確定何時Bookmark包含TextTable?我想通過XTextRange對象來查看,但是,文檔沒有指定任何關於比較範圍的內容。如何檢查書籤是否包含表格?

+0

可以肯定的是,你問書籤是否位於* TextTable內?這很容易確定。我在下面的回答中假設你的意思是*包含*,如問題所述,並且書籤橫跨一系列文本而不僅僅是一個位置。 –

+0

不,我在**書籤**中詢問**表**。 – soon

回答

1

所以先從下面的代碼:

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。這很慢,但普通文本光標無法遍歷不同的文本對象,所以如果範圍內有文本表或其他對象,它將會失敗。

這裏是我的Python類,這或許可以適應的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)

將視圖光標移動到xTextRange的開頭。然後繼續撥打xViewCursor.goRight(1, False)進行循環。每次檢查光標是否位於TextTable中。當rangeCompare.compareVC() > 0停止。

+0

謝謝你的回答,我會盡快檢查 – soon