auf-libreoffice-extension 1204.3
[macros-openoffice.git] / auf-libreoffice-extension / extension / calc2moin.py
CommitLineData
4d9c5a11
P
1#!
2# -*- coding: utf-8 -*-
3"""
4Macro pour faciliter l'intégration des données d'une feuille de calcul
5vers un wiki MoinMoin.
6
7Copyright : Agence universitaire de la Francophonie
8Licence : GNU General Public Licence, version 2
9Auteur : Jean Christophe André
10Date de création : septembre 2009
11"""
12import uno
13import unohelper
14from com.sun.star.task import XJobExecutor
15from com.sun.star.beans import PropertyValue
16from com.sun.star.table.CellContentType import EMPTY, VALUE, TEXT, FORMULA
17from com.sun.star.table.CellHoriJustify import STANDARD, LEFT, CENTER, RIGHT
18
19def calc2moin(ctx):
20 data = []
21
22 ## The context variable is of type XScriptContext and is available to
23 ## all BeanShell scripts executed by the Script Framework
24 #curdoc = XSCRIPTCONTEXT.getDocument()
25 #ctx = uno.getComponentContext()
26
27 # récupération du document en cours, vérification du type Spreadsheet
28 smgr = ctx.ServiceManager
29 desktop = smgr.createInstanceWithContext(
30 "com.sun.star.frame.Desktop", ctx)
31 curdoc = desktop.getCurrentComponent()
32 if not curdoc.supportsService("com.sun.star.sheet.SpreadsheetDocument"):
33 raise RuntimeError, u"Ce n'est pas un document de type Spreadsheet (Calc)."
34
35 controller = curdoc.getCurrentController()
36 sheet = controller.getActiveSheet()
37
38 # localisation de la la zone déjà utilisée
39 cursor = sheet.createCursor()
40 cursor.gotoEndOfUsedArea(False)
41 cursor.gotoStartOfUsedArea(True)
42 rangeAddress = cursor.getRangeAddress()
43 rows = rangeAddress.EndRow - rangeAddress.StartRow + 1
44 columns = rangeAddress.EndColumn - rangeAddress.StartColumn + 1
45 for row in range(rows):
46 row_data = []
47 for column in range(columns):
48 cell_data = []
49 cell = cursor.getCellByPosition(column, row)
50 cell_type = cell.getType()
51 cell_string = cell.getString()
52 # process cell' styles
53 cell_styles = []
54 if hasattr(cell, 'CellBackColor'):
55 color = cell.getPropertyValue('CellBackColor')
56 if color >= 0:
57 cell_styles.append('background: #%06x' % color)
58 if hasattr(cell, 'HoriJustify'):
59 horiJustify = cell.getPropertyValue('HoriJustify')
60 if horiJustify == LEFT:
61 text_align = '' #text_align = 'left'
62 elif horiJustify == CENTER:
63 text_align = 'center'
64 elif horiJustify == RIGHT:
65 text_align = 'right'
66 else:
67 if cell_type == TEXT:
68 text_align = '' #text_align = 'left'
69 elif cell_type != EMPTY:
70 text_align = 'right'
71 if text_align and cell_string:
72 cell_styles.append('text-align: ' + text_align)
73 # add cell to row
74 if cell_styles:
75 cell_data.append('<style="%s;">' % '; '.join(cell_styles))
76 if cell_string:
77 cell_data.append(cell_string)
78 else:
79 cell_data.append(' ')
80 row_data.append(''.join(cell_data))
81 # display row's code
82 data.append('||' + '||'.join(row_data) + '||')
83
84 #
85 # création d'un document Writer pour écrire le code MoinMoin
86 #
87 # ouverture d'un document Writer caché
88 hidden = PropertyValue()
89 hidden.Name = "Hidden"
90 hidden.Value = True
91 doc = desktop.loadComponentFromURL(
92 "private:factory/swriter", "_blank", 0, (hidden, ) )
93 text = doc.Text
94 textcursor = text.createTextCursor()
95 text.insertString(textcursor, '\n'.join(data) + '\n', 0)
96 # on copie ça dans le presse papier
97 dispatcher = smgr.createInstanceWithContext(
98 "com.sun.star.frame.DispatchHelper", ctx)
99 frame = doc.getCurrentController().getFrame()
100 dispatcher.executeDispatch(frame, ".uno:SelectAll", "", 0, ())
101 dispatcher.executeDispatch(frame, ".uno:Copy", "", 0, ())
102 doc.close(True)
103
104##############################################################################
105
106def copier(event=False):
107 u"""Copier une feuille de calcul vers un wiki MoinMoin."""
108 ctx = uno.getComponentContext()
109 calc2moin(ctx)
110 return None
111
112g_exportedScripts = (copier, )
113
114##############################################################################
115
116class CopierJob(unohelper.Base, XJobExecutor):
117 def __init__(self, context):
118 self._context = context
119
120 def trigger(self, args):
121 calc2moin(self._context)
122
123g_ImplementationHelper = unohelper.ImplementationHelper()
124g_ImplementationHelper.addImplementation( \
125 CopierJob, "org.auf.openoffice.calc2moin.Copier", \
126 ("com.sun.star.task.Job",),)