1 //<script type="text/javascript">
3 * @class Pman Gnumeric.
4 *-> load up a remote xml file of a gnumeric document.
6 * -> convert into a usable data structure
8 * -> ?? apply templated values ??
9 * -> allow modification of fields
11 * -> render to screen.
13 * -> send for conversion to XLS (via ssconvert)
19 url: rootURL + '/xxx/yyy/templates/reports/myreport.xml',
25 x.applyData({ ... }); // key value data looks for {value} in strings and replaces it..
29 mypanel.update(x.toHTML());
41 * @param {Object} cfg Configuration object.
46 Pman.Gnumeric = function (cfg)
48 cfg.data = cfg.data || {};
58 * Fires when source document has been loaded
59 * @param {Pman.Gnumerci} this
64 Roo.util.Observable.call(this,cfg);
83 Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
86 * @cfg {String} url the source of the Gnumeric document.
90 * @cfg {Object} data overlay data for spreadsheet - from constructor.
94 * @cfg {String} downloadURL where GnumerictoExcel.php is...
100 * @type {XmlDocument} doc the gnumeric xml document
105 * @type {XmlNode} sheet the 'Sheet' element
110 * @type {XmlNode} sheet the 'Cells' element
114 * @type {Object} grid the map[row][col] = cellData
118 * @type {Object} colInfo - list of column sizes
122 * @type {Object} rowInfo - list of row sizes
127 * @type {Number} cmax - maximum number of columns
131 * @type {Object} rmax - maximum number of rows
135 * @type {String} stylesheetID id of stylesheet created to render spreadsheat
137 stylesheetID : false,
140 * run the connection, parse document and fire load event..
141 * can be run multiple times with new data..
147 this.url = url || this.url;
155 this.colInfo = false;
156 this.rowInfo = false;
160 if (this.stylesheetID) {
162 Roo.util.CSS.removeStyleSheet(this.stylesheetID);
163 this.stylesheetID = false;
168 var c = new Roo.data.Connection();
172 success : function(resp, opts) {
174 _t.doc = resp.responseXML;
180 _t.fireEvent('load', _t);
184 Roo.MessageBox.alert("Error", "Failed to Load Template for Spreadsheet");
197 * convert 'A1' style position to row/column reference
199 * @arg {String} k cell name
200 * @return {Object} as { r: {Number} , c: {Number} }
205 var c = k.charCodeAt(0)-64;
206 var n = k.substring(1);
207 if (k.charCodeAt(1) > 64) {
209 c+=k.charCodeAt(1)-64;
212 return { c:c -1 ,r: (n*1)-1 }
216 * convert 'A1:B1' style position to array of row/column references
218 * @arg {String} k cell range
219 * @return {Array} as [ { r: {Number} , c: {Number} }. { r: {Number} , c: {Number} } ]
221 rangeToRC : function(s) {
222 var ar = s.split(':');
223 return [ this.toRC(ar[0]) , this.toRC(ar[1])]
232 * convert XML document into cells and other data..
235 parseDoc : function()
242 this.sheet = _t.doc.getElementsByTagNameNS('*','Sheet')[0];
245 this.cellholder = this.sheet.getElementsByTagNameNS('*','Cells');
246 var cells = this.sheet.getElementsByTagNameNS('*','Cell');
250 Roo.each(cells, function(c) {
252 var row = c.getAttribute('Row') * 1;
253 var col = c.getAttribute('Col') * 1;
254 _t.cmax = Math.max(col+1, _t.cmax);
255 _t.rmax = Math.max(row+1, _t.rmax);
256 var vt = c.getAttribute('ValueType');
257 var vf = c.getAttribute('ValueFormat');
258 var val = c.textContent;
260 if (typeof(_t.grid[row]) == 'undefined') {
263 _t.grid[row][col] = Roo.applyIf({
273 for (var r = 0; r < this.rmax;r++) {
274 if (typeof(this.grid[r]) == 'undefined') {
277 for (var c = 0; c < this.cmax;c++) {
278 if (typeof(this.grid[r][c]) == 'undefined') {
281 //this.print( "[" + r + "]["+c+"]=" + grid[r][c].value +'<br/>');
285 var merge = this.sheet.getElementsByTagNameNS('*','Merge');
287 Roo.each(merge, function(c) {
288 var rc = _t.rangeToRC(c.textContent);
289 //Roo.log(JSON.stringify(rc))
290 if (typeof(_t.grid[rc[0].r][rc[0].c]) == 'undefined') {
291 _t.grid[rc[0].r][rc[0].c] = Roo.applyIf({ r : rc[0].r, c : rc[0].c }, _t.defaultCell);
294 _t.grid[rc[0].r][rc[0].c].colspan = (rc[1].c - rc[0].c) + 1;
295 _t.grid[rc[0].r][rc[0].c].rowspan = (rc[1].r - rc[0].r) + 1;
296 for(var r = (rc[0].r); r < (rc[1].r+1); r++) {
297 for(var c = rc[0].c; c < (rc[1].c+1); c++) {
298 //Roo.log('adding alias : ' + r+','+c);
299 _t.grid[r][c] = _t.grid[rc[0].r][rc[0].c];
306 var ci = this.sheet.getElementsByTagNameNS('*','ColInfo');
309 Roo.each(ci, function(c) {
310 var count = c.getAttribute('Count') || 1;
311 var s = c.getAttribute('No')*1;
312 for(var i =0; i < count; i++) {
313 _t.colInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
318 ci = this.sheet.getElementsByTagNameNS('*','RowInfo');
321 Roo.each(ci, function(c) {
322 var count = c.getAttribute('Count') || 1;
323 var s = c.getAttribute('No')*1;
324 for(var i =0; i < count; i++) {
325 _t.rowInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
336 * put the style info onto the cell data.
339 overlayStyles : function ()
343 Roo.each(this.styles, function(s) {
345 for (var r = s.r; r < s.r1;r++) {
346 if (typeof(_t.grid[r]) == 'undefined') {
349 for (var c = s.c; c < s.c1;c++) {
350 if (c > _t.cmax) continue;
352 if (typeof(_t.grid[r][c]) == 'undefined') _t.grid[r][c] = Roo.applyIf({ r: r , c : c }, _t.defaultCell);
354 if (typeof(g.cls) =='undefined') {
358 if (g.cls.indexOf(s.name) > -1) continue;
360 g.styles.push(s.dom);
368 * read the style information
369 * generates a stylesheet for the current file
370 * this should be disposed of really.....
373 parseStyles : function() {
375 var srs = this.sheet.getElementsByTagNameNS('*','StyleRegion');
380 HAlign : function(ent,v) {
381 ent['text-align'] = { '1' : 'left', '8': 'center', '4' : 'right'}[v] || 'left';
383 VAlign : function(ent,v) {
384 ent['vertical-align'] = { '1' : 'top', '4': 'middel', '8' : 'bottom'}[v] || 'top'
386 Fore : function(ent,v) {
388 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
389 ent['color'] = 'rgb(' + col.join(',') + ')';
391 Back : function(ent,v) {
393 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
394 ent['background-color'] = 'rgb(' + col.join(',') + ')';
396 FontUnit : function(ent,v) {
397 ent['font-size'] = v + 'px';
399 FontBold : function(ent,v) {
401 ent['font-weight'] = 'bold';
403 FontItalic : function(ent,v) {
405 //ent['font-weight'] = 'bold';
407 FontName : function(ent,v) {
408 ent['font-family'] = v;
410 BorderStyle : function(ent,v) {
411 var vv = v.split('-');
412 ent['border-'+vv[0]+'-style'] = 'solid';
413 ent['border-'+vv[0]+'-width'] = vv[1]+'px';
415 BorderColor : function(ent,v) {
416 var vv = v.split('-');
418 Roo.each(vv[1].split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
419 ent['border-'+vv[0]+'-color'] = 'rgb(' + col.join(',') + ')';
422 function add(e, k, v) {
425 if (typeof(map[k]) == 'undefined') {
435 Roo.each(srs, function(sr,n)
438 c : sr.getAttribute('startCol') *1,
439 r : sr.getAttribute('startRow')*1,
440 c1 : (sr.getAttribute('endCol')*1) +1,
441 r1 : (sr.getAttribute('endRow')*1) +1,
442 style : {}, // key val of style for HTML..
443 gstyle : {}, // key val of attributes used..
444 name : sid +'-gstyle-' + n,
449 Roo.each(sr.getElementsByTagNameNS('*','Style')[0].attributes, function(e) {
450 add(ent, e.name, e.value);
452 if (sr.getElementsByTagNameNS('*','Font').length) {
453 Roo.each(sr.getElementsByTagNameNS('*','Font')[0].attributes, function(e) {
454 add(ent, 'Font'+e.name, e.value);
457 add(ent, 'FontName', sr.getElementsByTagNameNS('*','Font')[0].textContent);
460 if (sr.getElementsByTagNameNS('*','StyleBorder').length) {
461 Roo.each(sr.getElementsByTagNameNS('*','StyleBorder')[0].childNodes, function(e) {
465 Roo.each(e.attributes, function(ea) {
466 add(ent, 'Border'+ea.name, e.tagName.split(':')[1].toLowerCase() + '-' + ea.value);
472 css['.'+ent.name] = ent.style;
475 this.styles = styles;
477 this.stylesheetID = sid;
478 Roo.util.CSS.createStyleSheet(css, sid);
481 /* --------------------------------------- AFTER LOAD METHODS... ----------------------- */
484 * Set the value of a cell..
485 * @param {String} cell name of cell, eg. C10
486 * @param {Value} value to put in cell..
488 * Cells should exist at present, we do not make them up...
492 set : function(cell, v) {
494 var cs= typeof(cell) == 'string' ? this.toRC(cell) : cell;
495 //Roo.log( this.grid[cs.r][cs.c]);
496 // need to generate clell if it doe
497 if (typeof(this.grid[cs.r]) == 'undefined') {
498 Roo.log('no row:' + cell);
499 this.grid[cs.r] = []; // create a row..
502 if (typeof(this.grid[cs.r][cs.c]) == 'undefined') {
503 Roo.log('cell not defined:' + cell);
505 if (typeof(this.grid[cs.r][cs.c].dom) == 'undefined') {
506 Roo.log('no default content for cell:' + cell);
513 this.grid[cs.r][cs.c].value= v;
515 this.grid[cs.r][cs.c].dom.textContent= v;
519 copyRow : function(src, dest) {
523 // Roo.log('create Row' + dest);
524 if (typeof(this.grid[dest]) == 'undefined') {
529 for (var c = 0; c < this.cmax; c++) {
531 this.copyCell({ r: src, c: c } , { r: dest, c: c});
534 this.rmax = Math.max(this.rmax, dest +1);
538 createCell: function(r,c)
540 //<gnm:Cell Row="6" Col="5" ValueType="60">Updated</gnm:Cell>
541 var nc = this.doc.createElement('gnm:Cell');
542 this.cellholder.appendChild(nc);
543 nc.setAttribute('Row', r);
544 nc.setAttribute('Col', c);
545 nc.setAttribute('ValueType', '60');
552 copyCell : function(src, dest)
554 var old = this.grid[src.r][src.c];
556 if ((old.c != src.c) || (old.r != src.r)) {
557 // only really works on horizonatal merges..
559 this.grid[dest.r][dest.c] = this.grid[desc.r][old.c]; // let's hope it exists.
564 var nc = Roo.apply({}, this.grid[src.r][src.c]);
567 if (typeof(old.dom) == 'undefined') {
568 Roo.log("No cell to copy for " + Roo.encode(src));
571 this.grid[dest.r][dest.c] = nc;
572 nc.dom = old.dom.cloneNode(true);
573 nc.dom.setAttribute('Row', dest.r);
574 nc.dom.setAttribute('Cell', dest.c);
575 nc.dom.textContent = '';
576 old.dom.parentNode.appendChild(nc.dom);
577 if (!old.styles || !old.styles.length) {
584 Roo.each(old.styles, function(s) {
585 // try and extend existing styles..
586 var er = s.getAttribute('endRow') * 1;
587 var ec = s.getAttribute('endCol') * 1;
590 s.setAttribute('endRow', dest.r + 1);
593 s.setAttribute('endCol', dest.c + 1);
595 /*var ns = s.cloneNode(true);
596 s.parentNode.appendChild(ns);
597 ns.setAttribute('startCol', dest.c);
598 ns.setAttribute('startRow', dest.r);
599 ns.setAttribute('endCol', dest.c + 1);
600 ns.setAttribute('endRow', dest.r +1);
609 * Set the value of a cell..
610 * @param {String} cell name of cell, eg. C10
611 * @param {Value} value to put in cell..
613 * Cells should exist at present, we do not make them up...
616 applyData : function(data)
619 data = data || this.data;
620 for (var r = 0; r < this.rmax;r++) {
621 if (typeof(this.grid[r]) == 'undefined') continue;
622 for (var c = 0; c < this.cmax;c++) {
623 if (typeof(this.grid[r][c]) == 'undefined') {
626 if (!this.grid[r][c].value.length
627 || !this.grid[r][c].value.match(/\{/)) {
631 var x = new Roo.Template({ html: this.grid[r][c].value });
633 var res = x.applyTemplate(data);
634 //Roo.log("set " + r + "," + c + ":"+res)
635 this.set({ r: r, c: c}, x.applyTemplate(data));
637 // Roo.log(e.toString());
650 * Convert spreadsheet into a HTML table.
656 function calcWidth(sc, span)
659 for(var i =sc; i< sc+span;i++) {
665 var grid = this.grid;
666 // lets do a basic dump..
667 var out = '<table style="table-layout:fixed;" cellpadding="0" cellspacing="0">';
668 for (var r = 0; r < this.rmax;r++) {
669 out += '<tr style="height:'+this.rowInfo[r]+'px;">';
670 for (var c = 0; c < this.cmax;c++) {
671 var g = (typeof(grid[r][c]) == 'undefined') ? defaultCell : grid[r][c];
673 if (typeof(g.cls) =='undefined') g.cls = [];
674 var w= calcWidth(c,g.colspan);
675 out+=String.format('<td colspan="{0}" rowspan="{1}" class="{4}"><div style="{3}">{2}</div></td>',
676 g.colspan, g.rowspan, g.value,
680 'text-overflow:ellipsis;' +
681 'white-space:nowrap;',
691 return out+'</table>';
698 * @param {String} name filename to downlaod (without xls)
699 * @param {String} callback (optional) - callback to call after callback is complete.
701 download : function(name,callback)
703 name = name || "Missing_download_filename";
705 if (this.downloadURL && this.downloadURL.charAt(this.downloadURL .length-1) != '/') {
706 this.downloadURL += '/';
709 var ser = new XMLSerializer();
710 var x = new Pman.Download({
713 xml : ser.serializeToString(this.doc),
714 format : 'xls', //xml
718 url : (this.downloadURL || (baseURL + '/GnumericToExcel/')) + name + '.xls',
719 success : function() {
720 Roo.MessageBox.alert("Alert", "File should have downloaded now");