[Solved] How do I apply autoformats to text tables?
-
- Posts: 32
- Joined: Thu Jan 06, 2011 12:03 am
[Solved] How do I apply autoformats to text tables?
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
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
Windows 7 Professional
-
- Posts: 32
- Joined: Thu Jan 06, 2011 12:03 am
Re: How do I apply autoformats to text tables?
I meant to say that creating an instance of the XAutoformattable interface requires a cell range as a context, not autoformat().Tim Grantham wrote:... the autoformat() method requires a cell range as input...
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
Windows 7 Professional
- Charlie Young
- Volunteer
- Posts: 1559
- Joined: Fri May 14, 2010 1:07 am
Re: How do I apply autoformats to text tables?
Get the table, sayTim 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
Code: Select all
oTable = ThisComponent.TextTables.getByIndex(0)
Code: Select all
oTable.autoformat("Format Name")
Apache OpenOffice 4.1.1
Windows XP
Windows XP
-
- Posts: 32
- Joined: Thu Jan 06, 2011 12:03 am
Re: How do I apply autoformats to text tables?
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.
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
Windows 7 Professional
Re: How do I apply autoformats to text tables?
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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
-
- Posts: 32
- Joined: Thu Jan 06, 2011 12:03 am
Re: How do I apply autoformats to text tables?
I assumed that what was available in Basic was also available in Java. Apparently not.
Here is my Java code:
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.
Thanks,
Tim.
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 ) );
}
Tim.
OpenOffice 4.1
Windows 7 Professional
Windows 7 Professional
- Charlie Young
- Volunteer
- Posts: 1559
- Joined: Fri May 14, 2010 1:07 am
Re: How do I apply autoformats to text tables?
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
Windows XP
Re: How do I apply autoformats to text tables?
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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
-
- Posts: 32
- Joined: Thu Jan 06, 2011 12:03 am
Re: How do I apply autoformats to text tables?
Thanks to help from Charlie and from my ace brother Simon, I got this working. Here is the Java code I use:
I see this is virtually identical to what FJCC has posted. Validation!
Tim.
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");
}
Tim.
OpenOffice 4.1
Windows 7 Professional
Windows 7 Professional