Changeset 1980

Show
Ignore:
Timestamp:
02/15/07 08:53:56 (2 years ago)
Author:
ajo
Message:

ExcelReportPlugin:

* cleaned up a little bit
* added spanish support

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • excelreportplugin/0.10/excel_report_plugin.py

    r1978 r1980  
    66from trac.ticket.query import QueryModule 
    77from pyExcelerator import *  
     8from datetime import datetime  
     9import copy 
     10import types 
    811 
    912class XlsDoc(CompoundDoc.XlsDoc):  
     
    4447        def get_report_linkclass(self): 
    4548                return None #'xls' 
    46                          
    47                  
     49                                         
    4850        def render(self, req, cols, rows):  
    4951                 
     
    5860                                         req.hdf['project.name'])  
    5961                
    60                 sheetname = sheetname.replace('/','-')  
    61                 try: 
    62                     ws = wb.add_sheet(sheetname.decode('utf-8'))  
    63                 except:  
    64                     ws = wb.add_sheet(sheetname)  
     62                 
     63                ws = wb.add_sheet(self.convertSheetName(sheetname))  
     64                 
    6565                ws.panes_frozen = True  
    6666                ro = 1  
    6767                ws.horz_split_pos = ro  
    68           
    69                 import copy  
    70           
     68                 
     69                 
    7170                font0 = Font()  
    7271                font0.charset = font0.CHARSET_SYS_DEFAULT  
     
    8281                align2.wrap = align2.WRAP_AT_RIGHT  
    8382          
    84                 style0 = XFStyle()  
    85                 style0.font = font0  
    86                 style0.alignment = align1  
    87                 style0.num_format_str = 'general'  
    88                 style_colheader = copy.copy(style0)  
    89                 style_colheader.num_format_str = '@'  
    90                 style_colheader.font = font1  
    91                 style_num = copy.copy(style0)  
    92                 style_str = copy.copy(style0)  
    93                 style_str.num_format_str = '@'  
    94                 style_wrap_str = copy.copy(style0)  
    95                 style_wrap_str.alignment = align2  
    96                 style_wrap_str.font = font2  
    97                 style_date = copy.copy(style0)  
    98                 style_date.num_format_str = 'yyyy/mm/dd'  
     83                self.style0 = XFStyle()  
     84                self.style0.font = font0  
     85                self.style0.alignment = align1  
     86                self.style0.num_format_str = 'general'  
     87                self.style_colheader = copy.copy(self.style0)  
     88                self.style_colheader.num_format_str = '@'  
     89                self.style_colheader.font = font1  
     90                self.style_num = copy.copy(self.style0)  
     91                self.style_str = copy.copy(self.style0)  
     92                self.style_str.num_format_str = '@'  
     93                self.style_wrap_str = copy.copy(self.style0)  
     94                self.style_wrap_str.alignment = align2  
     95                self.style_wrap_str.font = font2  
     96                self.style_date = copy.copy(self.style0)  
     97                self.style_date.num_format_str = 'yyyy/mm/dd'  
    9998          
    10099                for col, cx in map(lambda x, y: [x, y], cols, range(len(cols))):  
    101100                    
    102101                    name = str(col).replace('_','')  
    103                     ws.write(ro-1, cx, name.decode('utf-8'), style_colheader)  
     102                    ws.write(ro-1, cx, name.decode('utf-8'), self.style_colheader)  
    104103                    
    105                     def conv(x): 
    106                         try:     
    107                             a = str(x).replace('\r','').rstrip('\r\n').decode('utf-8')  
    108                         except: 
    109                             return x.replace('\r','').rstrip('\r\n') 
    110                         return a 
     104                    conv = self.convertComments 
    111105         
    112                     style = style_str  
    113                     if name in ['time', 'date','changetime', 'created', 'modified']:  
     106                    style = self.style_str  
     107                     
     108                    if name in ['time', 'date','changetime', 'created', 'modified', 
     109                                'hora', 'fecha','cambio'   ,'creado'  ,'modificado']:  
    114110                        ws.col(cx).width = 0xb00  
    115                         from datetime import datetime  
    116                         conv = lambda x: datetime.fromtimestamp(float(x))  
    117                         style = style_date  
    118                     elif name in ['summary', 'description']:  
    119                         if name == 'description':  
    120                             ws.col(cx).width = 0x7000  
    121                         else:  
    122                             ws.col(cx).width = 0x1a00  
    123                         style = style_wrap_str  
     111                        conv = self.convertTimeStamp 
     112                        style = self.style_date  
     113                         
     114                    elif name in ['summary','resumen']: 
     115                        ws.col(cx).width = 0x1a00  
     116                        style = self.style_wrap_str  
     117                         
     118                    elif name in ['description','descripcion']: 
     119                        ws.col(cx).width = 0x7000  
     120                        style = self.style_wrap_str  
     121                         
    124122                    elif name in ['color', 'ticket', 'id']:  
    125123                        if name in ['color']:  
    126124                            ws.col(cx).hidden = 1  
    127                         conv = lambda x: int(x)  
    128                         style = style_num  
     125                        conv = self.convertInteger  
     126                        style = self.style_num  
    129127                    elif name in ['style']:  
    130128                        ws.col(cx).hidden = 1  
    131129                    elif name == "component": 
    132130                        ws.col(cx).width = 0x1a00  
     131                         
     132                         
    133133                    for value, rx in map(lambda x, y: [conv(x[cx]), ro + y], \ 
    134134                                         rows, range(len(rows))):  
    135                         ws.write(rx, cx, value, style)  
     135                        if type(value) is int: 
     136                                ws.write(rx, cx, value, self.style_num)  
     137                        elif type(value) is datetime: 
     138                                ws.write(rx, cx, value, self.style_date)  
     139                        elif type(value) is types.NoneType: 
     140                                ws.write(rx, cx, '-',self.style_str) 
     141                        else: 
     142                                ws.write(rx, cx, value, style)  
     143                         
     144                         
     145                                 
     146                    
    136147                req.write(wb.get())      
    137148 
     149        def convertComments(self, x): 
     150                try:     
     151                        if type(x) is types.NoneType: 
     152                                return x 
     153                        else: 
     154                                return str(x).replace('\r','').rstrip('\r\n').decode('utf-8')  
     155                except: 
     156                        return x.replace('\r','').rstrip('\r\n') 
    138157                 
     158         
     159        def convertTimeStamp(self,timestamp): 
     160                 
     161                try: 
     162                        return datetime.fromtimestamp(float(timestamp))  
     163                except: 
     164                        return timestamp 
     165                 
     166        def convertInteger(self,value): 
     167                try: 
     168                        return int(value) 
     169                except: 
     170                        return value 
     171                         
     172        def convertSheetName(self, sheetname): 
     173                 
     174                sheetname = sheetname.replace('/','-')  
     175                 
     176                try: 
     177                        return sheetname.decode('utf-8') 
     178                except: 
     179                        return sheetname 
     180                         
     181                   
    139182                         
    140183