Hi everyone,
I have a small problem. I am creating a sCalc sheet with 10 columns using code. After the creation, I want to Autosize the columns so that they adjust to the content with maximum width. However, I want only the 2nd columns onwards to autofit. The code I use is :
FOR mcounter = 1 TO last_field
oCalc.oCurrentSheet.Columns(mcounter).OptimalWidth=.T.
ENDFOR
The problem is, the moment the 2nd column's [i.e. Column(1)] OptimalWidth width is set to true, all columns automatically autofit.
Any help?
Abhay
[Solved] [Calc] Problem with OptimalWidth
-
- Posts: 17
- Joined: Tue Jun 01, 2010 10:10 am
[Solved] [Calc] Problem with OptimalWidth
Last edited by Hagar Delest on Mon Jun 07, 2010 11:41 pm, edited 1 time in total.
Reason: tagged [Solved].
Reason: tagged [Solved].
OpenOffice 3.0 on XP
Re: Problem with OptimalWidth
I suppose there is something wrong with your loop. I used a test macro to only modify the 2. column in a spreadsheet with 8 or 9 columns:
And for my spreadsheet only the second column B changed in the width. I suggest you simplify your code to apply the autofitting only two a single column and see if you still get the same behaviour that all other columns change their width.
If the problems is not the loop the second candidate is oCurrentSheet.Columns(mcounter). I usually try to avoid pseudo properties. They are only convenient until something stops working.
Code: Select all
Sub FitColumnTwo()
Dim oSheets As Object, oSheet As Object
Dim oCols As Object, oCol As Object
oSheets = ThisComponent.getSheets()
oSheet = oSheets.getByIndex(0)
oCols = oSheet.getColumns()
oCol = oCols.getByIndex(1)
oCol.OptimalWidth = True
End Sub
If the problems is not the loop the second candidate is oCurrentSheet.Columns(mcounter). I usually try to avoid pseudo properties. They are only convenient until something stops working.
Edit: The OptimalWidth property can be set for the column collection as well: oCurrentSheet.Columns.OptimalWidth = True is executed as correct Basic code (and autofits all columns). Chances are that the mcounter in your code is ignored by the basic interpreter and it actually interprets: oCurrentSheet.Columns().OptimalWidth = True |
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
-
- Volunteer
- Posts: 1160
- Joined: Mon Oct 08, 2007 1:26 am
- Location: France, Paris area
Re: Problem with OptimalWidth
Hi,
A simpler solution (columns B to G of sheet named : xxx)
A simpler solution (columns B to G of sheet named : xxx)
Code: Select all
Dim mySheet As Object, myColumns As Object
mySheet = ThisComponent.Sheets.getByName("xxx")
myColumns = mySheet.getCellRangeByName("B1:G1").Columns
' ** if column numbers are known at execution :
' myColumns = mySheet.getCellRangeByPosition(colStart, 0, colEnd, 0)
myColumns.OptimalWidth = True
Bernard
OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
-
- Posts: 17
- Joined: Tue Jun 01, 2010 10:10 am
[Solved] Problem with OptimalWidth
Thankx Guys,
Rudolfo's solution worked for me. Strangely, when I get a column by using getByIndex(1) rather than using Columns(1), the OptimalWidth works perfectly.
Abhay
Rudolfo's solution worked for me. Strangely, when I get a column by using getByIndex(1) rather than using Columns(1), the OptimalWidth works perfectly.
Abhay
OpenOffice 3.0 on XP