[Solved] Get cell width

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Arthas
Posts: 4
Joined: Mon Jun 27, 2022 9:18 pm

[Solved] Get cell width

Post by Arthas »

How can I get a cell witdh? This is my (broken) code:

Code: Select all

REM  *****  BASIC  *****

Sub Main

Dim Doc As Object
Dim DestSheet As Object
Dim w As Long
 
Doc = StarDesktop.CurrentComponent

DestSheet = Doc.Sheets(1)

w = DestSheet.getCellRangeByName("A1").Width

End Sub
Basic runtime error, Property or method not found: Width
Thanks in advance
Last edited by MrProgrammer on Mon Jun 27, 2022 11:08 pm, edited 2 times in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
Apache OpenOffice 4.1.12 on Windows 10
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Get cell width

Post by FJCC »

The width is a property of the column, not of a cell. Here is code with two routes to get to a column.

Code: Select all

 oSheets = ThisComponent.getSheets()
  oObj1 = oSheets.getByName("Sheet1")
  
  'Method 1
  oColumns = oObj1.getColumns()
  oObj2 = oColumns.getByName("A")
  nWidth = oObj2.Width
  
  'Method 2
  oCellRangeByName = oObj1.getCellRangeByName("A1")
    oColumns2 = oCellRangeByName.getColumns()
  oObj3 = oColumns2.getByName("A")
  nWidth = oObj3.Width
  
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.
Arthas
Posts: 4
Joined: Mon Jun 27, 2022 9:18 pm

Re: Get cell width

Post by Arthas »

Can I select two adjacent column, and his total width? There is certainly a better method than this

Code: Select all

 oSheets = ThisComponent.getSheets()
  oObj1 = oSheets.getByName("Sheet1")
  
  'Method 1
  oColumns = oObj1.getColumns()
  oObj2 = oColumns.getByName("A")
  nWidth = oObj2.Width
  
  'Method 2
  oCellRangeByName = oObj1.getCellRangeByName("A1")
    oColumns2 = oCellRangeByName.getColumns()
  oObj3 = oColumns2.getByName("A")
  nWidth = oObj3.Width
  
w = 0
For I = 1 To 2
  w = w + oObj1.Columns(I).Width
Next I
REM now w is colum B width + column C width
  
Last edited by Arthas on Mon Jun 27, 2022 11:08 pm, edited 1 time in total.
Apache OpenOffice 4.1.12 on Windows 10
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Get cell width

Post by FJCC »

I do not know of a better method than iterating over the columns of the cell range. I do that in the code below. Notice that the column index still starts at 0 though the cell range is B1:C1 because I am iterating over the columns of the range not the columns of the Sheet.

Code: Select all

oSheet = ThisComponent.Sheets.getByName("Sheet1")
oRng = oSheet.getCellrangeByName("B1:C1")
oCols = oRng.getColumns()
ColCount = oCols.Count
w = 0
for I = 0 to ColCount - 1
  Column = oCols.getByIndex(I)
  w = w + Column.Width

next I
print w
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.
User avatar
Lupp
Volunteer
Posts: 3549
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Solved] Get cell width

Post by Lupp »

There are many kinds of objects which have a .Size property. Some have additional similar properties using different units.
A com.sun.star.Sheet.SheetCellRange has a .Size of type com.sun.star.awt.Size . The used unit is "by concept" 10 µm (0.01 mm), but the relation to the actual size on screen or in print may depend.

Code: Select all

Sub showSizeOf(Optional pObj)
REM Only a primitive demo which can't treat selctions returned as containers.
If IsMissing(pObj) Then pObj = ThisComponent.CurrentSelection
sz = tryGetSize(ThisComponent.CurrentSelection)
If NOT IsNull(sz) Then
 With sz
  MsgBox("(Width;Height)=(" & .Width & ";" & .Height & ")")
 End With
Else
 MsgBox("Selected object has no 'Size'.")
EndIf
End Sub

Function tryGetSize(pObj) As Object
On Local Error Goto fail
tryGetSize = pObj.Size
fail:
End Function
===Editing, @FJCC===
Sorry. I missed to emphasize that

Code: Select all

com.sun.star.sheet.SheetCellRange
is one of the services knowing the .Size property in the above mentioned sense.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply