 | OO-Snippets: Commons| Keywords | Calc, cells, spreadsheet, merge, join, same, equal, content, formula, value, string |
|---|
| Language | OOBasic |
|---|
| Application | Calc |
|---|
| Authors | Michael Hoennig (initial)
Tom Schindl
|
|---|
| Supported Versions | |
|---|
| Supported OS | |
|---|
| Question |
How can I merge subsequent cells with same value?
E.g. a table like:
A A 1
A B 1
A B 1
A B 1
A B 2
B A 1
B A 1
B A 1
B A 2
B B 1
B B 2
B B 2
Will be transformed into:
A A 1
B 1
2
B A 1
2
B 1
2
|
|---|
| Answer |
The following macro joins all cells in the same column which are
next to each other and have the same value. For each joined area,
ehe macro continunes with subsequent columns as long as it stays
in the selection.
This first macro works on the current selection.
It calls the subroutine defined below for the actual merge process.
The following macro is the actual implementation of the merge process.
It could be used programmatically as it receives the document and selection to work on as arguments.
|
|---|
Sub mergeEqualCellsInSelectedColumns
aDoc = StarDesktop.CurrentComponent
aSelection = aDoc.getCurrentSelection()
aArea = aSelection.getRangeAddress()
mergeEqualCellsInColumns( aDoc, aArea )
End Sub
Sub mergeEqualCellsInColumns( aDoc as object, aArea as com.sun.star.table.CellRangeAddress )
aSheet = aDoc.getSheets().getByIndex( aArea.Sheet )
for nRow = aArea.StartRow to aArea.EndRow
nCol = aArea.StartColumn
aStartCell = aSheet.getCellByPosition( nCol, nRow )
aCursor = aSheet.createCursorByRange( aStartCell )
nCompareRow = nRow+1
while ( nCompareRow <= aArea.EndRow and _
aSheet.getCellByPosition( nCol, nCompareRow ).getFormula() = aStartCell.getFormula() )
nCompareRow = nCompareRow + 1
wend
nLastEqualRow = nCompareRow-1
if ( nLastEqualRow > nRow ) then
aRange = aSheet.getCellRangeByPosition( nCol, nRow, nCol, nLastEqualRow )
aRange.merge(true)
aRange.VertJustify = 1
if nCol < aArea.EndColumn then
dim aSubArea as new com.sun.star.table.CellRangeAddress
aSubArea.Sheet = aArea.Sheet
aSubArea.StartRow = nRow
aSubArea.EndRow = nLastEqualRow
aSubArea.StartColumn = nCol+1
aSubArea.EndColumn = aArea.EndColumn
mergeEqualCellsInColumns( aDoc, aSubArea )
endif
endif
nRow = nLastEqualRow
next
End Sub
|
Changelog| Date | User | Modification |
|---|
| 2004-06-22 | tomsontom | Modified to match new snippet-DTD | | 0000-00-00 | mi | initial release |
|