Page 1 of 1

[Solved] How do I apply autoformats to text tables?

Posted: Tue Jun 07, 2011 1:11 am
by Tim Grantham
The developer guide says in two places that you can apply autoformats to text tables. But there is no description how to do this. There is a code sample that shows how to apply autoformats to cells in spreadsheet tables, but this doesn't appear to work for text tables, because the autoformat() method requires a cell range as input, and there is no method on TextTable that directly supports cell ranges.

Can anyone help?

Thanks,
Tim

Re: How do I apply autoformats to text tables?

Posted: Tue Jun 07, 2011 3:59 am
by Tim Grantham
Tim Grantham wrote:... the autoformat() method requires a cell range as input...
I meant to say that creating an instance of the XAutoformattable interface requires a cell range as a context, not autoformat().

But this doesn't change the problem that there is apparently no method on the XTextTable interface to get a cell range as a context.

I know OpenOffice Writer can do this, because I have applied an autoformat to a table in a text document through the user interface. I need to be able to do this programmatically for every table in the text document.

Thanks,
Tim.

Re: How do I apply autoformats to text tables?

Posted: Tue Jun 07, 2011 4:01 am
by Charlie Young
Tim Grantham wrote:The developer guide says in two places that you can apply autoformats to text tables. But there is no description how to do this. There is a code sample that shows how to apply autoformats to cells in spreadsheet tables, but this doesn't appear to work for text tables, because the autoformat() method requires a cell range as input, and there is no method on TextTable that directly supports cell ranges.

Can anyone help?

Thanks,
Tim
Get the table, say

Code: Select all

oTable = ThisComponent.TextTables.getByIndex(0)
then

Code: Select all

oTable.autoformat("Format Name")
where "Format Name" is one of the names in the autoformat collection, which I discussed in my reply to your other post on the topic.

Re: How do I apply autoformats to text tables?

Posted: Tue Jun 07, 2011 4:04 pm
by Tim Grantham
Thanks again for your help Charlie.

I don't understand how the second part would work, since autoformat() is not a method that's available on TextTable objects.

Tim.

Re: How do I apply autoformats to text tables?

Posted: Tue Jun 07, 2011 4:59 pm
by FJCC
The autoFormat method is available in a TextTable. I just recorded this Basic code with MRI and it changes the format of the chosen table. oInitialTarget is the text document.

Code: Select all

  oTextTables = oInitialTarget.TextTables
  oObj_1 = oTextTables.getByName("Table1")
  oObj_1.autoFormat("Green")
  

Re: How do I apply autoformats to text tables?

Posted: Tue Jun 07, 2011 7:51 pm
by Tim Grantham
I assumed that what was available in Basic was also available in Java. Apparently not.

Here is my Java code:

Code: Select all

        // Get the TextTablesSupplier interface of the document
        XTextTablesSupplier xTableSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xDoc);

            // Get an XIndexAccess of TextTables
            XIndexAccess xTables = (XIndexAccess)UnoRuntime.queryInterface(
                XIndexAccess.class, xTableSupplier.getTextTables());

        int tablecount = xTables.getCount();

        for (int i=0; i < tablecount; i++) {
            XTextTable xTextTable = ( XTextTable ) UnoRuntime.queryInterface (
                XTextTable.class, xTables.getByIndex( i ) );
            
        }
Entering "xTextTable." as the next statement in the for loop in my NetBeans Java editor pulls up a list of methods and properties that does not contain autoFormat(). See the attached image for what is available.
screen capture of available methods on XTextTable class
screen capture of available methods on XTextTable class
Thanks,
Tim.

Re: How do I apply autoformats to text tables?

Posted: Tue Jun 07, 2011 8:04 pm
by Charlie Young
Query your table on the interface XAutoFormattable, that should get you an object with the desired method. I had tested this in Basic, not java. I'll look some more if you still have problems.

Re: How do I apply autoformats to text tables?

Posted: Tue Jun 07, 2011 8:36 pm
by FJCC
As suggested by Charlie, you have to query the right interface. Here is a Java version I recorded in MRI

Code: Select all

import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XNameAccess;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.IndexOutOfBoundsException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.table.XAutoFormattable;
import com.sun.star.text.XTextTable;
import com.sun.star.text.XTextTablesSupplier;
import com.sun.star.uno.UnoRuntime;

static public void snippet(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));
		
		XAutoFormattable xAutoFormattable = UnoRuntime.queryInterface(
			XAutoFormattable.class, xTextTable);
		
		xAutoFormattable.autoFormat("Green");
		
	}
	catch (WrappedTargetException e1)
	{
		// getByIndex
		e1.printStackTrace();
	}
	catch (IllegalArgumentException e2)
	{
		// autoFormat
		e2.printStackTrace();
	}
	catch (IndexOutOfBoundsException e3)
	{
		// getByIndex
		e3.printStackTrace();
	}
}

Re: How do I apply autoformats to text tables?

Posted: Wed Jun 08, 2011 12:09 am
by Tim Grantham
Thanks to help from Charlie and from my ace brother Simon, I got this working. Here is the Java code I use:

Code: Select all

        // Get the TextTablesSupplier interface of the document
        XTextTablesSupplier xTableSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xDoc);

            // Get an XIndexAccess of TextTables
            XIndexAccess xTables = (XIndexAccess)UnoRuntime.queryInterface(
                XIndexAccess.class, xTableSupplier.getTextTables());

        int tablecount = xTables.getCount();

        for (int i=0; i < tablecount; i++) {

            XAutoFormattable xAutoFormattable = ( XAutoFormattable ) UnoRuntime.queryInterface (
                XAutoFormattable.class, xTables.getByIndex( i ) );

            xAutoFormattable.autoFormat("simpletable_header");

        }
I see this is virtually identical to what FJCC has posted. Validation! :bravo:

Tim.