[Solved] How to use XSortable to sort TextTables in Writer?

Java, C++, C#, Delphi, ??? - Using the UNO bridges

[Solved] How to use XSortable to sort TextTables in Writer?

Postby SNM90 » Fri May 10, 2013 7:51 am

Hello,

I need urgent help in sorting TextTables in Writer while writing C++ code. I'm using the XSortable interface and the what I'm not able to figure out is how to use the TextSortDesciptor2 and the TableSortDescriptor2 to aid in sorting. I have a text table of 2 columns and 5 rows and have to sort column 1 numerically.

Any help in this?

The following is the code I'm trying unsuccessfully:

Code: Select all   Expand viewCollapse view
        Reference <XSortable> xSort (xTT, UNO_QUERY);

   Sequence <PropertyValue> sortPropsDefault = xSort->createSortDescriptor();

   Sequence <PropertyValue> sortProps(3);
   Sequence <TableSortField> tableSortField(1);

   tableSortField[0].Field = 0;
   tableSortField[0].FieldType = TableSortFieldType::TableSortFieldType_NUMERIC;
   tableSortField[0].IsAscending = false;

   sortProps[0].Name = OUString::createFromAscii("SortFields");
   sortProps[0].Value <<= (Sequence <TableSortField>)tableSortField;
   sortProps[1].Name = OUString::createFromAscii("IsSortColumns");
   sortProps[1].Value <<= (sal_Bool)false;
   sortProps[2].Name = OUString::createFromAscii("IsSortInTable");
   sortProps[2].Value <<= (sal_Bool)true;

   xSort->sort(sortProps);
Last edited by SNM90 on Sat May 11, 2013 9:27 am, edited 1 time in total.
OpenOffice 3.1 on Windows Vista
(Libreoffice 4.0.0 on Windows 7)
SNM90
 
Posts: 21
Joined: Fri Mar 01, 2013 6:56 am

Re: How to use XSortable to sort TextTables in Writer?

Postby Villeroy » Fri May 10, 2013 9:09 am

The following Java has been generated by the MRI extension:
Code: Select all   Expand viewCollapse view
import com.sun.star.beans.PropertyValue;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XNameAccess;
import com.sun.star.lang.IndexOutOfBoundsException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.text.XTextTable;
import com.sun.star.text.XTextTablesSupplier;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XSortable;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
   try
   {
      XTextTablesSupplier xTextTablesSupplier = UnoRuntime.queryInterface(
         XTextTablesSupplier.class, oInitialTarget);
      XNameAccess xNameAccess = xTextTablesSupplier.getTextTables();
      
      XIndexAccess xIndexAccess = UnoRuntime.queryInterface(
         XIndexAccess.class, xNameAccess);
      XTextTable xTextTable = UnoRuntime.queryInterface(
         XTextTable.class, xIndexAccess.getByIndex(0));
      
      XSortable xSortable = UnoRuntime.queryInterface(
         XSortable.class, xTextTable);
      
      PropertyValue[] aPropertyValue = xSortable.createSortDescriptor();
      
   }
   catch (WrappedTargetException e1)
   {
      // getByIndex
      e1.printStackTrce();
   }
   catch (IndexOutOfBoundsException e2)
   {
      // getByIndex
      e2.printStackTrce();
   }
}

The TextTable supports service com.sun.star.text.TextSortable so you have to query that interface from the TextTable:
XSortable xSortable = UnoRuntime.queryInterface(XSortable.class, xTextTable);

Same in C++
Code: Select all   Expand viewCollapse view
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/text/XTextTable.hpp>
#include <com/sun/star/text/XTextTablesSupplier.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/uno/XInterface.hpp>
#include <com/sun/star/util/XSortable.hpp>

using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::text;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::util;
using namespace ::rtl;

void snippet(const Reference< XComponentContext > &xContext, const Reference< XInterface > &oInitialTarget)
{
    try
    {
        Reference< XTextTablesSupplier > xTextTablesSupplier(oInitialTarget, UNO_QUERY);
        Reference< XNameAccess > xNameAccess = xTextTablesSupplier->getTextTables();
       
        Reference< XIndexAccess > xIndexAccess(xNameAccess, UNO_QUERY);
        Reference< XTextTable > xTextTable(xIndexAccess->getByIndex(0), UNO_QUERY);
        Reference< XSortable > xSortable(xTextTable, UNO_QUERY);
        Sequence< PropertyValue > aPropertyValue = xSortable->createSortDescriptor();
       
    }
    catch (WrappedTargetException &e)
    {
        // getByIndex
        //printf(OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
    }
    catch (IndexOutOfBoundsException &e)
    {
        // getByIndex
        //printf(OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
    }
}
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27366
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to use XSortable to sort TextTables in Writer?

Postby SNM90 » Fri May 10, 2013 9:53 am

Thank you for your reply.

Actually, I didn't have a problem doing a default sort as you mentioned (in fact one more line is to be added to your code for the actual sorting - xSortable->sort(xsortDesciptor)). My query was when one has to customize the sort. It seems I'm not able to get how to use the TextSortDescriptor2 and the TableSortDescriptor2 to use their properties to change my sorting mechanism.

EDIT: One thing that needed to be changed which I found by digging some forums was that the documentation has an error. The member 'Field' in the structure 'TableSortField' is 1-based not 0-based. Properties work now :).
OpenOffice 3.1 on Windows Vista
(Libreoffice 4.0.0 on Windows 7)
SNM90
 
Posts: 21
Joined: Fri Mar 01, 2013 6:56 am


Return to External Programs

Who is online

Users browsing this forum: No registered users and 2 guests