Process/Php7.php
[Pman.Core] / Pman.Gnumeric.js
1 //<script type="text/javascript">
2 /**
3 * @class Pman Gnumeric.
4 *-> load up a remote xml file of a gnumeric document.
5
6 * -> convert into a usable data structure
7
8 * -> ?? apply templated values ??
9 * -> allow modification of fields
10
11 * -> render to screen.
12
13 * -> send for conversion to XLS (via ssconvert)
14
15 * Usage:
16 <pre><code>
17
18     new Pman.Gnumeric( {
19       url: rootURL + '/xxx/yyy/templates/reports/myreport.xml',
20       data: { ..... },
21       listeners : {
22           load : function()
23           {
24           
25                x.applyData({ ... }); // key value data looks for {value} in strings and replaces it..
26                
27                x.set('A3', 'test');
28                
29                mypanel.update(x.toHTML());
30                
31                x.download()       
32                
33            }
34       }
35     });
36     
37
38 </code></pre>
39
40 * @constructor
41 * @param {Object} cfg   Configuration object.
42 */
43  
44
45
46 Pman.Gnumeric = function (cfg)
47 {
48     cfg.data = cfg.data || {};
49     
50     
51     
52     
53     
54     
55     this.addEvents({
56         /**
57              * @event load
58              * Fires when source document has been loaded
59              * @param {Pman.Gnumerci} this
60              */
61             'load' : true
62     }); 
63     
64     Roo.util.Observable.call(this,cfg);
65     
66     this.defaultCell = {
67         c : 0,
68         r : 0,
69         valueType : 0,
70         valueFormat : '',
71         value : '',
72         colspan: 1,
73         rowspan: 1
74           
75     };
76      
77     this.load();
78     
79    
80     
81     
82 }
83 Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
84     
85     /**
86      * @cfg {String} url the source of the Gnumeric document.
87      */
88     url : false,
89       /**
90      * @cfg {Object} data overlay data for spreadsheet - from constructor.
91      */
92     data : false,
93      /**
94      * @cfg {String} downloadURL where GnumerictoExcel.php is...
95      */
96      
97     downloadURL : false,
98     
99     /**
100      * @type {XmlDocument} doc the gnumeric xml document
101      */
102     doc : false,
103     
104     /**
105      * @type {XmlNode} sheet the 'Sheet' element 
106      */
107     sheet : false,
108     
109     /**
110      * @type {XmlNode} sheet the 'Cells' element 
111      */    
112     cellholder : false,
113     /**
114      * @type {Object} grid the map[row][col] = cellData 
115      */
116     grid : false,
117     /**
118      * @type {Object} colInfo - list of column sizes
119      */
120     colInfo : false,
121     /**
122      * @type {Object} colInfoDom - column sizes dom element
123      */
124     colInfoDom : false,
125     /**
126      * @type {Object} rowInfo - list of row sizes
127      */
128     rowInfo : false,
129      /**
130      * @type {Object} rowInfoDom - dom elements with sizes
131      */
132     rowInfoDom : false,
133     /**
134      * @type {Number} cmax - maximum number of columns
135      */
136     cmax: false,
137     /**
138      * @type {Object} rmax - maximum number of rows
139      */
140     rmax : false,
141        /**
142      * @type {String} stylesheetID id of stylesheet created to render spreadsheat
143      */
144     stylesheetID : false,
145     /**
146      * @type {Number} rowOffset - used by table importer to enable multiple tables to be improted
147      */
148     
149     rowOffset : 0,
150     
151     /**
152      * load:
153      * run the connection, parse document and fire load event..
154      * can be run multiple times with new data..
155      * 
156     */
157     
158     load : function(url)
159     {
160         this.url = url || this.url;
161         if (!this.url) {
162             return;
163         }
164         // reset stufff..
165         this.doc = false;
166         this.sheet = false;
167         this.grid = false;
168         this.colInfo = false;
169         this.colInfoDom = false;
170         this.rowInfo = false;
171         this.rowInfoDom = false;
172         this.cmax = false;
173         this.rmax = false;
174         
175         if (this.stylesheetID) {
176             
177             Roo.util.CSS.removeStyleSheet(this.stylesheetID);
178             this.stylesheetID = false;
179             
180         }
181         
182         _t = this;
183         var c = new Roo.data.Connection();
184         c.request({
185             url: this.url,
186             method:  'GET',
187             success : function(resp, opts) {
188                 _t.response = resp;
189                 _t.doc = resp.responseXML;
190                 
191                 _t.parseDoc(0);
192                 
193                 
194                 _t.applyData();
195     
196                 _t.fireEvent('load', _t);
197             },
198             failure : function()
199             {
200                 Roo.MessageBox.alert("Error", "Failed to Load Template for Spreadsheet");
201             }
202         });
203         
204
205     },
206     
207     
208      
209     RCtoCell : function(r,c)
210     {
211         // we wil only support AA not AAA
212         var top = Math.floor(c/26);
213         var bot = c % 26;
214         var cc = top > 0 ? String.fromCharCode('A'.charCodeAt(0) + top) : '';
215         cc += String.fromCharCode('A'.charCodeAt(0)  + bot);
216         return cc+'' +r;
217         
218     },
219     
220     /**
221      * toRC:
222      * convert 'A1' style position to row/column reference
223      * 
224      * @arg {String} k cell name
225      * @return {Object}  as { r: {Number} , c: {Number}  }
226      */
227     
228     toRC : function(k)
229     {
230         var c = k.charCodeAt(0)-64;
231         var n = k.substring(1);
232         if (k.charCodeAt(1) > 64) {
233             c *=26;
234             c+=k.charCodeAt(1)-64;
235             n = k.substring(2);
236         }
237         return { c:c -1 ,r: (n*1)-1 }
238     },
239       /**
240      * rangeToRC:
241      * convert 'A1:B1' style position to array of row/column references
242      * 
243      * @arg {String} k cell range
244      * @return {Array}  as [ { r: {Number} , c: {Number}  }. { r: {Number} , c: {Number}  } ]
245      */
246     rangeToRC : function(s) {
247         var ar = s.split(':');
248         return [ this.toRC(ar[0]) , this.toRC(ar[1])]
249     },
250     
251     
252     
253    
254     
255     /**
256      * parseDoc:
257      * convert XML document into cells and other data..
258      * 
259      */
260     parseDoc : function(sheetnum) 
261     {
262         var _t = this;
263         this.grid = {}
264         this.rmax = 1;
265         this.cmax = 1;
266         
267         this.sheet = _t.doc.getElementsByTagNameNS('*','Sheet')[sheetnum];
268         
269         
270         this.cellholder = this.sheet.getElementsByTagNameNS('*','Cells')[0];
271         var cells = this.sheet.getElementsByTagNameNS('*','Cell');
272
273         
274         
275         Roo.each(cells, function(c) {
276            // Roo.log(c);
277             var row = c.getAttribute('Row') * 1;
278             var col = c.getAttribute('Col') * 1;
279             _t.cmax = Math.max(col+1, _t.cmax);
280             _t.rmax = Math.max(row+1, _t.rmax);
281             var vt = c.getAttribute('ValueType');
282             var vf = c.getAttribute('ValueFormat');
283             var val = c.textContent;
284             
285             if (typeof(_t.grid[row]) == 'undefined') {
286                 _t.grid[row] ={};
287             }
288             _t.grid[row][col] = Roo.applyIf({
289                 valueType : vt,
290                 valueFormat : vf,
291                 value : val,
292                 dom: c,
293                 r: row,
294                 c: col
295             }, _t.defaultCell);
296         });
297        
298         for (var r = 0; r < this.rmax;r++) {
299             if (typeof(this.grid[r]) == 'undefined') {
300               this.grid[r] ={};
301             }
302             for (var c = 0; c < this.cmax;c++) {
303                 if (typeof(this.grid[r][c]) == 'undefined') {
304                     continue;
305                 }
306                 //this.print( "[" + r + "]["+c+"]=" + grid[r][c].value +'<br/>');
307             }
308         }
309         
310         var merge = this.sheet.getElementsByTagNameNS('*','Merge');
311
312         Roo.each(merge, function(c) {
313             var rc = _t.rangeToRC(c.textContent);
314             //Roo.log(JSON.stringify(rc));
315             if (typeof(_t.grid[rc[0].r][rc[0].c]) == 'undefined') {
316                 //Roo.log(["creating empty cell for  ",rc[0].r,  rc[0].c ]);
317                  _t.createCell(rc[0].r,  rc[0].c );
318                 //_t.grid[rc[0].r][rc[0].c] =  //Roo.applyIf({ r : rc[0].r, c : rc[0].c }, _t.defaultCell);
319             }
320                 
321             _t.grid[rc[0].r][rc[0].c].colspan = (rc[1].c - rc[0].c) + 1;
322             _t.grid[rc[0].r][rc[0].c].rowspan = (rc[1].r - rc[0].r) + 1;
323             for(var r = (rc[0].r); r < (rc[1].r+1); r++) {
324                for(var cc = rc[0].c; cc < (rc[1].c+1); cc++) {
325                     //Roo.log('adding alias : ' + r+','+c);
326                    _t.grid[r][cc] = _t.grid[rc[0].r][rc[0].c];
327                }
328            }
329             
330             
331         });
332         // read colinfo..
333         var ci = this.sheet.getElementsByTagNameNS('*','ColInfo');
334         this.colInfo = {};
335         this.colInfoDom = {};
336         
337         Roo.each(ci, function(c) {
338             var count = c.getAttribute('Count') || 1;
339             var s =  c.getAttribute('No')*1;
340             for(var i =0; i < count; i++) {
341                 _t.colInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
342                 _t.colInfoDom[s+i] = c;
343             }
344         });
345         
346         
347         ci = this.sheet.getElementsByTagNameNS('*','RowInfo');
348         
349         this.rowInfo = {};
350         this.rowInfoDom = {};
351         Roo.each(ci, function(c) {
352             var count = c.getAttribute('Count') || 1;
353             var s =  c.getAttribute('No')*1;
354             for(var i =0; i < count; i++) {
355                 _t.rowInfoDom[s+i] = c;
356                 _t.rowInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
357             }
358         });
359     
360         _t.parseStyles();
361         _t.overlayStyles();
362                 
363         
364      
365         
366     },
367      /**
368      * overlayStyles:
369      * put the style info onto the cell data.
370      * 
371      */
372     overlayStyles : function ()
373     {
374            // apply styles.
375         var _t = this;
376         Roo.each(this.styles, function(s) {
377        
378             for (var r = s.r; r < s.r1;r++) {
379                 if (typeof(_t.grid[r]) == 'undefined') {
380                    continue;
381                 }
382                 for (var c = s.c; c < s.c1;c++) {
383                     if (c > _t.cmax) {
384                         continue;
385                     }
386     
387                     if (typeof(_t.grid[r][c]) == 'undefined') {
388                         _t.createCell(r,c);
389                         //_t.grid[r][c] = Roo.applyIf({ r: r , c : c }, _t.defaultCell);
390                     }
391                     var g=_t.grid[r][c];
392                     if (typeof(g.cls) =='undefined') {
393                         g.cls = [];
394                         g.styles = [];
395                     }
396                     if (g.cls.indexOf(s.name)  > -1) {
397                        continue;
398                     }
399                     g.cls.push(s.name);
400                     g.styles.push(s.dom);
401                     
402                 }
403             }
404         });
405     },
406      /**
407      * parseStyles: 
408      *  read the style information
409      * generates a stylesheet for the current file
410      * this should be disposed of really.....
411      * 
412      */
413     parseStyles : function() {
414                 
415         var srs = this.sheet.getElementsByTagNameNS('*','StyleRegion');
416         var _t  = this;
417         var ent = {};
418         
419         var map =  {
420             HAlign : function(ent,v) { 
421                 ent['text-align'] = { '1' : 'left', '8': 'center', '4' : 'right'}[v] || 'left';
422             },
423             VAlign : function(ent,v) { 
424                 ent['vertical-align'] = { '1' : 'top', '4': 'middle', '8' : 'bottom'}[v]  || 'top'
425             },
426             Fore : function(ent,v) { 
427                 var col=[];
428                 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
429                 ent['color'] = 'rgb(' + col.join(',') + ')';
430             },
431             Back : function(ent,v) { 
432                 var col=[];
433                 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
434                 ent['background-color'] = 'rgb(' + col.join(',') + ')';
435             },
436             FontUnit : function(ent,v) { 
437                 ent['font-size'] = v + 'px';
438             },
439             FontBold : function(ent,v) { 
440                 if (v*1 < 1) { return; }
441                 ent['font-weight'] = 'bold';
442             },
443             FontItalic : function(ent,v) { 
444                 if (v*0 < 1) { return; }
445                 //ent['font-weight'] = 'bold';
446             },
447             FontName : function(ent,v) { 
448                 ent['font-family'] = v;
449             },
450             BorderStyle : function(ent,v) { 
451                 var vv  = v.split('-');
452                 ent['border-'+vv[0]+'-style'] = 'solid';
453                 ent['border-'+vv[0]+'-width'] = vv[1]+'px';
454             },
455             BorderColor : function(ent,v) { 
456                 var vv  = v.split('-');
457                 var col=[];
458                 Roo.each(vv[1].split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
459                 ent['border-'+vv[0]+'-color'] = 'rgb(' + col.join(',') + ')';
460             }
461         };
462         function add(e, k, v) {
463             //Roo.log(k,v);
464             e.gstyle[k] = v;
465             if (typeof(map[k]) == 'undefined') {
466                 return;
467             }
468             map[k](e.style,v);    
469         }
470         var css = {};
471         var styles = [];
472         var sid= Roo.id();
473         
474         
475         Roo.each(srs, function(sr,n)
476         {
477             ent = {
478                 c : sr.getAttribute('startCol') *1,
479                 r : sr.getAttribute('startRow')*1,
480                 c1 : (sr.getAttribute('endCol')*1) +1,
481                 r1 : (sr.getAttribute('endRow')*1) +1,
482                 style : {},  // key val of style for HTML..
483                 gstyle : {}, // key val of attributes used..
484                 name : sid +'-gstyle-' + n,
485                 dom : sr
486                 
487             };
488     
489             Roo.each(sr.getElementsByTagNameNS('*','Style')[0].attributes, function(e) { 
490                 add(ent, e.name, e.value);
491             });
492             if (sr.getElementsByTagNameNS('*','Font').length) {
493                 Roo.each(sr.getElementsByTagNameNS('*','Font')[0].attributes, function(e) { 
494                      add(ent, 'Font'+e.name, e.value);
495     
496                 });
497                 add(ent, 'FontName', sr.getElementsByTagNameNS('*','Font')[0].textContent);
498     
499             }
500             if (sr.getElementsByTagNameNS('*','StyleBorder').length) {
501                 Roo.each(sr.getElementsByTagNameNS('*','StyleBorder')[0].childNodes, function(e) {
502                     if (!e.tagName) {
503                         return;
504                     }
505                     Roo.each(e.attributes, function(ea) { 
506                         add(ent, 'Border'+ea.name, e.tagName.split(':')[1].toLowerCase() + '-' + ea.value);
507                     });
508                 })
509                     
510             }
511             styles.push(ent);
512             css['.'+ent.name] = ent.style;
513         });
514         
515         this.styles = styles;
516         
517         this.stylesheetID = sid;
518         Roo.util.CSS.createStyleSheet(css, sid);
519     },
520
521     
522     
523     
524     /* ---------------------------------------  AFTER LOAD METHODS... ----------------------- */
525     /**
526      * set: 
527      * Set the value of a cell..
528      * @param {String} cell name of cell, eg. C10 or { c: 1, r :1 }
529          
530      * @param {Value} value to put in cell..
531      * @param {ValueType} type of value
532      * @param {ValueFormat} value format of cell
533      * 
534      * Cells should exist at present, we do not make them up...
535      */
536      
537     
538     set : function(cell, v, vt, vf) {
539         
540         var cs= typeof(cell) == 'string' ? this.toRC(cell) : cell;
541         
542         
543         Roo.log( cs.r+ ',' + cs.c + ' = '+ v);
544         // need to generate clell if it doe
545         if (typeof(this.grid[cs.r]) == 'undefined') {
546             Roo.log('no row:' + cell);
547             this.grid[cs.r] = []; // create a row..
548             //return;
549         }
550         if (typeof(this.grid[cs.r][cs.c]) == 'undefined') {
551             Roo.log('cell not defined:' + cell);
552             this.createCell(cs.r,cs.c);
553         }
554         // cell might not be rendered yet... so if we try and create a cell, it overrides the default formating..
555         
556         if (typeof(this.grid[cs.r][cs.c].dom) == 'undefined') {
557             Roo.log('no default content for cell:' + cell);
558             Roo.log(this.grid[cs.r][cs.c]);
559             //this.createCell(cs.r,cs.c);
560             //return;
561         }
562         this.grid[cs.r][cs.c].value=  v;
563         if (this.grid[cs.r][cs.c].dom) {
564             this.grid[cs.r][cs.c].dom.textContent=  v;
565         }
566         
567         
568         if (typeof(vt) != 'undefined') {
569             this.grid[cs.r][cs.c].valueType = vt;
570             this.grid[cs.r][cs.c].dom.setAttribute('ValueType', vt);
571             if (vt === '' || vt === false) { // value type is empty for formula's
572                 this.grid[cs.r][cs.c].dom.removeAttribute('ValueType');
573             }
574         }
575         if (typeof(vf) != 'undefined' && vf !== false) {
576             this.grid[cs.r][cs.c].valueFormat = vf;
577             this.grid[cs.r][cs.c].dom.setAttribute('ValueFormat', vf);
578             if (vf === '' || vf === false) { // value type is empty for formula's
579                 this.grid[cs.r][cs.c].dom.removeAttribute('ValueFormat');
580             }
581         }
582         
583     },
584     
585     // private
586     copyRow : function(src, dest) {
587         if (dest == src) {
588             return;
589         }
590        // Roo.log('create Row' + dest);
591         if (typeof(this.grid[dest]) == 'undefined') {
592             this.grid[dest] = {}
593         }
594         
595            
596         for (var c = 0; c < this.cmax; c++) {
597
598             this.copyCell({ r: src, c: c } , { r: dest, c: c});
599             
600         }
601         this.rmax = Math.max(this.rmax, dest +1);
602         
603     },
604     
605     // private
606     
607     createCell: function(r,c)
608     {
609         //<gnm:Cell Row="6" Col="5" ValueType="60">Updated</gnm:Cell>    
610         var nc = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Cell');
611         this.cellholder.appendChild(nc);
612         var lb = this.doc.createTextNode("\n");// add a line break..
613         this.cellholder.appendChild(lb);
614         
615         nc.setAttribute('Row', new String(r));
616         nc.setAttribute('Col', new String(c));
617         nc.setAttribute('ValueType', '60');
618         nc.textContent = '';
619         
620         this.grid[r][c] = Roo.applyIf({
621             valueType : '60',
622             valueFormat : '',
623             value : '',
624             dom: nc,
625             r: r,
626             c: c
627             }, _t.defaultCell);
628         
629         return nc;
630
631     },
632     
633     // private
634     copyCell : function(src, dest)
635     {
636         var old = this.grid[src.r][src.c];
637         // is it an alias...
638         if ((old.c != src.c)  || (old.r != src.r)) {
639             // only really works on horizonatal merges..
640             
641             this.grid[dest.r][dest.c] = this.grid[desc.r][old.c]; // let's hope it exists.
642             return;
643         }
644         
645         
646         var nc = Roo.apply({}, this.grid[src.r][src.c]);
647         
648         nc.value = '';
649         if (typeof(old.dom) == 'undefined') {
650             Roo.log("No cell to copy for " + Roo.encode(src));
651             return;
652         }
653         this.grid[dest.r][dest.c] = nc;
654         nc.dom = old.dom.cloneNode(true);
655         nc.dom.setAttribute('Row', dest.r);
656         nc.dom.setAttribute('Cell', dest.c);
657         nc.dom.textContent = '';
658         old.dom.parentNode.appendChild(nc.dom);
659         if (!old.styles || !old.styles.length) {
660             return;
661         }
662         //Roo.log("DEST");
663         //Roo.log(dest);
664         //Roo.log("STYLES");
665         //  .styles...
666         Roo.each(old.styles, function(s) {
667             // try and extend existing styles..
668             var er = s.getAttribute('endRow') * 1;
669             var ec = s.getAttribute('endCol') * 1;
670             //Roo.log(s);
671             if (dest.r == er) {
672                 s.setAttribute('endRow', dest.r + 1);
673             }
674             if (dest.c == ec) {
675                 s.setAttribute('endCol', dest.c + 1);
676             }
677             /*var ns = s.cloneNode(true);
678             s.parentNode.appendChild(ns);
679             ns.setAttribute('startCol', dest.c);
680             ns.setAttribute('startRow', dest.r);
681             ns.setAttribute('endCol', dest.c + 1);
682             ns.setAttribute('endRow', dest.r +1);
683             */
684         });
685         
686     },
687     
688     
689     /**
690      * applyData: 
691      * Set the value of a cell..
692      * @param {String} cell name of cell, eg. C10
693      * @param {Value} value to put in cell..
694      * 
695      * Cells should exist at present, we do not make them up...
696      */
697      
698     applyData : function(data)
699     {
700         
701         data = data || this.data;
702         for (var r = 0; r < this.rmax;r++) {
703             if (typeof(this.grid[r]) == 'undefined') {
704                 continue;
705             }
706             for (var c = 0; c < this.cmax;c++) {  
707                 if (typeof(this.grid[r][c]) == 'undefined') {
708                     continue;
709                 }
710                 if (!this.grid[r][c].value.length 
711                         || !this.grid[r][c].value.match(/\{/)) {
712                     continue;
713                 }
714                 
715                 var x = new Roo.Template({ html: this.grid[r][c].value });
716                 try {
717                     var res = x.applyTemplate(data);
718                     //Roo.log("set " + r  + "," + c + ":"+res)
719                     this.set({ r: r, c: c}, x.applyTemplate(data));
720                 } catch (e) {
721                  //   Roo.log(e.toString());
722                   //  Roo.log(e);
723                     // continue?
724                 }
725                 
726             }
727         }
728             
729     },
730     
731     readTableData : function(table)
732     {
733         // read the first row.
734         var tds = Roo.get(table).select('tr').item(0).select('td');
735         var maxnc = 0;
736         
737         Roo.get(table).select('tr').each(function(trs) {
738             var nc = 0;
739            
740             trs.select('td').each(function(td) {
741                 var cs = td.dom.getAttribute('colspan');
742                 cs = cs ? cs * 1 : 1;
743                 nc += cs;
744             });
745             maxnc = Math.max(nc, maxnc);
746         });
747         
748         var tr = document.createElement('tr');
749         table.appendChild(tr);
750         var ar = {};
751         for (i =0; i < maxnc; i++) {
752             ar[i] = document.createElement('td');
753             tr.appendChild(ar[i]);
754         }
755         // find the left.
756         var ret = { cols : maxnc, pos : {} };
757         for (i =0; i < maxnc; i++) {
758             ret.pos[ Roo.get(ar[i]).getLeft()] =i;
759         }
760         ret.near = function(p) {
761             // which one is nearest..
762             
763             if (this.pos[p]) {
764                 return this.pos[p];
765             }
766             var prox = 100000;
767             var match = 0;
768             for(var i in this.pos) {
769                 var dis = Math.abs(p-i);
770                 if (dis < prox) {
771                     prox = dis;
772                     match = this.pos[i];
773                 }
774             }
775             return match;
776             
777         }
778         table.removeChild(tr);
779         return ret;
780     },
781     
782      
783    
784      
785     /**
786      * importTable: 
787      * Import a table and put it into the spreadsheet
788      * @param {HTMLTable} datagrid dom element of html table.
789      * @param {Number} xoff X offset to start rendering to
790      * @param {Number} yoff Y offset to start rendering to
791      **/
792      
793  
794     importTable : function (datagrid, xoff,yoff)
795     {
796         if (!datagrid) {
797             Roo.log("Error table not found!?");
798             return;
799         }
800         xoff = xoff || 0;
801         yoff = yoff || 0;
802         
803         
804         var table_data = this.readTableData(datagrid);
805         
806         // oroginally this cleaned line breaks, but we acutally need them..
807         var cleanHTML = function (str) {
808             
809             var ret = str;
810             ret = ret.replace(/&nbsp;/g,' ');
811            // ret = ret.replace(/\n/g,'.');
812           //  ret = ret.replace(/\r/g,'.');
813             var i;
814              
815             return ret;
816         };
817
818         
819         // <cell col="A" row="1">Test< / cell>
820         // <cell col="B" row="2" type="Number" format="test1">30< / cell>
821         var rowOffsets = {};
822         var rows = datagrid.getElementsByTagName('tr');
823         //alert(rows.length);
824         
825         
826         for(var row=0;row<rows.length;row++) {
827             
828             // let's see what affect this has..
829             // it might mess things up..
830             
831             if (rows[row].getAttribute('xls:height')) {
832                 this.setRowHeight(row + yoff +1, 1* rows[row].getAttribute('xls:height'));
833             } else {
834                 this.setRowHeight( row + yoff +1, Roo.get(rows[row]).getHeight());
835             }
836             
837          
838             var cols = rows[row].getElementsByTagName('td');
839             
840             
841             for(var col=0;col < cols.length; col++) {
842                 
843                 
844                
845                 
846                 var colspan = cols[col].getAttribute('colspan');
847                 colspan  = colspan ? colspan *1 : 1;
848                 
849                 var rowspan = cols[col].getAttribute('rowspan');
850                 rowspan = rowspan ? rowspan * 1 : 1;
851                 
852                 var realcol = table_data.near( Roo.get(cols[col]).getLeft() );
853                 
854                 
855                 
856                 if (colspan > 1 || rowspan > 1) {
857                     
858                     // getting thisese right is tricky..
859                     this.mergeRegion(
860                         realcol + xoff,
861                         row + yoff +1,
862                         realcol+ xoff + (colspan -1),
863                         row + yoff + rowspan 
864                     );
865                     
866                 }
867                 
868                 // skip blank cells
869                 // set the style first..
870                 this.parseHtmlStyle( cols[col], row + yoff, realcol + xoff   , colspan, rowspan);
871                 
872                 if (!cols[col].childNodes.length) {
873                      
874                     continue;
875                 }
876                 
877                 
878                 
879                 
880                 var vt = '60';
881                 var vf = false;
882                 var xlstype = cols[col].getAttribute('xls:type');
883                 switch(xlstype) {
884                     case 'int':
885                         vt = 30; // int!!!!
886                     
887                         break;
888                         
889                     case 'float':
890                         vt = 40; // float!!!!
891                         if (cols[col].getAttribute('xls:floatformat')) {
892                             vf = cols[col].getAttribute('xls:floatformat');
893                         }
894                         break;
895                         
896                     case 'date':
897                         vt = 30;
898                         //ValueFormat="d/m/yyyy" 38635  
899                         var vf = 'd/m/yyy';
900                         if (cols[col].getAttribute('xls:dateformat')) {
901                             vf= cols[col].getAttribute('xls:dateformat');
902                         }
903                         
904                        
905                         
906                         break;
907                     
908                     default:
909                        
910                         break;
911                 }
912                
913                 if (!cols[col].childNodes[0].nodeValue) {
914                    
915                     continue;
916                 }
917                 if (!cols[col].childNodes[0].nodeValue.replace(/^\s*|\s*$/g,"").length) {
918                   
919                     continue;
920                 }
921                 // strip me.!
922                 var cell_value_text = cleanHTML(cols[col].childNodes[0].nodeValue);
923        
924                 if (cols[col].getAttribute('xls:percent')) {
925                     cell_value_text = '' + ((cell_value_text * 1) / 100);
926                 }
927
928                 if (cell_value_text.length && (vt == 30) && xlstype == 'date') {
929                     var bits = cell_value_text.split(/-/);
930                     var cur = new Date(bits[0],bits[1]-1,bits[2]);
931                     cell_value_text = '' + Math.round((cur.getTime() - Date.UTC(1899,11,30)) / (24 * 60 * 60 * 1000));
932                 }
933
934                 
935                 
936                 if (cols[col].getAttribute('xls:formula')) {
937                     var s = cols[col].getAttribute('xls:formula');
938                     vt = '';
939                     cell_value_text = s.replace(/#row#/g,(row + yoff + 1));
940                 }
941                 this.set({ r: row + yoff, c : realcol + xoff }, cell_value_text, vt, vf);
942                  
943                   
944                 
945                 
946                 
947             }
948         }
949         this.rowOffset += rows.length;
950         
951     },
952     
953     
954     
955     parseHtmlStyle : function(dom, row, col, colspan, rowspan) {
956         
957         function toCol (rgb) {
958             
959             var ar = rgb.replace(/rgb[a]?\(/, '').replace(/\)/, '').replace(/ /, '').split(',');
960             var rcs = [];
961             ar = ar.slice(0,3);
962             Roo.each(ar, function(c) { 
963                 rcs.push((c*c).toString(16)) ;   
964             });
965             return rcs.join(':');
966             
967         }
968         
969         var el = Roo.get(dom);
970         var map =  {
971             'text-align'  : function(ent,v) { 
972                 ent['HAlign'] = { 'left' : '1', 'center' : '8' ,  'right' : '4' }[v] || '1';
973             },
974             'vertical-align': function(ent,v) { 
975                 ent['VAlign'] = { 'top' : '1', 'middel' : '8' ,  'bottom' : '4' }[v] || '1';
976             },
977             
978             'color': function(ent,v) { 
979                 ent['Fore'] = toCol(v);
980                 // this is a bit dumb.. we assume that if it's not black text, then it's shaded..
981                 if (ent['Fore'] != '0:0:0') {
982                     ent['Shade'] = 1;
983                 }
984                 
985             },
986             'background-color' : function(ent,v) { 
987                 ent['Back'] = toCol(v);
988                  
989             }
990             
991         };
992        
993         var ent = {
994                 HAlign:"1",
995                 VAlign:"2",
996                 WrapText:"0",
997                 ShrinkToFit:"0",
998                 Rotation:"0",
999                 Shade:"0",
1000                 Indent:"0",
1001                 Locked:"0",
1002                 Hidden:"0",
1003                 Fore:"0:0:0",
1004                 Back:"FFFF:FFFF:FFFF",
1005                 PatternColor:"0:0:0",
1006                 Format:"General"
1007         };
1008            
1009         for(var k in map) {
1010             var val = el.getStyle(k);
1011             if (!val || !val.length) {
1012                continue;
1013             }
1014             map[k](ent,val);
1015         }
1016         // special flags..
1017         if (el.dom.getAttribute('xls:wraptext')) {
1018             ent.WrapText = 1;
1019         }
1020         if (el.dom.getAttribute('xls:valign')) {
1021             ent.VAlign= 1;
1022         }
1023         if (el.dom.getAttribute('xls:halign')) {
1024             ent.HAlign= 1;
1025         }
1026         // fonts..
1027         var fmap = {
1028             
1029            
1030             'font-size' : function(ent,v) { 
1031                 ent['Unit'] = v.replace(/px/, '');
1032             },
1033             'font-weight' : function(ent,v) { 
1034                 if (v != 'bold') {
1035                    return;
1036                 }
1037                 ent['Bold'] = 1;
1038             },
1039             'font-style' : function(ent,v) { 
1040                 if (v != 'italic') {
1041                     return;
1042                 }
1043                 ent['Italic'] = 1;
1044             } 
1045         };
1046        
1047         var fent = {
1048             Unit:"10",
1049             Bold:"0",
1050             Italic:"0",
1051             Underline:"0",
1052             StrikeThrough:"0"
1053         };
1054         
1055         for(var k in fmap) {
1056             var val = el.getStyle(k);
1057             if (!val || !val.length) {
1058                continue;
1059             }
1060             fmap[k](fent,val);
1061         }
1062         var font = el.getStyle('font-family') || 'Sans';
1063         if (font.split(',').length > 1) {
1064             font = font.split(',')[1].replace(/\s+/, '');
1065         }
1066         
1067         
1068         /// -- now create elements..
1069         
1070         var objs = this.sheet.getElementsByTagNameNS('*','Styles')[0];
1071         
1072         //<gnm:StyleRegion startCol="0" startRow="0" endCol="255" endRow="65535"
1073         var sr = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleRegion');
1074         objs.appendChild(sr);
1075         objs.appendChild(this.doc.createTextNode("\n"));// add a line break..
1076
1077         sr.setAttribute('startCol', col);
1078         sr.setAttribute('endCol', col+ colspan-1);
1079         sr.setAttribute('startRow', row);
1080         sr.setAttribute('endRow', row + rowspan -1);
1081         
1082         
1083         var st = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Style');
1084         sr.appendChild(st);
1085         // do we need some defaults..
1086         for(var k in ent) {
1087             //Roo.log(k);
1088             st.setAttribute(k, ent[k]);
1089         }
1090         
1091         var fo = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Font');
1092         st.appendChild(fo);
1093         // do we need some defaults..
1094         for(var k in fent) {
1095             fo.setAttribute(k, fent[k]);
1096         }
1097         fo.textContent  = font;
1098         
1099         var sb = false;
1100         // borders..
1101         Roo.each(['top','left','bottom','right'], function(p) {
1102             var w = el.getStyle('border-' + p + '-width').replace(/px/, '');
1103             if (!w || !w.length || (w*1) < 1) {
1104                 return;
1105             }
1106             if (!sb) {
1107                 sb= this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleBorder');
1108             }
1109             var be = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:' + p[0].toUpperCase() + p.substring(1));
1110             be.setAttribute('Style', '1');
1111             be.setAttribute('Color', '0:0:0'); // fixme..
1112             sb.appendChild(be);
1113             
1114         }, this);
1115         // start adding them all together..
1116         
1117         if (sb) {
1118             st.appendChild(sb);
1119         }
1120         
1121         
1122         
1123         
1124     },
1125     
1126     
1127     
1128     /**
1129      * writeImage:
1130      * write an image (needs base64 data to write it)
1131      * 
1132      * 
1133      * @param {Number} row  row to put it in (rows start at 0)
1134      * @param {Number} col  column to put it in
1135      * @param {Number} data  the base64 description of the images
1136      * @param {Number} width image width
1137      * @param {Number} width image height
1138      * 
1139      */
1140     
1141     
1142     writeImage : function (row, col, data, width, height, type) 
1143     {
1144         
1145         if (!data) {
1146             throw "write Image called with missing data";
1147         }
1148         // our default height width is 50/50 ?!
1149         //console.log('w='+width+',height='+height);
1150                 //        <gmr:Objects>
1151         row*=1;
1152         col*=1;
1153         height*=1;
1154         width*=1;
1155         var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1156         var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1157         
1158         //<gmr:SheetObjectImage 
1159         //      ObjectBound="A3:J8" 
1160         //      ObjectOffset="0.375 0.882 0.391 0.294" 
1161         //      ObjectAnchorType="16 16 16 16" 
1162         //      Direction="17" 
1163         //      crop-top="0.000000" 
1164         //      crop-bottom="0.000000" 
1165         //      crop-left="0.000000" 
1166         //      crop-right="0.000000">
1167                 
1168                 
1169         //alert(gnumeric_colRowToName(row,col));
1170                
1171         // this is where we really have fun!!!... 
1172         // since our design currently assumes the height is enough to fit
1173         // stuff in, we only really need to work out how wide it has to be..
1174         
1175         // note we should probably use centralized calcs if it fits in the first cell!
1176         
1177         // step 1 - work out how many columns it will span..
1178         // lets hope the spreadsheet is big enought..
1179         var colwidth = 0;
1180         var endcol=col;
1181         for ( endcol=col;endcol <100; endcol++) {
1182             if (!this.colInfo[endcol]) {
1183                 this.colInfo[endcol] = 100; // eak fudge
1184             }
1185             colwidth += this.colInfo[endcol];
1186             if (colwidth > width) {
1187                 break;
1188             }
1189         }
1190         
1191         soi.setAttribute('ObjectBound',
1192             //gnumeric_colRowToName(row,col) + ':' + gnumeric_colRowToName(row+1,col+1));
1193             this.RCtoCell(row,col) + ':' + this.RCtoCell(row,endcol));
1194      
1195         var ww = 0.01; // offset a bit...
1196         var hh = 0.01; //
1197         
1198         var ww2 = 1 - ((colwidth - width) / this.colInfo[endcol]);
1199         var hh2 = 0.99;
1200         
1201         var offset_str = ww + ' '  + hh + ' ' + ww2 + ' '+hh2;
1202         //console.log(offset_str );
1203         //alert(offset_str);
1204         soi.setAttribute('ObjectOffset', offset_str);
1205         soi.setAttribute('ObjectAnchorType','16 16 16 16');
1206         soi.setAttribute('Direction','17');
1207         soi.setAttribute('crop-top','0.000000');
1208         soi.setAttribute('crop-bottom','0.000000');
1209         soi.setAttribute('crop-left','0.000000');
1210         soi.setAttribute('crop-right','0.000000');
1211                 // <Content image-type="jpeg" size-bytes="3900">......  < / Content>
1212                 
1213         var name = 'Image' + Math.random().toString(36).substring(2);
1214         var content = this.doc.createElement('Content');
1215         content.setAttribute('image-type', type ? type : 'jpeg');
1216         content.setAttribute('name', name);
1217         soi.appendChild(content);
1218         objs.appendChild(soi);
1219         
1220         var godoc = this.doc.getElementsByTagNameNS('*','GODoc')[0];
1221         
1222         var goimage = this.doc.createElement('GOImage');
1223         goimage.setAttribute('image-type', type ? type : 'jpeg');
1224         goimage.setAttribute('name', name);
1225         goimage.setAttribute('type', 'GOPixbuf');
1226         goimage.setAttribute('width', width);
1227         goimage.setAttribute('height', height);
1228         goimage.textContent = data;
1229         
1230         godoc.appendChild(goimage);
1231         
1232         return true;
1233                 //< /gnm:SheetObjectImage>
1234                 // < /gnm:Objects>
1235
1236     },
1237     
1238     writeFixedImage : function (startCol, startRow, endCol, endRow, type, data, width, height) 
1239     {
1240         if (!data) {
1241             throw "write Image called with missing data";
1242         }
1243         
1244         startCol = startCol * 1;
1245         startRow = startRow * 1;
1246         endCol = endCol * 1;
1247         endRow = endRow * 1;
1248         width = width * 1;
1249         height = height * 1;
1250         
1251         var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1252         var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1253         
1254         soi.setAttribute('ObjectBound',this.RCtoCell(startRow, startCol) + ':' + this.RCtoCell(endRow, endCol));
1255         
1256         soi.setAttribute('ObjectOffset', '0 0 0 0');
1257         soi.setAttribute('ObjectAnchorType','16 16 16 16');
1258         soi.setAttribute('Direction','17');
1259         soi.setAttribute('crop-top','0.000000');
1260         soi.setAttribute('crop-bottom','0.000000');
1261         soi.setAttribute('crop-left','0.000000');
1262         soi.setAttribute('crop-right','0.000000');
1263         
1264         var name = 'Image' + Math.random().toString(36).substring(2);
1265         var content = this.doc.createElement('Content');
1266         content.setAttribute('image-type', type ? type : 'jpeg');
1267         content.setAttribute('name', name);
1268         soi.appendChild(content);
1269         objs.appendChild(soi);
1270         
1271         Roo.log(name);
1272         
1273         var godoc = this.doc.getElementsByTagNameNS('*','GODoc')[0];
1274         
1275         var goimage = this.doc.createElement('GOImage');
1276         goimage.setAttribute('image-type', type ? type : 'jpeg');
1277         goimage.setAttribute('name', name);
1278         goimage.setAttribute('type', 'GOPixbuf');
1279         goimage.setAttribute('width', width);
1280         goimage.setAttribute('height', height);
1281         goimage.textContent = data;
1282         
1283         godoc.appendChild(goimage);
1284         
1285         return true;
1286     },
1287  
1288     /**
1289      * mergeRegion:
1290      * Merge cells in the spreadsheet. (does not check if existing merges exist..)
1291      * 
1292      * @param {Number} col1  first column 
1293      * @param {Number} row1  first row
1294      * @param {Number} col2  to column 
1295      * @param {Number} row2  to row
1296      * 
1297      */
1298     mergeRegion : function (col1,row1,col2,row2)
1299     {
1300         var cell = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Merge');
1301         //if (col1 > 50|| col2 > 50) { // do not merge cols off to right?
1302        //     return;
1303         //}
1304         
1305         cell.textContent = this.RCtoCell(row1,col1) + ':' + this.RCtoCell(row2,col2);
1306         
1307         //var merges = this.gnumeric.getElementsByTagNameNS('*','MergedRegions');
1308         var merges = this.sheet.getElementsByTagNameNS('*','MergedRegions');
1309         if (!merges || !merges.length) {
1310             merges = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:MergedRegions');
1311             var sl = this.sheet.getElementsByTagNameNS('*','SheetLayout')[0];
1312             this.sheet.insertBefore(merges,sl);
1313         } else {
1314             merges = merges[0];
1315         }
1316         merges.appendChild(cell);
1317     
1318     },
1319     /**
1320      * setRowHeight:
1321      * Sets the height of a row.
1322      * 
1323      * @param {Number} r  the row to set the height of. (rows start at 0)
1324      * @param {Number} height (in pixels)
1325      */
1326     setRowHeight : function (r,height)
1327     {
1328         
1329         //<gmr:Rows DefaultSizePts="12.75">
1330         //   <gmr:RowInfo No="2" Unit="38.25" MarginA="0" MarginB="0" HardSize="1"/>
1331     //  < /gmr:Rows>
1332         
1333         // this doesnt handle row ranges very well.. - with 'count in them..'
1334         
1335         if (this.rowInfoDom[r]) {
1336             this.rowInfoDom[r].setAttribute('Unit', height);
1337             return;
1338         }
1339     
1340         var rows = this.sheet.getElementsByTagNameNS('*','Rows')[0]; // assume this exists..
1341         var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:RowInfo');
1342         // assume we have no rows..
1343         ri.setAttribute('No', r-1);
1344         ri.setAttribute('Unit', height);
1345         ri.setAttribute('MarginA', 0);
1346         ri.setAttribute('MarginB', 0);
1347         ri.setAttribute('HardSize', 1);
1348         rows.appendChild(ri);
1349         this.rowInfoDom[r] = ri;
1350     },
1351      
1352     /**
1353      * setSheetName: 
1354      * Set the sheet name.
1355      * @param {String} title for sheet
1356      **/
1357     setSheetName : function(name,sheet)
1358     {
1359         sheet = sheet || 0;
1360         /*
1361         <gnm:SheetNameIndex>
1362         <gnm:SheetName>Sheet1</gnm:SheetName>
1363         <gnm:SheetName>Sheet2</gnm:SheetName>
1364         <gnm:SheetName>Sheet3</gnm:SheetName>
1365         </gnm:SheetNameIndex>
1366         */
1367         // has to set sheet name on index and body..
1368         Roo.log(sheet);
1369         Roo.log(name);
1370         var sheetnames = this.doc.getElementsByTagNameNS('*','SheetName');
1371         if (sheet >=  sheetnames.length) {
1372             
1373             sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1374             // copy body.
1375             sheetnames = this.doc.getElementsByTagNameNS('*','Sheet');
1376             sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1377             var sn = this.doc.getElementsByTagNameNS('*','Sheet')[sheet];
1378             var cls = sn.getElementsByTagNameNS('*','Cells')[0];
1379             while (cls.childNodes.length) {
1380                 cls.removeChild(cls.firstChild);
1381             }
1382             
1383         }
1384         
1385         var sheetn = this.doc.getElementsByTagNameNS('*','SheetName')[sheet];
1386         sheetn.textContent = name;
1387         var sheetb = this.doc.getElementsByTagNameNS('*','Sheet')[sheet].getElementsByTagNameNS('*','Name')[0];
1388         sheetb.textContent = name;
1389         this.parseDoc(sheet);
1390         
1391         
1392         
1393         
1394     },
1395      /**
1396      * setColumnWidth: 
1397      * Set the column width
1398      * @param {Number} column number (starts at '0')
1399      * @param {Number} width size of column
1400      **/
1401     setColumnWidth : function(column, width)
1402     {
1403         column = column *1; 
1404         width= width*1;
1405         if (typeof(this.colInfoDom[column]) == 'undefined') {
1406             var cols = this.doc.getElementsByTagNameNS('*','Cols')[0];
1407             var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:ColInfo');
1408             ri.setAttribute('No', column);
1409             ri.setAttribute('Unit', width);
1410             ri.setAttribute('MarginA', 2);
1411             ri.setAttribute('MarginB', 2);
1412             ri.setAttribute('HardSize', 1);
1413             cols.appendChild(ri);
1414             this.colInfo[column] = width;
1415             this.colInfoDom[column]  = ri;
1416             return;
1417         }
1418         this.colInfoDom[column].setAttribute('Unit', width);
1419         
1420     },
1421     
1422     
1423     
1424     
1425     
1426      /**
1427      * toHTML: 
1428      * Convert spreadsheet into a HTML table.
1429      */
1430             
1431     toHTML :function()
1432     {
1433          var _t = this;
1434         function calcWidth(sc, span)
1435         {
1436             var n =0;
1437             for(var i =sc; i< sc+span;i++) {
1438                 n+=_t.colInfo[i];
1439             }   
1440             return n;
1441         }
1442         
1443         var grid = this.grid;
1444         // lets do a basic dump..
1445         var out = '<table style="table-layout:fixed;" cellpadding="0" cellspacing="0">';
1446         for (var r = 0; r < this.rmax;r++) {
1447             out += '<tr style="height:'+this.rowInfo[r]+'px;">';
1448             for (var c = 0; c < this.cmax;c++) {
1449                 if (typeof(grid[r][c]) == 'undefined')  {
1450                     this.createCell(r,c);
1451                     
1452                 }
1453                 var g = grid[r][c];
1454                 
1455                 if (typeof(g.cls) =='undefined') {
1456                     g.cls = [];
1457                 }
1458                 var w= calcWidth(c,g.colspan);
1459                 
1460                 var value = g.value[0] == '=' ? 'CALCULATED' : g.value;
1461                 
1462                 try {
1463                     if(
1464                         g.styles[0].firstElementChild.getAttribute('Format') == "D\\-MMM\\-YYYY;@" &&
1465                         g.value[0] != '=' &&
1466                         !isNaN(value * 1) && 
1467                         value != 0
1468                     ){
1469                         value = new Date(value * 24 * 60 * 60 * 1000 + new Date('1899-12-30').getTime()).format('d-M-Y');
1470                     }
1471                     
1472                 } catch(e) {
1473                     
1474                 }
1475                 
1476                 out+=String.format('<td colspan="{0}" rowspan="{1}"  class="{4}"><div style="{3}">{2}</div></td>', 
1477                     g.colspan, g.rowspan, value,
1478                     'overflow:hidden;' + 
1479                     'width:'+w+'px;' +
1480                    
1481                     'text-overflow:ellipsis;' +
1482                     'white-space:nowrap;',
1483                      g.cls.join(' ')
1484     
1485     
1486                 );
1487                 c+=(g.colspan-1);
1488             }
1489             out += '</tr>';
1490         }
1491         //Roo.log(out);
1492         return out+'</table>';
1493         
1494         
1495         
1496     },
1497     /**
1498      * download:
1499      * @param {String} name  filename to downlaod (without xls)
1500      * @param {String} callback  (optional) - callback to call after callback is complete.
1501      */
1502     download : function(name,callback)
1503     {
1504         name = name || "Missing_download_filename";
1505         
1506         if (this.downloadURL && this.downloadURL.charAt(this.downloadURL.length-1) != '/') {
1507             this.downloadURL += '/';
1508         }
1509         
1510         var ser = new XMLSerializer();
1511         var x = new Pman.Download({
1512             method: 'POST',
1513             timeout : 120000, // quite a long wait.. 2 minutes.
1514             params : {
1515                xml : ser.serializeToString(this.doc),
1516                format : 'xls', //xml
1517                debug : 0
1518                
1519             },
1520             url : (this.downloadURL || (baseURL + '/GnumericToExcel/')) + name + '.xls',
1521             success : function() {
1522                 Roo.MessageBox.alert("Alert", "File should have downloaded now");
1523                 if (callback) {
1524                     callback();
1525                 }
1526             }
1527         });
1528          
1529     }
1530
1531 });