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

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Tim Grantham
Posts: 32
Joined: Thu Jan 06, 2011 12:03 am

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

Post 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
Last edited by Tim Grantham on Wed Jun 08, 2011 12:11 am, edited 1 time in total.
OpenOffice 4.1
Windows 7 Professional
Tim Grantham
Posts: 32
Joined: Thu Jan 06, 2011 12:03 am

Re: How do I apply autoformats to text tables?

Post 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.
OpenOffice 4.1
Windows 7 Professional
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: How do I apply autoformats to text tables?

Post 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.
Apache OpenOffice 4.1.1
Windows XP
Tim Grantham
Posts: 32
Joined: Thu Jan 06, 2011 12:03 am

Re: How do I apply autoformats to text tables?

Post 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.
OpenOffice 4.1
Windows 7 Professional
FJCC
Moderator
Posts: 9277
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How do I apply autoformats to text tables?

Post 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")
  
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Tim Grantham
Posts: 32
Joined: Thu Jan 06, 2011 12:03 am

Re: How do I apply autoformats to text tables?

Post 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.
OpenOffice 4.1
Windows 7 Professional
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: How do I apply autoformats to text tables?

Post 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.
Apache OpenOffice 4.1.1
Windows XP
FJCC
Moderator
Posts: 9277
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How do I apply autoformats to text tables?

Post 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();
	}
}
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Tim Grantham
Posts: 32
Joined: Thu Jan 06, 2011 12:03 am

Re: How do I apply autoformats to text tables?

Post 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.
OpenOffice 4.1
Windows 7 Professional
Post Reply