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

Creating a macro - Writing a Script - Using the API

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

Postby Tim Grantham » Tue Jun 07, 2011 1:11 am

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: 30
Joined: Thu Jan 06, 2011 12:03 am

Re: How do I apply autoformats to text tables?

Postby Tim Grantham » Tue Jun 07, 2011 3:59 am

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
Tim Grantham
 
Posts: 30
Joined: Thu Jan 06, 2011 12:03 am

Re: How do I apply autoformats to text tables?

Postby Charlie Young » Tue Jun 07, 2011 4:01 am

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   Expand viewCollapse view
oTable = ThisComponent.TextTables.getByIndex(0)


then

Code: Select all   Expand viewCollapse view
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
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: How do I apply autoformats to text tables?

Postby Tim Grantham » Tue Jun 07, 2011 4:04 pm

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
Tim Grantham
 
Posts: 30
Joined: Thu Jan 06, 2011 12:03 am

Re: How do I apply autoformats to text tables?

Postby FJCC » Tue Jun 07, 2011 4:59 pm

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   Expand viewCollapse view
  oTextTables = oInitialTarget.TextTables
  oObj_1 = oTextTables.getByName("Table1")
  oObj_1.autoFormat("Green")
 
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7158
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How do I apply autoformats to text tables?

Postby Tim Grantham » Tue Jun 07, 2011 7:51 pm

I assumed that what was available in Basic was also available in Java. Apparently not.

Here is my Java code:
Code: Select all   Expand viewCollapse view
        // 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.
TextTable methods.png
screen capture of available methods on XTextTable class


Thanks,
Tim.
OpenOffice 4.1
Windows 7 Professional
Tim Grantham
 
Posts: 30
Joined: Thu Jan 06, 2011 12:03 am

Re: How do I apply autoformats to text tables?

Postby Charlie Young » Tue Jun 07, 2011 8:04 pm

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

Re: How do I apply autoformats to text tables?

Postby FJCC » Tue Jun 07, 2011 8:36 pm

As suggested by Charlie, you have to query the right interface. Here is a Java version I recorded in MRI
Code: Select all   Expand viewCollapse view
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();
   }
}
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7158
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How do I apply autoformats to text tables?

Postby Tim Grantham » Wed Jun 08, 2011 12:09 am

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   Expand viewCollapse view
        // 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
Tim Grantham
 
Posts: 30
Joined: Thu Jan 06, 2011 12:03 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 10 guests