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} colInfoDom - column sizes dom element
126 * @type {Object} rowInfo - list of row sizes
130 * @type {Object} rowInfoDom - dom elements with sizes
134 * @type {Number} cmax - maximum number of columns
138 * @type {Object} rmax - maximum number of rows
142 * @type {String} stylesheetID id of stylesheet created to render spreadsheat
144 stylesheetID : false,
146 * @type {Number} rowOffset - used by table importer to enable multiple tables to be improted
153 * run the connection, parse document and fire load event..
154 * can be run multiple times with new data..
160 this.url = url || this.url;
168 this.colInfo = false;
169 this.colInfoDom = false;
170 this.rowInfo = false;
171 this.rowInfoDom = false;
175 if (this.stylesheetID) {
177 Roo.util.CSS.removeStyleSheet(this.stylesheetID);
178 this.stylesheetID = false;
183 var c = new Roo.data.Connection();
187 success : function(resp, opts) {
189 _t.doc = resp.responseXML;
196 _t.fireEvent('load', _t);
200 Roo.MessageBox.alert("Error", "Failed to Load Template for Spreadsheet");
209 RCtoCell : function(r,c)
211 // we wil only support AA not AAA
212 var top = Math.floor(c/26);
214 var cc = top > 0 ? String.fromCharCode('A'.charCodeAt(0) + top) : '';
215 cc += String.fromCharCode('A'.charCodeAt(0) + bot);
222 * convert 'A1' style position to row/column reference
224 * @arg {String} k cell name
225 * @return {Object} as { r: {Number} , c: {Number} }
230 var c = k.charCodeAt(0)-64;
231 var n = k.substring(1);
232 if (k.charCodeAt(1) > 64) {
234 c+=k.charCodeAt(1)-64;
237 return { c:c -1 ,r: (n*1)-1 }
241 * convert 'A1:B1' style position to array of row/column references
243 * @arg {String} k cell range
244 * @return {Array} as [ { r: {Number} , c: {Number} }. { r: {Number} , c: {Number} } ]
246 rangeToRC : function(s) {
247 var ar = s.split(':');
248 return [ this.toRC(ar[0]) , this.toRC(ar[1])]
257 * convert XML document into cells and other data..
260 parseDoc : function(sheetnum)
267 this.sheet = _t.doc.getElementsByTagNameNS('*','Sheet')[sheetnum];
270 this.cellholder = this.sheet.getElementsByTagNameNS('*','Cells')[0];
271 var cells = this.sheet.getElementsByTagNameNS('*','Cell');
275 Roo.each(cells, function(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;
285 if (typeof(_t.grid[row]) == 'undefined') {
288 _t.grid[row][col] = Roo.applyIf({
298 for (var r = 0; r < this.rmax;r++) {
299 if (typeof(this.grid[r]) == 'undefined') {
302 for (var c = 0; c < this.cmax;c++) {
303 if (typeof(this.grid[r][c]) == 'undefined') {
306 //this.print( "[" + r + "]["+c+"]=" + grid[r][c].value +'<br/>');
310 var merge = this.sheet.getElementsByTagNameNS('*','Merge');
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);
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];
332 var ci = this.sheet.getElementsByTagNameNS('*','ColInfo');
334 this.colInfoDom = {};
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;
346 ci = this.sheet.getElementsByTagNameNS('*','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);
368 * put the style info onto the cell data.
371 overlayStyles : function ()
375 Roo.each(this.styles, function(s) {
377 for (var r = s.r; r < s.r1;r++) {
378 if (typeof(_t.grid[r]) == 'undefined') {
381 for (var c = s.c; c < s.c1;c++) {
386 if (typeof(_t.grid[r][c]) == 'undefined') {
388 //_t.grid[r][c] = Roo.applyIf({ r: r , c : c }, _t.defaultCell);
391 if (typeof(g.cls) =='undefined') {
395 if (g.cls.indexOf(s.name) > -1) {
399 g.styles.push(s.dom);
407 * read the style information
408 * generates a stylesheet for the current file
409 * this should be disposed of really.....
412 parseStyles : function() {
414 var srs = this.sheet.getElementsByTagNameNS('*','StyleRegion');
419 HAlign : function(ent,v) {
420 ent['text-align'] = { '1' : 'left', '8': 'center', '4' : 'right'}[v] || 'left';
422 VAlign : function(ent,v) {
423 ent['vertical-align'] = { '1' : 'top', '4': 'middle', '8' : 'bottom'}[v] || 'top'
425 Fore : function(ent,v) {
427 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
428 ent['color'] = 'rgb(' + col.join(',') + ')';
430 Back : function(ent,v) {
432 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
433 ent['background-color'] = 'rgb(' + col.join(',') + ')';
435 FontUnit : function(ent,v) {
436 ent['font-size'] = v + 'px';
438 FontBold : function(ent,v) {
439 if (v*1 < 1) { return; }
440 ent['font-weight'] = 'bold';
442 FontItalic : function(ent,v) {
443 if (v*0 < 1) { return; }
444 //ent['font-weight'] = 'bold';
446 FontName : function(ent,v) {
447 ent['font-family'] = v;
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';
454 BorderColor : function(ent,v) {
455 var vv = v.split('-');
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(',') + ')';
461 function add(e, k, v) {
464 if (typeof(map[k]) == 'undefined') {
474 Roo.each(srs, function(sr,n)
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,
488 Roo.each(sr.getElementsByTagNameNS('*','Style')[0].attributes, function(e) {
489 add(ent, e.name, e.value);
491 if (sr.getElementsByTagNameNS('*','Font').length) {
492 Roo.each(sr.getElementsByTagNameNS('*','Font')[0].attributes, function(e) {
493 add(ent, 'Font'+e.name, e.value);
496 add(ent, 'FontName', sr.getElementsByTagNameNS('*','Font')[0].textContent);
499 if (sr.getElementsByTagNameNS('*','StyleBorder').length) {
500 Roo.each(sr.getElementsByTagNameNS('*','StyleBorder')[0].childNodes, function(e) {
504 Roo.each(e.attributes, function(ea) {
505 add(ent, 'Border'+ea.name, e.tagName.split(':')[1].toLowerCase() + '-' + ea.value);
511 css['.'+ent.name] = ent.style;
514 this.styles = styles;
516 this.stylesheetID = sid;
517 Roo.util.CSS.createStyleSheet(css, sid);
523 /* --------------------------------------- AFTER LOAD METHODS... ----------------------- */
526 * Set the value of a cell..
527 * @param {String} cell name of cell, eg. C10 or { c: 1, r :1 }
529 * @param {Value} value to put in cell..
530 * @param {ValueType} type of value
531 * @param {ValueFormat} value format of cell
533 * Cells should exist at present, we do not make them up...
537 set : function(cell, v, vt, vf) {
539 var cs= typeof(cell) == 'string' ? this.toRC(cell) : cell;
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..
549 if (typeof(this.grid[cs.r][cs.c]) == 'undefined') {
550 Roo.log('cell not defined:' + cell);
551 this.createCell(cs.r,cs.c);
553 // cell might not be rendered yet... so if we try and create a cell, it overrides the default formating..
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);
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;
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');
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');
585 copyRow : function(src, dest) {
589 // Roo.log('create Row' + dest);
590 if (typeof(this.grid[dest]) == 'undefined') {
595 for (var c = 0; c < this.cmax; c++) {
597 this.copyCell({ r: src, c: c } , { r: dest, c: c});
600 this.rmax = Math.max(this.rmax, dest +1);
606 createCell: function(r,c)
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);
614 nc.setAttribute('Row', new String(r));
615 nc.setAttribute('Col', new String(c));
616 nc.setAttribute('ValueType', '60');
619 this.grid[r][c] = Roo.applyIf({
633 copyCell : function(src, dest)
635 var old = this.grid[src.r][src.c];
637 if ((old.c != src.c) || (old.r != src.r)) {
638 // only really works on horizonatal merges..
640 this.grid[dest.r][dest.c] = this.grid[desc.r][old.c]; // let's hope it exists.
645 var nc = Roo.apply({}, this.grid[src.r][src.c]);
648 if (typeof(old.dom) == 'undefined') {
649 Roo.log("No cell to copy for " + Roo.encode(src));
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) {
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;
671 s.setAttribute('endRow', dest.r + 1);
674 s.setAttribute('endCol', dest.c + 1);
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);
690 * Set the value of a cell..
691 * @param {String} cell name of cell, eg. C10
692 * @param {Value} value to put in cell..
694 * Cells should exist at present, we do not make them up...
697 applyData : function(data)
700 data = data || this.data;
701 for (var r = 0; r < this.rmax;r++) {
702 if (typeof(this.grid[r]) == 'undefined') {
705 for (var c = 0; c < this.cmax;c++) {
706 if (typeof(this.grid[r][c]) == 'undefined') {
709 if (!this.grid[r][c].value.length
710 || !this.grid[r][c].value.match(/\{/)) {
714 var x = new Roo.Template({ html: this.grid[r][c].value });
716 var res = x.applyTemplate(data);
717 //Roo.log("set " + r + "," + c + ":"+res)
718 this.set({ r: r, c: c}, x.applyTemplate(data));
720 // Roo.log(e.toString());
730 readTableData : function(table)
732 // read the first row.
733 var tds = Roo.get(table).select('tr').item(0).select('td');
736 Roo.get(table).select('tr').each(function(trs) {
739 trs.select('td').each(function(td) {
740 var cs = td.dom.getAttribute('colspan');
741 cs = cs ? cs * 1 : 1;
744 maxnc = Math.max(nc, maxnc);
747 var tr = document.createElement('tr');
748 table.appendChild(tr);
750 for (i =0; i < maxnc; i++) {
751 ar[i] = document.createElement('td');
752 tr.appendChild(ar[i]);
755 var ret = { cols : maxnc, pos : {} };
756 for (i =0; i < maxnc; i++) {
757 ret.pos[ Roo.get(ar[i]).getLeft()] =i;
759 ret.near = function(p) {
760 // which one is nearest..
767 for(var i in this.pos) {
768 var dis = Math.abs(p-i);
777 table.removeChild(tr);
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
793 importTable : function (datagrid, xoff,yoff)
796 Roo.log("Error table not found!?");
803 var table_data = this.readTableData(datagrid);
805 // oroginally this cleaned line breaks, but we acutally need them..
806 var cleanHTML = function (str) {
809 ret = ret.replace(/ /g,' ');
810 // ret = ret.replace(/\n/g,'.');
811 // ret = ret.replace(/\r/g,'.');
818 // <cell col="A" row="1">Test< / cell>
819 // <cell col="B" row="2" type="Number" format="test1">30< / cell>
821 var rows = datagrid.getElementsByTagName('tr');
822 //alert(rows.length);
825 for(var row=0;row<rows.length;row++) {
827 // let's see what affect this has..
828 // it might mess things up..
830 if (rows[row].getAttribute('xls:height')) {
831 this.setRowHeight(row + yoff +1, 1* rows[row].getAttribute('xls:height'));
833 this.setRowHeight( row + yoff +1, Roo.get(rows[row]).getHeight());
836 var cols = rows[row].getElementsByTagName('td');
838 for(var col=0;col < cols.length; col++) {
840 if (cols[col].getAttribute('xls:width')) {
841 this.setColumnWidth(col, 1 * cols[col].getAttribute('xls:width'));
844 var colspan = cols[col].getAttribute('colspan');
845 colspan = colspan ? colspan *1 : 1;
847 var rowspan = cols[col].getAttribute('rowspan');
848 rowspan = rowspan ? rowspan * 1 : 1;
850 var realcol = table_data.near( Roo.get(cols[col]).getLeft() );
854 if (colspan > 1 || rowspan > 1) {
856 // getting thisese right is tricky..
860 realcol+ xoff + (colspan -1),
867 // set the style first..
868 this.parseHtmlStyle( cols[col], row + yoff, realcol + xoff , colspan, rowspan);
870 if (!cols[col].childNodes.length) {
880 var xlstype = cols[col].getAttribute('xls:type');
888 vt = 40; // float!!!!
889 if (cols[col].getAttribute('xls:floatformat')) {
890 vf = cols[col].getAttribute('xls:floatformat');
896 //ValueFormat="d/m/yyyy" 38635
898 if (cols[col].getAttribute('xls:dateformat')) {
899 vf= cols[col].getAttribute('xls:dateformat');
911 if (!cols[col].childNodes[0].nodeValue) {
915 if (!cols[col].childNodes[0].nodeValue.replace(/^\s*|\s*$/g,"").length) {
920 var cell_value_text = cleanHTML(cols[col].childNodes[0].nodeValue);
922 if (cols[col].getAttribute('xls:percent')) {
923 cell_value_text = '' + ((cell_value_text * 1) / 100);
926 if (cell_value_text.length && (vt == 30) && xlstype == 'date') {
927 var bits = cell_value_text.split(/-/);
928 var cur = new Date(bits[0],bits[1]-1,bits[2]);
929 cell_value_text = '' + Math.round((cur.getTime() - Date.UTC(1899,11,30)) / (24 * 60 * 60 * 1000));
934 if (cols[col].getAttribute('xls:formula')) {
935 var s = cols[col].getAttribute('xls:formula');
937 cell_value_text = s.replace(/#row#/g,(row + yoff + 1));
939 this.set({ r: row + yoff, c : realcol + xoff }, cell_value_text, vt, vf);
947 this.rowOffset += rows.length;
953 parseHtmlStyle : function(dom, row, col, colspan, rowspan) {
955 function toCol (rgb) {
957 var ar = rgb.replace(/rgb[a]?\(/, '').replace(/\)/, '').replace(/ /, '').split(',');
960 Roo.each(ar, function(c) {
961 rcs.push((c*c).toString(16)) ;
963 return rcs.join(':');
967 var el = Roo.get(dom);
969 'text-align' : function(ent,v) {
970 ent['HAlign'] = { 'left' : '1', 'center' : '8' , 'right' : '4' }[v] || '1';
972 'vertical-align': function(ent,v) {
973 ent['VAlign'] = { 'top' : '1', 'middel' : '8' , 'bottom' : '4' }[v] || '1';
976 'color': function(ent,v) {
977 ent['Fore'] = toCol(v);
978 // this is a bit dumb.. we assume that if it's not black text, then it's shaded..
979 if (ent['Fore'] != '0:0:0') {
984 'background-color' : function(ent,v) {
985 ent['Back'] = toCol(v);
1002 Back:"FFFF:FFFF:FFFF",
1003 PatternColor:"0:0:0",
1008 var val = el.getStyle(k);
1009 if (!val || !val.length) {
1015 if (el.dom.getAttribute('xls:wraptext')) {
1018 if (el.dom.getAttribute('xls:valign')) {
1021 if (el.dom.getAttribute('xls:halign')) {
1028 'font-size' : function(ent,v) {
1029 ent['Unit'] = v.replace(/px/, '');
1031 'font-weight' : function(ent,v) {
1037 'font-style' : function(ent,v) {
1038 if (v != 'italic') {
1053 for(var k in fmap) {
1054 var val = el.getStyle(k);
1055 if (!val || !val.length) {
1060 var font = el.getStyle('font-family') || 'Sans';
1061 if (font.split(',').length > 1) {
1062 font = font.split(',')[1].replace(/\s+/, '');
1066 /// -- now create elements..
1068 var objs = this.sheet.getElementsByTagNameNS('*','Styles')[0];
1070 //<gnm:StyleRegion startCol="0" startRow="0" endCol="255" endRow="65535"
1071 var sr = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleRegion');
1072 objs.appendChild(sr);
1073 objs.appendChild(this.doc.createTextNode("\n"));// add a line break..
1075 sr.setAttribute('startCol', col);
1076 sr.setAttribute('endCol', col+ colspan-1);
1077 sr.setAttribute('startRow', row);
1078 sr.setAttribute('endRow', row + rowspan -1);
1081 var st = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Style');
1083 // do we need some defaults..
1086 st.setAttribute(k, ent[k]);
1089 var fo = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Font');
1091 // do we need some defaults..
1092 for(var k in fent) {
1093 fo.setAttribute(k, fent[k]);
1095 fo.textContent = font;
1099 Roo.each(['top','left','bottom','right'], function(p) {
1100 var w = el.getStyle('border-' + p + '-width').replace(/px/, '');
1101 if (!w || !w.length || (w*1) < 1) {
1105 sb= this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleBorder');
1107 var be = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:' + p[0].toUpperCase() + p.substring(1));
1108 be.setAttribute('Style', '1');
1109 be.setAttribute('Color', '0:0:0'); // fixme..
1113 // start adding them all together..
1126 * write an image in old gnumberic format (needs base64 data to write it)
1129 * @param {Number} row row to put it in (rows start at 0)
1130 * @param {Number} col column to put it in
1131 * @param {Number} data the base64 description of the images
1132 * @param {Number} width image width
1133 * @param {Number} width image height
1136 writeImageOld : function (row, col, data, width, height, type, size)
1140 throw "write Image called with missing data";
1147 var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1148 var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1152 for ( endcol=col;endcol <100; endcol++) {
1153 if (!this.colInfo[endcol]) {
1154 this.colInfo[endcol] = 100; // eak fudge
1156 colwidth += this.colInfo[endcol];
1157 if (colwidth > width) {
1162 soi.setAttribute('ObjectBound', this.RCtoCell(row,col) + ':' + this.RCtoCell(row,endcol));
1164 var ww = 0.01; // offset a bit...
1167 var ww2 = 1 - ((colwidth - width) / this.colInfo[endcol]);
1170 var offset_str = ww + ' ' + hh + ' ' + ww2 + ' '+hh2;
1172 //alert(offset_str);
1173 soi.setAttribute('ObjectOffset', offset_str);
1174 soi.setAttribute('ObjectAnchorType','16 16 16 16');
1175 soi.setAttribute('Direction','17');
1176 soi.setAttribute('crop-top','0.000000');
1177 soi.setAttribute('crop-bottom','0.000000');
1178 soi.setAttribute('crop-left','0.000000');
1179 soi.setAttribute('crop-right','0.000000');
1181 var content = this.doc.createElement('Content');
1182 content.setAttribute('image-type', type ? type : 'jpeg');
1183 content.setAttribute('size-bytes', size);
1184 soi.appendChild(content);
1185 objs.appendChild(soi);
1187 var godoc = this.doc.getElementsByTagNameNS('*','GODoc')[0];
1189 var goimage = this.doc.createElement('GOImage');
1190 goimage.setAttribute('image-type', type ? type : 'jpeg');
1191 goimage.setAttribute('name', name);
1192 goimage.setAttribute('type', 'GOPixbuf');
1193 goimage.setAttribute('width', width);
1194 goimage.setAttribute('height', height);
1195 goimage.textContent = data;
1197 godoc.appendChild(goimage);
1199 if (typeof(this.grid[row]) == 'undefined') {
1200 this.grid[row] = [];
1202 if (typeof(this.grid[row][col]) == 'undefined') {
1203 this.createCell(row,col);
1206 this.grid[row][col].value= data;
1207 this.grid[row][col].valueFormat = 'image';
1208 this.grid[row][col].imageType = type;
1209 this.grid[row][col].width = width;
1210 this.grid[row][col].height = height;
1213 //< /gnm:SheetObjectImage>
1220 * write an image (needs base64 data to write it)
1223 * @param {Number} row row to put it in (rows start at 0)
1224 * @param {Number} col column to put it in
1225 * @param {Number} data the base64 description of the images
1226 * @param {Number} width image width
1227 * @param {Number} width image height
1230 writeImage : function (row, col, data, width, height, type)
1234 throw "write Image called with missing data";
1236 // our default height width is 50/50 ?!
1237 //console.log('w='+width+',height='+height);
1243 var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1244 var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1246 //<gmr:SheetObjectImage
1247 // ObjectBound="A3:J8"
1248 // ObjectOffset="0.375 0.882 0.391 0.294"
1249 // ObjectAnchorType="16 16 16 16"
1251 // crop-top="0.000000"
1252 // crop-bottom="0.000000"
1253 // crop-left="0.000000"
1254 // crop-right="0.000000">
1257 //alert(gnumeric_colRowToName(row,col));
1259 // this is where we really have fun!!!...
1260 // since our design currently assumes the height is enough to fit
1261 // stuff in, we only really need to work out how wide it has to be..
1263 // note we should probably use centralized calcs if it fits in the first cell!
1265 // step 1 - work out how many columns it will span..
1266 // lets hope the spreadsheet is big enought..
1269 for ( endcol=col;endcol <100; endcol++) {
1270 if (!this.colInfo[endcol]) {
1271 this.colInfo[endcol] = 100; // eak fudge
1273 colwidth += this.colInfo[endcol];
1274 if (colwidth > width) {
1279 soi.setAttribute('ObjectBound',
1280 //gnumeric_colRowToName(row,col) + ':' + gnumeric_colRowToName(row+1,col+1));
1281 this.RCtoCell(row,col) + ':' + this.RCtoCell(row,endcol));
1283 var ww = 0.01; // offset a bit...
1286 var ww2 = 1 - ((colwidth - width) / this.colInfo[endcol]);
1289 var offset_str = ww + ' ' + hh + ' ' + ww2 + ' '+hh2;
1291 //alert(offset_str);
1292 soi.setAttribute('ObjectOffset', offset_str);
1293 soi.setAttribute('ObjectAnchorType','16 16 16 16');
1294 soi.setAttribute('Direction','17');
1295 soi.setAttribute('crop-top','0.000000');
1296 soi.setAttribute('crop-bottom','0.000000');
1297 soi.setAttribute('crop-left','0.000000');
1298 soi.setAttribute('crop-right','0.000000');
1299 // <Content image-type="jpeg" size-bytes="3900">...... < / Content>
1301 var name = 'Image' + Math.random().toString(36).substring(2);
1302 var content = this.doc.createElement('Content');
1303 content.setAttribute('image-type', type ? type : 'jpeg');
1304 content.setAttribute('name', name);
1305 soi.appendChild(content);
1306 objs.appendChild(soi);
1308 var godoc = this.doc.getElementsByTagNameNS('*','GODoc')[0];
1310 var goimage = this.doc.createElement('GOImage');
1311 goimage.setAttribute('image-type', type ? type : 'jpeg');
1312 goimage.setAttribute('name', name);
1313 goimage.setAttribute('type', 'GOPixbuf');
1314 goimage.setAttribute('width', width);
1315 goimage.setAttribute('height', height);
1316 goimage.textContent = data;
1318 godoc.appendChild(goimage);
1320 if (typeof(this.grid[row]) == 'undefined') {
1321 this.grid[row] = [];
1323 if (typeof(this.grid[row][col]) == 'undefined') {
1324 this.createCell(row,col);
1327 this.grid[row][col].value= data;
1328 this.grid[row][col].valueFormat = 'image';
1329 this.grid[row][col].imageType = type;
1330 this.grid[row][col].width = width;
1331 this.grid[row][col].height = height;
1334 //< /gnm:SheetObjectImage>
1340 * writeFixedImageOld:
1341 * write an image in old gnumberic format (needs base64 data to write it)
1343 writeFixedImageOld : function (startCol, startRow, endCol, endRow, type, data, width, height, size)
1346 throw "write Image called with missing data";
1349 startCol = startCol * 1;
1350 startRow = startRow * 1;
1351 endCol = endCol * 1;
1352 endRow = endRow * 1;
1354 height = height * 1;
1356 var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1357 var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1359 soi.setAttribute('ObjectBound',this.RCtoCell(startRow, startCol) + ':' + this.RCtoCell(endRow, endCol));
1361 soi.setAttribute('ObjectOffset', '0 0 0 0');
1362 soi.setAttribute('ObjectAnchorType','16 16 16 16');
1363 soi.setAttribute('Direction','17');
1364 soi.setAttribute('crop-top','0.000000');
1365 soi.setAttribute('crop-bottom','0.000000');
1366 soi.setAttribute('crop-left','0.000000');
1367 soi.setAttribute('crop-right','0.000000');
1369 var content = this.doc.createElement('Content');
1370 content.setAttribute('image-type', type ? type : 'jpeg');
1371 content.setAttribute('size-bytes', size);
1372 content.textContent = data;
1373 soi.appendChild(content);
1374 objs.appendChild(soi);
1376 if (typeof(this.grid[startRow]) == 'undefined') {
1377 this.grid[startRow] = [];
1379 if (typeof(this.grid[startRow][startCol]) == 'undefined') {
1380 this.createCell(startRow,startCol);
1383 this.grid[startRow][startCol].value= data;
1384 this.grid[startRow][startCol].valueFormat = 'image';
1385 this.grid[startRow][startCol].imageType = type;
1386 this.grid[startRow][startCol].width = width;
1387 this.grid[startRow][startCol].height = height;
1392 writeFixedImage : function (startCol, startRow, endCol, endRow, type, data, width, height)
1395 throw "write Image called with missing data";
1398 startCol = startCol * 1;
1399 startRow = startRow * 1;
1400 endCol = endCol * 1;
1401 endRow = endRow * 1;
1403 height = height * 1;
1405 var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1406 var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1408 soi.setAttribute('ObjectBound',this.RCtoCell(startRow, startCol) + ':' + this.RCtoCell(endRow, endCol));
1410 soi.setAttribute('ObjectOffset', '0 0 0 0');
1411 soi.setAttribute('ObjectAnchorType','16 16 16 16');
1412 soi.setAttribute('Direction','17');
1413 soi.setAttribute('crop-top','0.000000');
1414 soi.setAttribute('crop-bottom','0.000000');
1415 soi.setAttribute('crop-left','0.000000');
1416 soi.setAttribute('crop-right','0.000000');
1418 var name = 'Image' + Math.random().toString(36).substring(2);
1419 var content = this.doc.createElement('Content');
1420 content.setAttribute('image-type', type ? type : 'jpeg');
1421 content.setAttribute('name', name);
1422 soi.appendChild(content);
1423 objs.appendChild(soi);
1425 var godoc = this.doc.getElementsByTagNameNS('*','GODoc')[0];
1427 var goimage = this.doc.createElement('GOImage');
1428 goimage.setAttribute('image-type', type ? type : 'jpeg');
1429 goimage.setAttribute('name', name);
1430 goimage.setAttribute('type', 'GOPixbuf');
1431 goimage.setAttribute('width', width);
1432 goimage.setAttribute('height', height);
1433 goimage.textContent = data;
1435 godoc.appendChild(goimage);
1437 if (typeof(this.grid[startRow]) == 'undefined') {
1438 this.grid[startRow] = [];
1440 if (typeof(this.grid[startRow][startCol]) == 'undefined') {
1441 this.createCell(startRow,startCol);
1444 this.grid[startRow][startCol].value= data;
1445 this.grid[startRow][startCol].valueFormat = 'image';
1446 this.grid[startRow][startCol].imageType = type;
1447 this.grid[startRow][startCol].width = width;
1448 this.grid[startRow][startCol].height = height;
1455 * Merge cells in the spreadsheet. (does not check if existing merges exist..)
1457 * @param {Number} col1 first column
1458 * @param {Number} row1 first row
1459 * @param {Number} col2 to column
1460 * @param {Number} row2 to row
1463 mergeRegion : function (col1,row1,col2,row2)
1465 var cell = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Merge');
1466 //if (col1 > 50|| col2 > 50) { // do not merge cols off to right?
1470 cell.textContent = this.RCtoCell(row1,col1) + ':' + this.RCtoCell(row2,col2);
1472 //var merges = this.gnumeric.getElementsByTagNameNS('*','MergedRegions');
1473 var merges = this.sheet.getElementsByTagNameNS('*','MergedRegions');
1474 if (!merges || !merges.length) {
1475 merges = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:MergedRegions');
1476 var sl = this.sheet.getElementsByTagNameNS('*','SheetLayout')[0];
1477 this.sheet.insertBefore(merges,sl);
1481 merges.appendChild(cell);
1486 * Sets the height of a row.
1488 * @param {Number} r the row to set the height of. (rows start at 0)
1489 * @param {Number} height (in pixels)
1491 setRowHeight : function (r,height)
1494 //<gmr:Rows DefaultSizePts="12.75">
1495 // <gmr:RowInfo No="2" Unit="38.25" MarginA="0" MarginB="0" HardSize="1"/>
1498 // this doesnt handle row ranges very well.. - with 'count in them..'
1500 if (this.rowInfoDom[r]) {
1501 this.rowInfoDom[r].setAttribute('Unit', height);
1505 var rows = this.sheet.getElementsByTagNameNS('*','Rows')[0]; // assume this exists..
1506 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:RowInfo');
1507 // assume we have no rows..
1508 ri.setAttribute('No', r-1);
1509 ri.setAttribute('Unit', height);
1510 ri.setAttribute('MarginA', 0);
1511 ri.setAttribute('MarginB', 0);
1512 ri.setAttribute('HardSize', 1);
1513 rows.appendChild(ri);
1514 this.rowInfoDom[r] = ri;
1519 * Set the sheet name.
1520 * @param {String} title for sheet
1522 setSheetName : function(name,sheet)
1526 <gnm:SheetNameIndex>
1527 <gnm:SheetName>Sheet1</gnm:SheetName>
1528 <gnm:SheetName>Sheet2</gnm:SheetName>
1529 <gnm:SheetName>Sheet3</gnm:SheetName>
1530 </gnm:SheetNameIndex>
1532 // has to set sheet name on index and body..
1535 var sheetnames = this.doc.getElementsByTagNameNS('*','SheetName');
1536 if (sheet >= sheetnames.length) {
1538 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1540 sheetnames = this.doc.getElementsByTagNameNS('*','Sheet');
1541 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1542 var sn = this.doc.getElementsByTagNameNS('*','Sheet')[sheet];
1543 var cls = sn.getElementsByTagNameNS('*','Cells')[0];
1544 while (cls.childNodes.length) {
1545 cls.removeChild(cls.firstChild);
1550 var sheetn = this.doc.getElementsByTagNameNS('*','SheetName')[sheet];
1551 sheetn.textContent = name;
1552 var sheetb = this.doc.getElementsByTagNameNS('*','Sheet')[sheet].getElementsByTagNameNS('*','Name')[0];
1553 sheetb.textContent = name;
1554 this.parseDoc(sheet);
1562 * Set the column width
1563 * @param {Number} column number (starts at '0')
1564 * @param {Number} width size of column
1566 setColumnWidth : function(column, width)
1570 if (typeof(this.colInfoDom[column]) == 'undefined') {
1571 var cols = this.sheet.getElementsByTagNameNS('*','Cols')[0];
1572 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:ColInfo');
1573 ri.setAttribute('No', column);
1574 ri.setAttribute('Unit', width);
1575 ri.setAttribute('MarginA', 2);
1576 ri.setAttribute('MarginB', 2);
1577 ri.setAttribute('HardSize', 1);
1578 cols.appendChild(ri);
1579 this.colInfo[column] = width;
1580 this.colInfoDom[column] = ri;
1583 this.colInfoDom[column].setAttribute('Unit', width);
1593 * Convert spreadsheet into a HTML table.
1599 function calcWidth(sc, span)
1602 for(var i =sc; i< sc+span;i++) {
1608 var grid = this.grid;
1609 // lets do a basic dump..
1610 var out = '<table style="table-layout:fixed;" cellpadding="0" cellspacing="0">';
1611 for (var r = 0; r < this.rmax;r++) {
1612 out += '<tr style="height:'+this.rowInfo[r]+'px;">';
1613 for (var c = 0; c < this.cmax;c++) {
1614 if (typeof(grid[r][c]) == 'undefined') {
1615 this.createCell(r,c);
1620 if (typeof(g.cls) =='undefined') {
1623 var w= calcWidth(c,g.colspan);
1625 var value = g.value[0] == '=' ? 'CALCULATED' : g.value;
1629 g.styles[0].firstElementChild.getAttribute('Format') == "D\\-MMM\\-YYYY;@" &&
1630 g.value[0] != '=' &&
1631 !isNaN(value * 1) &&
1634 value = new Date(value * 24 * 60 * 60 * 1000 + new Date('1899-12-30').getTime()).format('d-M-Y');
1641 if(g.valueFormat == 'image') {
1643 out+=String.format('<td colspan="{0}" rowspan="{1}" class="{2}"><div style="{3}"><img src="data:image/{4};base64, {5}" width="{6}" height="{7}"></div></td>',
1644 g.colspan, g.rowspan, g.cls.join(' '),
1645 'overflow:hidden;' +
1646 'width:'+g.width+'px;' +
1648 'text-overflow:ellipsis;' +
1649 'white-space:nowrap;',
1651 value, g.width, g.height
1658 out+=String.format('<td colspan="{0}" rowspan="{1}" class="{4}"><div style="{3}">{2}</div></td>',
1659 g.colspan, g.rowspan, value,
1660 'overflow:hidden;' +
1663 'text-overflow:ellipsis;' +
1664 'white-space:nowrap;',
1674 return out+'</table>';
1681 * @param {String} name filename to downlaod (without xls)
1682 * @param {String} callback (optional) - callback to call after callback is complete.
1684 download : function(name,callback)
1686 name = name || "Missing_download_filename";
1688 if (this.downloadURL && this.downloadURL.charAt(this.downloadURL.length-1) != '/') {
1689 this.downloadURL += '/';
1692 var ser = new XMLSerializer();
1693 var x = new Pman.Download({
1695 timeout : 120000, // quite a long wait.. 2 minutes.
1697 xml : ser.serializeToString(this.doc),
1698 format : 'xls', //xml
1702 url : (this.downloadURL || (baseURL + '/GnumericToExcel/')) + name + '.xls',
1703 success : function() {
1704 Roo.MessageBox.alert("Alert", "File should have downloaded now");