OpenOffice.org Code Snippets--Get and modify the current selection in calc (in C++)

本文介绍了一个用于管理 Calc 应用程序中不同类型的单元格选择(包括单个单元格、单个范围和多个范围)的代码示例。通过检查选择的具体类型,该代码能够在不同类型的选择上执行操作。
Below, an example to manage the selection in calc, whatever it is
(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"));
      }
     }
      }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值