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);
81 Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
84 * @cfg {String} url the source of the Gnumeric document.
88 * @cfg {Object} data overlay data for spreadsheet - from constructor.
92 * @cfg {String} downloadUrl where GnumerictoExcel.php is...
98 * @type {XmlDocument} doc the gnumeric xml document
103 * @type {XmlNode} sheet the 'Sheet' element
107 * @type {Object} grid the map[row][col] = cellData
111 * @type {Object} colInfo - list of column sizes
115 * @type {Object} rowInfo - list of row sizes
120 * @type {Number} cmax - maximum number of columns
124 * @type {Object} rmax - maximum number of rows
128 * @type {String} stylesheetID id of stylesheet created to render spreadsheat
130 stylesheetID : false,
133 * run the connection, parse document and fire load event..
134 * can be run multiple times with new data..
140 this.url = url || this.url;
148 this.colInfo = false;
149 this.rowInfo = false;
153 if (this.stylesheetID) {
155 Roo.util.CSS.removeStyleSheet(this.stylesheetID);
156 this.stylesheetID = false;
161 var c = new Roo.data.Connection();
165 success : function(resp, opts) {
167 _t.doc = resp.responseXML;
173 _t.fireEvent('load', _t);
177 Roo.MessageBox.alert("Error", "Failed to Load Template for Spreadsheet");
190 * convert 'A1' style position to row/column reference
192 * @arg {String} k cell name
193 * @return {Object} as { r: {Number} , c: {Number} }
198 var c = k.charCodeAt(0)-64;
199 var n = k.substring(1);
200 if (k.charCodeAt(1) > 64) {
202 c+=k.charCodeAt(1)-64;
205 return { c:c -1 ,r: (n*1)-1 }
209 * convert 'A1:B1' style position to array of row/column references
211 * @arg {String} k cell range
212 * @return {Array} as [ { r: {Number} , c: {Number} }. { r: {Number} , c: {Number} } ]
214 rangeToRC : function(s) {
215 var ar = s.split(':');
216 return [ this.toRC(ar[0]) , this.toRC(ar[1])]
225 * convert XML document into cells and other data..
228 parseDoc : function()
235 this.sheet = _t.doc.getElementsByTagNameNS('*','Sheet')[0];
236 var cells = this.sheet.getElementsByTagNameNS('*','Cell');
240 Roo.each(cells, function(c) {
242 var row = c.getAttribute('Row') * 1;
243 var col = c.getAttribute('Col') * 1;
244 _t.cmax = Math.max(col+1, _t.cmax);
245 _t.rmax = Math.max(row+1, _t.rmax);
246 var vt = c.getAttribute('ValueType');
247 var vf = c.getAttribute('ValueFormat');
248 var val = c.textContent;
250 if (typeof(_t.grid[row]) == 'undefined') {
253 _t.grid[row][col] = Roo.applyIf({
261 for (var r = 0; r < this.rmax;r++) {
262 if (typeof(this.grid[r]) == 'undefined') {
265 for (var c = 0; c < this.cmax;c++) {
266 if (typeof(this.grid[r][c]) == 'undefined') {
269 //this.print( "[" + r + "]["+c+"]=" + grid[r][c].value +'<br/>');
273 var merge = this.sheet.getElementsByTagNameNS('*','Merge');
275 Roo.each(merge, function(c) {
276 var rc = _t.rangeToRC(c.textContent);
277 //Roo.log(JSON.stringify(rc))
278 if (typeof(_t.grid[rc[0].r][rc[0].c]) == 'undefined') {
279 _t.grid[rc[0].r][rc[0].c] = Roo.apply({}, _t.defaultCell);
282 _t.grid[rc[0].r][rc[0].c].colspan = (rc[1].c - rc[0].c) + 1;
283 _t.grid[rc[0].r][rc[0].c].rowspan = (rc[1].r - rc[0].r) + 1;
284 for(var r = (rc[0].r); r < (rc[1].r+1); r++) {
285 for(var c = rc[0].c; c < (rc[1].c+1); c++) {
286 //Roo.log('adding alias : ' + r+','+c);
287 _t.grid[r][c] = _t.grid[rc[0].r][rc[0].c];
294 var ci = this.sheet.getElementsByTagNameNS('*','ColInfo');
297 Roo.each(ci, function(c) {
298 var count = c.getAttribute('Count') || 1;
299 var s = c.getAttribute('No')*1;
300 for(var i =0; i < count; i++) {
301 _t.colInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
306 ci = this.sheet.getElementsByTagNameNS('*','RowInfo');
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.rowInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
324 * put the style info onto the cell data.
327 overlayStyles : function ()
331 Roo.each(this.styles, function(s) {
333 for (var r = s.r; r < s.r1;r++) {
334 if (typeof(_t.grid[r]) == 'undefined') {
337 for (var c = s.c; c < s.c1;c++) {
338 if (c > _t.cmax) continue;
340 if (typeof(_t.grid[r][c]) == 'undefined') _t.grid[r][c] = Roo.apply({}, _t.defaultCell);
342 if (typeof(g.cls) =='undefined') g.cls = [];
343 if (g.cls.indexOf(s.name) > -1) continue;
351 * read the style information
352 * generates a stylesheet for the current file
353 * this should be disposed of really.....
356 parseStyles : function() {
358 var srs = this.sheet.getElementsByTagNameNS('*','StyleRegion');
363 HAlign : function(ent,v) {
364 ent['text-align'] = { '1' : 'left', '8': 'center', '4' : 'right'}[v] || 'left';
366 VAlign : function(ent,v) {
367 ent['vertical-align'] = { '1' : 'top', '4': 'middel', '8' : 'bottom'}[v] || 'top'
369 Fore : function(ent,v) {
371 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
372 ent['color'] = 'rgb(' + col.join(',') + ')';
374 Back : function(ent,v) {
376 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
377 ent['background-color'] = 'rgb(' + col.join(',') + ')';
379 FontUnit : function(ent,v) {
380 ent['font-size'] = v + 'px';
382 FontBold : function(ent,v) {
384 ent['font-weight'] = 'bold';
386 FontItalic : function(ent,v) {
388 //ent['font-weight'] = 'bold';
390 FontName : function(ent,v) {
391 ent['font-family'] = v;
393 BorderStyle : function(ent,v) {
394 var vv = v.split('-');
395 ent['border-'+vv[0]+'-style'] = 'solid';
396 ent['border-'+vv[0]+'-width'] = vv[1]+'px';
398 BorderColor : function(ent,v) {
399 var vv = v.split('-');
401 Roo.each(vv[1].split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
402 ent['border-'+vv[0]+'-color'] = 'rgb(' + col.join(',') + ')';
405 function add(e, k, v) {
408 if (typeof(map[k]) == 'undefined') {
418 Roo.each(srs, function(sr,n)
421 c : sr.getAttribute('startCol') *1,
422 r : sr.getAttribute('startRow')*1,
423 c1 : (sr.getAttribute('endCol')*1) +1,
424 r1 : (sr.getAttribute('endRow')*1) +1,
427 name : sid +'-gstyle-' + n
431 Roo.each(sr.getElementsByTagNameNS('*','Style')[0].attributes, function(e) {
432 add(ent, e.name, e.value);
434 if (sr.getElementsByTagNameNS('*','Font').length) {
435 Roo.each(sr.getElementsByTagNameNS('*','Font')[0].attributes, function(e) {
436 add(ent, 'Font'+e.name, e.value);
439 add(ent, 'FontName', sr.getElementsByTagNameNS('*','Font')[0].textContent);
442 if (sr.getElementsByTagNameNS('*','StyleBorder').length) {
443 Roo.each(sr.getElementsByTagNameNS('*','StyleBorder')[0].childNodes, function(e) {
447 Roo.each(e.attributes, function(ea) {
448 add(ent, 'Border'+ea.name, e.tagName.split(':')[1].toLowerCase() + '-' + ea.value);
454 css['.'+ent.name] = ent.style;
457 this.styles = styles;
459 this.stylesheetID = sid;
460 Roo.util.CSS.createStyleSheet(css, sid);
463 /* --------------------------------------- AFTER LOAD METHODS... ----------------------- */
466 * Set the value of a cell..
467 * @param {String} cell name of cell, eg. C10
468 * @param {Value} value to put in cell..
470 * Cells should exist at present, we do not make them up...
474 set : function(cell, v) {
476 var cs= typeof(cell) == 'string' ? this.toRC(cell) : cell;
477 Roo.log( this.grid[cs.r][cs.c]);
478 this.grid[cs.r][cs.c].value= v;
479 // need to generate clell if it doe
480 if (typeof(this.grid[cs.r][cs.c].dom) == 'undefined') {
481 Roo.log('no default content for cell:' + cell);
484 this.grid[cs.r][cs.c].dom.textContent= v;
488 * Set the value of a cell..
489 * @param {String} cell name of cell, eg. C10
490 * @param {Value} value to put in cell..
492 * Cells should exist at present, we do not make them up...
495 applyData : function(data)
498 data = data || this.data;
499 for (var r = 0; r < this.rmax;r++) {
500 if (typeof(this.grid[r]) == 'undefined') continue;
501 for (var c = 0; c < this.cmax;c++) {
502 if (typeof(this.grid[r][c]) == 'undefined') {
505 if (!this.grid[r][c].value.length
506 || !this.grid[r][c].value.match(/\{/)) {
510 var x = new Roo.Template({ html: this.grid[r][c].value });
512 var res = x.applyTemplate(data);
513 //Roo.log("set " + r + "," + c + ":"+res)
514 this.set({ r: r, c: c}, x.applyTemplate(data));
516 // Roo.log(e.toString());
529 * Convert spreadsheet into a HTML table.
535 function calcWidth(sc, span)
538 for(var i =sc; i< sc+span;i++) {
544 var grid = this.grid;
545 // lets do a basic dump..
546 var out = '<table style="table-layout:fixed;" cellpadding="0" cellspacing="0">';
547 for (var r = 0; r < this.rmax;r++) {
548 out += '<tr style="height:'+this.rowInfo[r]+'px;">';
549 for (var c = 0; c < this.cmax;c++) {
550 var g = (typeof(grid[r][c]) == 'undefined') ? defaultCell : grid[r][c];
551 if (typeof(g.cls) =='undefined') g.cls = [];
552 var w= calcWidth(c,g.colspan);
553 out+=String.format('<td colspan="{0}" rowspan="{1}" class="{4}"><div style="{3}">{2}</div></td>',
554 g.colspan, g.rowspan, g.value,
558 'text-overflow:ellipsis;' +
559 'white-space:nowrap;',
569 return out+'</table>';
576 * @param {String} name filename to downlaod (without xls)
577 * @param {String} callback (optional) - callback to call after callback is complete.
579 download : function(name,callback)
581 name = name || "Missing_download_filename";
583 var ser = new XMLSerializer();
584 var x = new Pman.Download({
587 xml : ser.serializeToString(this.doc),
588 format : 'xls', //xml
592 url : (this.downloadUrl || (baseURL + '/GnumericToExcel/')) + name + '.xls',
593 success : function() {
594 Roo.MessageBox.alert("Alert", "File should have downloaded now");