Below, an example to manage the selection in calc, whatever it is
(simple cell, simple range or multiple ranges).
(simple cell, simple range or multiple ranges).
| Code: |
Reference <XSelectionSupplier> xSelection (m_xController, UNO_QUERY); Reference <XServiceInfo> xServiceInfo; xSelection->getSelection() >>= xServiceInfo; strcpy(anImplementationName, OUStringToOString(xServiceInfo->getImplementationName(), RTL_TEXTENCODING_ISO_8859_1).getStr()); // printf ("Service implementation : %s", anImplementationName); if (strcmp(anImplementationName, "ScCellObj") == 0) { // Selection of a single cell Reference <XText> aCellText (xSelection->getSelection(), UNO_QUERY); aCellText->setString(OUString::createFromAscii("TEST1")); } else if (strcmp(anImplementationName, "ScCellRangeObj") == 0) { // Selection of many cells in a single Range Reference <XCellRangeAddressable> xCellRangeAddr (xSelection->getSelection(), UNO_QUERY); int numCol, numRow; CellRangeAddress anAddr = xCellRangeAddr->getRangeAddress(); numCol = anAddr.EndColumn-anAddr.StartColumn+1; numRow = anAddr.EndRow-anAddr.StartRow+1; Reference <XCellRange> xCellRange (xSelection->getSelection(), UNO_QUERY); for (int i=0; i<numCol; i++) for (int j=0; j<numRow ; j++) { Reference <XText> aCellText (xCellRange->getCellByPosition(i,j), UNO_QUERY); aCellText->setString(OUString::createFromAscii("TEST2")); } } else if (strcmp(anImplementationName, "ScCellRangesObj") == 0) { // Selection of many cells in many Ranges Reference <XEnumerationAccess> anEnumerationAccess (xSelection->getSelection(), UNO_QUERY); Reference <XEnumeration> anEnumeration = anEnumerationAccess->createEnumeration(); while (anEnumeration->hasMoreElements()) { Reference <XCellRangeAddressable> xCellRangeAddr (anEnumeration->nextElement(), UNO_QUERY); int numCol, numRow; CellRangeAddress anAddr = xCellRangeAddr->getRangeAddress(); numCol = anAddr.EndColumn-anAddr.StartColumn+1; numRow = anAddr.EndRow-anAddr.StartRow+1; Reference <XCellRange> xCellRange (xCellRangeAddr, UNO_QUERY); for (i=0; i<numCol; i++) for (j=0; j<numRow ; j++) { Reference <XText> aCellText (xCellRange->getCellByPosition(i,j), UNO_QUERY); aCellText->setString(OUString::createFromAscii("TEST3")); } } } |
本文介绍了一个用于管理 Calc 应用程序中不同类型的单元格选择(包括单个单元格、单个范围和多个范围)的代码示例。通过检查选择的具体类型,该代码能够在不同类型的选择上执行操作。

被折叠的 条评论
为什么被折叠?



