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