from com.sun.star.beans import PropertyValue
from com.sun.star.table.CellContentType import EMPTY, VALUE, TEXT, FORMULA
from com.sun.star.table.CellHoriJustify import STANDARD, LEFT, CENTER, RIGHT
+from com.sun.star.table.CellVertJustify import STANDARD, TOP, CENTER as MIDDLE, BOTTOM
+from com.sun.star.awt.FontWeight import DONTKNOW, NORMAL, BOLD
-def calc2moin(ctx):
- data = []
+moinmoin_old_style = True
+
+def getRangeSize(sheet, cursor):
+ r = cursor.getRangeAddress()
+ realRange = sheet.getCellRangeByPosition(r.StartColumn, r.StartRow,
+ r.EndColumn, r.EndRow)
+ return (realRange.Columns.Count, realRange.Rows.Count)
- ## The context variable is of type XScriptContext and is available to
- ## all BeanShell scripts executed by the Script Framework
- #curdoc = XSCRIPTCONTEXT.getDocument()
- #ctx = uno.getComponentContext()
+def getCellRealPosition(cursor, column, row):
+ r = cursor.getRangeAddress()
+ return (r.StartColumn + column, r.StartRow + row)
+
+def cell2moin(cell):
+ return cell.getString().replace('\n','<<BR>>')
+
+def calc2moin(ctx):
+ data = list()
# récupération du document en cours, vérification du type Spreadsheet
smgr = ctx.ServiceManager
curdoc = desktop.getCurrentComponent()
if not curdoc.supportsService("com.sun.star.sheet.SpreadsheetDocument"):
raise RuntimeError, u"Ce n'est pas un document de type Spreadsheet (Calc)."
-
controller = curdoc.getCurrentController()
sheet = controller.getActiveSheet()
cursor = sheet.createCursor()
cursor.gotoEndOfUsedArea(False)
cursor.gotoStartOfUsedArea(True)
- rangeAddress = cursor.getRangeAddress()
- rows = rangeAddress.EndRow - rangeAddress.StartRow + 1
- columns = rangeAddress.EndColumn - rangeAddress.StartColumn + 1
+ columns, rows = getRangeSize(sheet, cursor)
for row in range(rows):
- row_data = []
- for column in range(columns):
- cell_data = []
+ row_data = list()
+ column = 0
+ while column < columns:
cell = cursor.getCellByPosition(column, row)
cell_type = cell.getType()
cell_string = cell.getString()
- # process cell' styles
- cell_styles = []
- if hasattr(cell, 'CellBackColor'):
- color = cell.getPropertyValue('CellBackColor')
- if color >= 0:
- cell_styles.append('background: #%06x' % color)
- if hasattr(cell, 'HoriJustify'):
+ # calcul de l'étendue de la cellule courante
+ if cell.getIsMerged():
+ c, r = getCellRealPosition(cursor, column, row)
+ cellRange = sheet.getCellRangeByPosition(c, r, c, r)
+ cursor2 = sheet.createCursorByRange(cellRange)
+ cursor2.collapseToMergedArea()
+ cell_colspan, cell_rowspan = getRangeSize(sheet, cursor2)
+ else:
+ cell_colspan, cell_rowspan = 1, 1
+ # traitement des attributs et styles de la cellule
+ cell_attributes = list()
+ cell_styles = list()
+ if cell_string and hasattr(cell, 'HoriJustify'):
horiJustify = cell.getPropertyValue('HoriJustify')
if horiJustify == LEFT:
- text_align = '' #text_align = 'left'
+ if cell_colspan > 1 or cell_rowspan > 1:
+ text_align = 'left'
+ text_align_old = '('
+ else:
+ text_align = '' # left par défaut si span == 1
+ text_align_old = ''
elif horiJustify == CENTER:
- text_align = 'center'
+ if cell_colspan == 1 and cell_rowspan == 1:
+ text_align = 'center'
+ text_align_old = ':'
+ else:
+ text_align = '' # center par défaut si span > 1
+ text_align_old = ''
elif horiJustify == RIGHT:
text_align = 'right'
+ text_align_old = ')'
else:
if cell_type == TEXT:
- text_align = '' #text_align = 'left'
+ text_align = 'left'
+ text_align_old = '('
elif cell_type != EMPTY:
text_align = 'right'
- if text_align and cell_string:
- cell_styles.append('text-align: ' + text_align)
- # add cell to row
+ text_align_old = ')'
+ if text_align:
+ if moinmoin_old_style:
+ cell_attributes.append(text_align_old)
+ else:
+ cell_styles.append('text-align: ' + text_align)
+ if cell_string and hasattr(cell, 'VertJustify'):
+ vertJustify = cell.getPropertyValue('VertJustify')
+ if vertJustify == 1: #TOP(1)
+ vertical_align = 'top'
+ vertical_align_old = '^'
+ elif vertJustify == 2: #MIDDLE(2)
+ vertical_align = '' # middle par défaut
+ vertical_align_old = ''
+ else: #STANDARD(0), BOTTOM(3)
+ vertical_align = 'bottom'
+ vertical_align_old = 'v'
+ if vertical_align:
+ if moinmoin_old_style:
+ cell_attributes.append(vertical_align_old)
+ else:
+ cell_styles.append('vertical-align: ' + vertical_align)
+ if cell_string and hasattr(cell, 'CharWeight'):
+ charWeight = cell.getPropertyValue('CharWeight')
+ if charWeight == BOLD:
+ if moinmoin_old_style:
+ # FIXME: pas utilisé ensuite !!
+ cell_string = "'''%s'''" % cell_string
+ else:
+ cell_styles.append('font-weight: bold')
+ if cell_rowspan > 1:
+ cell_attributes.append('|%d' % cell_rowspan)
+ if cell_colspan > 1:
+ cell_attributes.append('-%d' % cell_colspan)
+ if hasattr(cell, 'CellBackColor'):
+ color = cell.getPropertyValue('CellBackColor')
+ if color >= 0:
+ if moinmoin_old_style:
+ cell_attributes.append('#%06x' % color)
+ else:
+ cell_styles.append('background: #%06x' % color)
+ if hasattr(cell, 'CharColor'):
+ color = cell.getPropertyValue('CharColor')
+ if color >= 0:
+ cell_styles.append('color: #%06x' % color)
+ # compilation des styles de la cellule
if cell_styles:
- cell_data.append('<style="%s;">' % '; '.join(cell_styles))
+ cell_attributes.insert(0, 'style="%s;"' % '; '.join(cell_styles))
+ # ajout de la définition de la cellule à la ligne courante
+ cell_data = list()
+ if cell_attributes:
+ cell_data.append('<' + ''.join(cell_attributes) + '>')
if cell_string:
- cell_data.append(cell_string)
+ cell_data.append(cell2moin(cell))
else:
cell_data.append(' ')
row_data.append(''.join(cell_data))
+ column += cell_colspan
# display row's code
data.append('||' + '||'.join(row_data) + '||')
calc2moin(ctx)
return None
+# lists the scripts, that shall be visible inside OOo. Can be omited, if
+# all functions shall be visible, however here getNewString shall be surpressed
g_exportedScripts = (copier, )
##############################################################################