Page 1 of 1
[Solved] How to use XSortable to sort TextTables in Writer?
Posted: Fri May 10, 2013 7:51 am
by SNM90
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
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);
Re: How to use XSortable to sort TextTables in Writer?
Posted: Fri May 10, 2013 9:09 am
by Villeroy
The following Java has been generated by the
MRI extension:
Code: Select all
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
#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());
}
}
Re: How to use XSortable to sort TextTables in Writer?
Posted: Fri May 10, 2013 9:53 am
by SNM90
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

.