interface XMergeableCell

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
skar-1980
Posts: 4
Joined: Fri Nov 13, 2015 7:04 pm

interface XMergeableCell

Post by skar-1980 »

Hello!

I'm trying to understand how to work with single cell in merged cell range. When I use .getIsMerged(), it returns TRUE only for top-left cell in merged ranged for other cell in merged range FALSE is returned. How to get, that cell is in merge.

Official says that interface XMergeableCell has .getRowSpan and .getColumnSpan methods.
http://www.openoffice.org/api/docs/comm ... eCell.html
Object Xcell return error for these methods.

Oleg.
Thx for help!
Microsoft Windows XP, Apache OpenOffice 4.1.1
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: interface XMergeableCell

Post by B Marcelly »

Hi,
Interface com.sun.star.table.XMergeableCell exists for a cell in a Writer table.
Same remark for com.sun.star.table.XMergeableCellRange.

Calc uses interfaces com.sun.star.util.XMergeable
See Merging Cell Ranges into a Single Cell

See also method collapseToMergedArea() of interface com.sun.star.sheet.XSheetCellCursor.
Developer's Guide Cell Cursor, Cursor Movement.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
skar-1980
Posts: 4
Joined: Fri Nov 13, 2015 7:04 pm

Re: interface XMergeableCell

Post by skar-1980 »

Thx Bernard! It's first time I use Cursor and it takes time to understand XSheet.CreateCursorByRange().
I write in that way.
Is it possible not to check every xCell?

Code: Select all

Sub Main

GlobalScope.BasicLibraries.LoadLibrary("MRILib")
'Dim d as Object 'thiscomponent
'Dim m as Object 'CellRange to Merge
'Dim x as Object 'Cell to Find IsMerged() = True
'Dim c as Object 'Cursor
'd=createUnoService ("com.sun.star.frame.Desktop") 
d=thiscomponent
m=d.Sheets(0).getCellRangeByName("a1:x18") '(" : ")anyRange
m.merge(TRUE)

Dim column as Integer
Dim row as Long
Dim ForExit as Integer 'to exit Sub

for column% = 0 to 1024
'row = 0
	for row = 0 to 1048576 '! That's too much and get time. Ex for Range "AC1:AD16"
	x=d.Sheets(0).getCellByPosition(column,row)
	if x.IsMerged() = TRUE then
	CollapseToMergedArea(d,x,ForExit)
		if ForExit = 2 then
		exit Sub
		end If
	else
	end if
	next
next
End Sub

Function CollapseToMergedArea (d,x,Optional ForExit as Integer)
c=d.Sheets(0).createCursorByRange(x)'
c.collapseToMergedArea() = TRUE
ForExit = msgbox (c.AbsoluteName,1)
End Function
Microsoft Windows XP, Apache OpenOffice 4.1.1
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: interface XMergeableCell

Post by Villeroy »

obj.getUniqueCellFormatRanges() returns an array of range collections.
Each range collection consists of cell ranges with exact same format attributes. Obj can be any single rectangle of cells. A sheet is a rectangle of cells.
Merging cells (the visible top-left cells of a merged range where getIsMerged() returns True) appear in separate range collections.

Code: Select all

a() = oSheet.getUniqueCellFormatRanges()
for each obj in a()
 for i = 0 to obj.getCount()-1
  oRange =  obj.getByIndex(i)
  if oRange.getIsMerged() then 
  x = getMergedArea(oRange)
  etc, etc
  else
  exit for 'skip the for i loop because if this one is not merged the others aren't neither
  endif
If you want to get rid of all merges on a sheet (which is the only thing I would do with that interface):
objSheet.merge(False) is enough.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
skar-1980
Posts: 4
Joined: Fri Nov 13, 2015 7:04 pm

Re: interface XMergeableCell

Post by skar-1980 »

Ok, I'm sorry for my ignorance. Villeroy, I understand you code logically. Sheets()->Sheet()->Range()->for_loop->if->...result->end. But due to lack of exerience ...

What is "obj"? I could not find clear information about it. Could you clear it, please. Just point me way and how to find "obj as .XSheetCellRangeContainer" appeared? All what I trying to do is step by step executing macros.
"getMergedArea" function is found at topic "Lets help Calc solve insert/delete row within merged range".

Code: Select all

Function  getMergedArea (oRange)
    oCursor = oRange.getSpreadsheet().createCursorByRange(oRange)
    oCursor.collapseToMergedArea()
    getMergedAreas  = oCursor
End Function 
Thank you.
Microsoft Windows XP, Apache OpenOffice 4.1.1
Post Reply