Reputation: 2027
I am working on some LibreOffice macros that work on tables, in particular to set the width and height of each column and row to 0.85 cm (0.335 in).
In MS Office, this is easy, just select the table and in the macro have:
Selection.Rows.Height = CentimetersToPoints(0.85)
Selection.Columns.PreferredWidth = CentimetersToPoints(0.85)
There isn't anything like this in LibreOffice 4.1. It appears each column/row must be adjusted individually. Two ways to do this:
Iterate through all the columns/rows and adjust each column/row
Adjust the first column/row to some carefully calculated wide width/height, then call Distribute Columns/Rows Evenly
Just to get an idea of the code, I tried using the macro recorder and went through Table | Table Properties and played around until the table looked okay, but most of what I did was not recorded in the macro.
Has anyone done something like this?
Upvotes: 5
Views: 4470
Reputation:
I am attempting to set the width of all the cells in a table to a certain value by setting the position of each row's separators using the TableColumnRelativeSum to calculate the appropriate relative Position. It's necessary to use the relative values because, as the TableColumnSeparator docs explain:
The real width of a table depends on the environment (page style and number of text columns at the table's position, alignment, and left and right margins). For that reason, the table column separator does not contain metric values for the column widths. The values are relative to the value of the property TextTable::TableColumnRelativeSum.
So I've got this code, and it runs without errors, and it seems to work. On some tables, though ("complex" ones where not all rows are identical), some separators don't get moved.
Sub Main
dim tables as object
dim table as object
dim tid as integer
' Get table
tables = ThisComponent.TextTables
table = tables.getByName("Table5")
tableWidthRelative = table.TableColumnRelativeSum
tableWidthIn = 5.5
columnWidthIn = 0.89
columnWidthRelative = columnWidthIn / tableWidthIn * tableWidthRelative
' Get rows
rows = table.getRows()
for i = 0 to (rows.Count() - 1)
row = rows.getByIndex(i)
' Seps
seps = row.TableColumnSeparators
' TableColumnSeparators is a Sequence, which does not support the Count method. You must use UBound() to get its length.
numSeps = UBound(seps)
for s = 0 to numSeps
sep = seps(s)
sep.Position = columnWidthRelative * (s+1)
seps(s) = sep
next
row.TableColumnSeparators = seps
table.Rows(i) = row
next
end sub
I'm putting this up here because it's been a real mess trying to figure it out, and maybe this will help someone someday.
In the end, what worked best was using a Bash script to send keyboard input to LibreOffice using xdotool
.
There are more details available on this question on the LibreOffice site.
Upvotes: 1
Reputation: 33
At last I got the solution for this issue...
But still dont know what the unit for Position properties.
Sub Main
dim tables as object
dim table as object
dim tid as integer
dim sep()
tables = ThisComponent.TextTables
for tid = 0 to tables.count - 1
table = tables(tid)
table.Width = 26000
sep = table.TableColumnSeparators
sep(0).Position = 1600
table.TableColumnSeparators = sep
next
End Sub
Upvotes: 3
Reputation:
Here is as far as I could get:
sub Testing
dim tables as object
dim table as object
dim columns as object
dim column as object
dim index as integer
tables = ThisComponent.TextTables
if tables.Count > 0 then
table = tables.getByIndex(0)
columns = table.columns
table.Width = 850 * columns.Count '850 == 0.85 cm
for index = 0 to columns.Count - 1
column = columns.getByIndex(index)
'column is always NULL
'column.Width = 850
next
end if
end sub
Major problems noted:
No way to retrieve the actual table you want to modify via ThisComponent.CurrentSelection
, so instead hardcoded to the table at index 0
Any changes to the table don't seem to be reflected in the document, and no obvious way to re-render or refresh Seems to be working now! But still looking for a way to call the function to distribute columns evenly
columns.getByIndex
always returns NULL
!, and there's no documentation on how to use the column enumeration class within Basic
Based on this investigation, would advise against trying to do anything productive with LibreOffice 4.1 Writer Basic macros.
Upvotes: 4