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];
333 var ci = this.sheet.getElementsByTagNameNS('*','ColInfo');
335 this.colInfoDom = {};
337 Roo.each(ci, function(c) {
338 var count = c.getAttribute('Count') || 1;
339 var s = c.getAttribute('No')*1;
340 for(var i =0; i < count; i++) {
341 _t.colInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
342 _t.colInfoDom[s+i] = c;
347 ci = this.sheet.getElementsByTagNameNS('*','RowInfo');
350 this.rowInfoDom = {};
351 Roo.each(ci, function(c) {
352 var count = c.getAttribute('Count') || 1;
353 var s = c.getAttribute('No')*1;
354 for(var i =0; i < count; i++) {
355 _t.rowInfoDom[s+i] = c;
356 _t.rowInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
369 * put the style info onto the cell data.
372 overlayStyles : function ()
376 Roo.each(this.styles, function(s) {
378 for (var r = s.r; r < s.r1;r++) {
379 if (typeof(_t.grid[r]) == 'undefined') {
382 for (var c = s.c; c < s.c1;c++) {
387 if (typeof(_t.grid[r][c]) == 'undefined') {
389 //_t.grid[r][c] = Roo.applyIf({ r: r , c : c }, _t.defaultCell);
392 if (typeof(g.cls) =='undefined') {
396 if (g.cls.indexOf(s.name) > -1) {
400 g.styles.push(s.dom);
408 * read the style information
409 * generates a stylesheet for the current file
410 * this should be disposed of really.....
413 parseStyles : function() {
415 var srs = this.sheet.getElementsByTagNameNS('*','StyleRegion');
420 HAlign : function(ent,v) {
421 ent['text-align'] = { '1' : 'left', '8': 'center', '4' : 'right'}[v] || 'left';
423 VAlign : function(ent,v) {
424 ent['vertical-align'] = { '1' : 'top', '4': 'middle', '8' : 'bottom'}[v] || 'top'
426 Fore : function(ent,v) {
428 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
429 ent['color'] = 'rgb(' + col.join(',') + ')';
431 Back : function(ent,v) {
433 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
434 ent['background-color'] = 'rgb(' + col.join(',') + ')';
436 FontUnit : function(ent,v) {
437 ent['font-size'] = v + 'px';
439 FontBold : function(ent,v) {
440 if (v*1 < 1) { return; }
441 ent['font-weight'] = 'bold';
443 FontItalic : function(ent,v) {
444 if (v*0 < 1) { return; }
445 //ent['font-weight'] = 'bold';
447 FontName : function(ent,v) {
448 ent['font-family'] = v;
450 BorderStyle : function(ent,v) {
451 var vv = v.split('-');
452 ent['border-'+vv[0]+'-style'] = 'solid';
453 ent['border-'+vv[0]+'-width'] = vv[1]+'px';
455 BorderColor : function(ent,v) {
456 var vv = v.split('-');
458 Roo.each(vv[1].split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
459 ent['border-'+vv[0]+'-color'] = 'rgb(' + col.join(',') + ')';
462 function add(e, k, v) {
465 if (typeof(map[k]) == 'undefined') {
475 Roo.each(srs, function(sr,n)
478 c : sr.getAttribute('startCol') *1,
479 r : sr.getAttribute('startRow')*1,
480 c1 : (sr.getAttribute('endCol')*1) +1,
481 r1 : (sr.getAttribute('endRow')*1) +1,
482 style : {}, // key val of style for HTML..
483 gstyle : {}, // key val of attributes used..
484 name : sid +'-gstyle-' + n,
489 Roo.each(sr.getElementsByTagNameNS('*','Style')[0].attributes, function(e) {
490 add(ent, e.name, e.value);
492 if (sr.getElementsByTagNameNS('*','Font').length) {
493 Roo.each(sr.getElementsByTagNameNS('*','Font')[0].attributes, function(e) {
494 add(ent, 'Font'+e.name, e.value);
497 add(ent, 'FontName', sr.getElementsByTagNameNS('*','Font')[0].textContent);
500 if (sr.getElementsByTagNameNS('*','StyleBorder').length) {
501 Roo.each(sr.getElementsByTagNameNS('*','StyleBorder')[0].childNodes, function(e) {
505 Roo.each(e.attributes, function(ea) {
506 add(ent, 'Border'+ea.name, e.tagName.split(':')[1].toLowerCase() + '-' + ea.value);
512 css['.'+ent.name] = ent.style;
515 this.styles = styles;
517 this.stylesheetID = sid;
518 Roo.util.CSS.createStyleSheet(css, sid);
524 /* --------------------------------------- AFTER LOAD METHODS... ----------------------- */
527 * Set the value of a cell..
528 * @param {String} cell name of cell, eg. C10 or { c: 1, r :1 }
530 * @param {Value} value to put in cell..
531 * @param {ValueType} type of value
532 * @param {ValueFormat} value format of cell
534 * Cells should exist at present, we do not make them up...
538 set : function(cell, v, vt, vf) {
540 var cs= typeof(cell) == 'string' ? this.toRC(cell) : cell;
543 Roo.log( cs.r+ ',' + cs.c + ' = '+ v);
544 // need to generate clell if it doe
545 if (typeof(this.grid[cs.r]) == 'undefined') {
546 Roo.log('no row:' + cell);
547 this.grid[cs.r] = []; // create a row..
550 if (typeof(this.grid[cs.r][cs.c]) == 'undefined') {
551 Roo.log('cell not defined:' + cell);
552 this.createCell(cs.r,cs.c);
554 // cell might not be rendered yet... so if we try and create a cell, it overrides the default formating..
556 if (typeof(this.grid[cs.r][cs.c].dom) == 'undefined') {
557 Roo.log('no default content for cell:' + cell);
558 Roo.log(this.grid[cs.r][cs.c]);
559 //this.createCell(cs.r,cs.c);
562 this.grid[cs.r][cs.c].value= v;
563 if (this.grid[cs.r][cs.c].dom) {
564 this.grid[cs.r][cs.c].dom.textContent= v;
568 if (typeof(vt) != 'undefined') {
569 this.grid[cs.r][cs.c].valueType = vt;
570 this.grid[cs.r][cs.c].dom.setAttribute('ValueType', vt);
571 if (vt === '' || vt === false) { // value type is empty for formula's
572 this.grid[cs.r][cs.c].dom.removeAttribute('ValueType');
575 if (typeof(vf) != 'undefined' && vf !== false) {
576 this.grid[cs.r][cs.c].valueFormat = vf;
577 this.grid[cs.r][cs.c].dom.setAttribute('ValueFormat', vf);
578 if (vf === '' || vf === false) { // value type is empty for formula's
579 this.grid[cs.r][cs.c].dom.removeAttribute('ValueFormat');
586 copyRow : function(src, dest) {
590 // Roo.log('create Row' + dest);
591 if (typeof(this.grid[dest]) == 'undefined') {
596 for (var c = 0; c < this.cmax; c++) {
598 this.copyCell({ r: src, c: c } , { r: dest, c: c});
601 this.rmax = Math.max(this.rmax, dest +1);
607 createCell: function(r,c)
609 //<gnm:Cell Row="6" Col="5" ValueType="60">Updated</gnm:Cell>
610 var nc = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Cell');
611 this.cellholder.appendChild(nc);
612 var lb = this.doc.createTextNode("\n");// add a line break..
613 this.cellholder.appendChild(lb);
615 nc.setAttribute('Row', new String(r));
616 nc.setAttribute('Col', new String(c));
617 nc.setAttribute('ValueType', '60');
620 this.grid[r][c] = Roo.applyIf({
634 copyCell : function(src, dest)
636 var old = this.grid[src.r][src.c];
638 if ((old.c != src.c) || (old.r != src.r)) {
639 // only really works on horizonatal merges..
641 this.grid[dest.r][dest.c] = this.grid[desc.r][old.c]; // let's hope it exists.
646 var nc = Roo.apply({}, this.grid[src.r][src.c]);
649 if (typeof(old.dom) == 'undefined') {
650 Roo.log("No cell to copy for " + Roo.encode(src));
653 this.grid[dest.r][dest.c] = nc;
654 nc.dom = old.dom.cloneNode(true);
655 nc.dom.setAttribute('Row', dest.r);
656 nc.dom.setAttribute('Cell', dest.c);
657 nc.dom.textContent = '';
658 old.dom.parentNode.appendChild(nc.dom);
659 if (!old.styles || !old.styles.length) {
666 Roo.each(old.styles, function(s) {
667 // try and extend existing styles..
668 var er = s.getAttribute('endRow') * 1;
669 var ec = s.getAttribute('endCol') * 1;
672 s.setAttribute('endRow', dest.r + 1);
675 s.setAttribute('endCol', dest.c + 1);
677 /*var ns = s.cloneNode(true);
678 s.parentNode.appendChild(ns);
679 ns.setAttribute('startCol', dest.c);
680 ns.setAttribute('startRow', dest.r);
681 ns.setAttribute('endCol', dest.c + 1);
682 ns.setAttribute('endRow', dest.r +1);
691 * Set the value of a cell..
692 * @param {String} cell name of cell, eg. C10
693 * @param {Value} value to put in cell..
695 * Cells should exist at present, we do not make them up...
698 applyData : function(data)
701 data = data || this.data;
703 for (var r = 0; r < this.rmax;r++) {
704 if (typeof(this.grid[r]) == 'undefined') {
707 for (var c = 0; c < this.cmax;c++) {
708 if (typeof(this.grid[r][c]) == 'undefined') {
711 if (!this.grid[r][c].value.length
712 || !this.grid[r][c].value.match(/\{/)) {
716 var x = new Roo.Template({ html: this.grid[r][c].value });
718 var res = x.applyTemplate(data);
719 //Roo.log("set " + r + "," + c + ":"+res)
720 this.set({ r: r, c: c}, x.applyTemplate(data));
722 // Roo.log(e.toString());
732 readTableData : function(table)
734 // read the first row.
735 var tds = Roo.get(table).select('tr').item(0).select('td');
738 Roo.get(table).select('tr').each(function(trs) {
741 trs.select('td').each(function(td) {
742 var cs = td.dom.getAttribute('colspan');
743 cs = cs ? cs * 1 : 1;
746 maxnc = Math.max(nc, maxnc);
749 var tr = document.createElement('tr');
750 table.appendChild(tr);
752 for (i =0; i < maxnc; i++) {
753 ar[i] = document.createElement('td');
754 tr.appendChild(ar[i]);
757 var ret = { cols : maxnc, pos : {} };
758 for (i =0; i < maxnc; i++) {
759 ret.pos[ Roo.get(ar[i]).getLeft()] =i;
761 ret.near = function(p) {
762 // which one is nearest..
769 for(var i in this.pos) {
770 var dis = Math.abs(p-i);
779 table.removeChild(tr);
788 * Import a table and put it into the spreadsheet
789 * @param {HTMLTable} datagrid dom element of html table.
790 * @param {Number} xoff X offset to start rendering to
791 * @param {Number} yoff Y offset to start rendering to
795 importTable : function (datagrid, xoff,yoff)
798 Roo.log("Error table not found!?");
805 var table_data = this.readTableData(datagrid);
807 // oroginally this cleaned line breaks, but we acutally need them..
808 var cleanHTML = function (str) {
811 ret = ret.replace(/ /g,' ');
812 // ret = ret.replace(/\n/g,'.');
813 // ret = ret.replace(/\r/g,'.');
820 // <cell col="A" row="1">Test< / cell>
821 // <cell col="B" row="2" type="Number" format="test1">30< / cell>
823 var rows = datagrid.getElementsByTagName('tr');
824 //alert(rows.length);
827 for(var row=0;row<rows.length;row++) {
829 // let's see what affect this has..
830 // it might mess things up..
832 if (rows[row].getAttribute('xls:height')) {
833 this.setRowHeight(row + yoff +1, 1* rows[row].getAttribute('xls:height'));
835 this.setRowHeight( row + yoff +1, Roo.get(rows[row]).getHeight());
839 var cols = rows[row].getElementsByTagName('td');
842 for(var col=0;col < cols.length; col++) {
847 var colspan = cols[col].getAttribute('colspan');
848 colspan = colspan ? colspan *1 : 1;
850 var rowspan = cols[col].getAttribute('rowspan');
851 rowspan = rowspan ? rowspan * 1 : 1;
853 var realcol = table_data.near( Roo.get(cols[col]).getLeft() );
857 if (colspan > 1 || rowspan > 1) {
859 // getting thisese right is tricky..
863 realcol+ xoff + (colspan -1),
870 // set the style first..
871 this.parseHtmlStyle( cols[col], row + yoff, realcol + xoff , colspan, rowspan);
873 if (!cols[col].childNodes.length) {
883 var xlstype = cols[col].getAttribute('xls:type');
891 vt = 40; // float!!!!
892 if (cols[col].getAttribute('xls:floatformat')) {
893 vf = cols[col].getAttribute('xls:floatformat');
899 //ValueFormat="d/m/yyyy" 38635
901 if (cols[col].getAttribute('xls:dateformat')) {
902 vf= cols[col].getAttribute('xls:dateformat');
914 if (!cols[col].childNodes[0].nodeValue) {
918 if (!cols[col].childNodes[0].nodeValue.replace(/^\s*|\s*$/g,"").length) {
923 var cell_value_text = cleanHTML(cols[col].childNodes[0].nodeValue);
925 if (cols[col].getAttribute('xls:percent')) {
926 cell_value_text = '' + ((cell_value_text * 1) / 100);
929 if (cell_value_text.length && (vt == 30) && xlstype == 'date') {
930 var bits = cell_value_text.split(/-/);
931 var cur = new Date(bits[0],bits[1]-1,bits[2]);
932 cell_value_text = '' + Math.round((cur.getTime() - Date.UTC(1899,11,30)) / (24 * 60 * 60 * 1000));
937 if (cols[col].getAttribute('xls:formula')) {
938 var s = cols[col].getAttribute('xls:formula');
940 cell_value_text = s.replace(/#row#/g,(row + yoff + 1));
942 this.set({ r: row + yoff, c : realcol + xoff }, cell_value_text, vt, vf);
950 this.rowOffset += rows.length;
956 parseHtmlStyle : function(dom, row, col, colspan, rowspan) {
958 function toCol (rgb) {
960 var ar = rgb.replace(/rgb[a]?\(/, '').replace(/\)/, '').replace(/ /, '').split(',');
963 Roo.each(ar, function(c) {
964 rcs.push((c*c).toString(16)) ;
966 return rcs.join(':');
970 var el = Roo.get(dom);
972 'text-align' : function(ent,v) {
973 ent['HAlign'] = { 'left' : '1', 'center' : '8' , 'right' : '4' }[v] || '1';
975 'vertical-align': function(ent,v) {
976 ent['VAlign'] = { 'top' : '1', 'middel' : '8' , 'bottom' : '4' }[v] || '1';
979 'color': function(ent,v) {
980 ent['Fore'] = toCol(v);
981 // this is a bit dumb.. we assume that if it's not black text, then it's shaded..
982 if (ent['Fore'] != '0:0:0') {
987 'background-color' : function(ent,v) {
988 ent['Back'] = toCol(v);
1005 Back:"FFFF:FFFF:FFFF",
1006 PatternColor:"0:0:0",
1011 var val = el.getStyle(k);
1012 if (!val || !val.length) {
1018 if (el.dom.getAttribute('xls:wraptext')) {
1021 if (el.dom.getAttribute('xls:valign')) {
1024 if (el.dom.getAttribute('xls:halign')) {
1031 'font-size' : function(ent,v) {
1032 ent['Unit'] = v.replace(/px/, '');
1034 'font-weight' : function(ent,v) {
1040 'font-style' : function(ent,v) {
1041 if (v != 'italic') {
1056 for(var k in fmap) {
1057 var val = el.getStyle(k);
1058 if (!val || !val.length) {
1063 var font = el.getStyle('font-family') || 'Sans';
1064 if (font.split(',').length > 1) {
1065 font = font.split(',')[1].replace(/\s+/, '');
1069 /// -- now create elements..
1071 var objs = this.sheet.getElementsByTagNameNS('*','Styles')[0];
1073 //<gnm:StyleRegion startCol="0" startRow="0" endCol="255" endRow="65535"
1074 var sr = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleRegion');
1075 objs.appendChild(sr);
1076 objs.appendChild(this.doc.createTextNode("\n"));// add a line break..
1078 sr.setAttribute('startCol', col);
1079 sr.setAttribute('endCol', col+ colspan-1);
1080 sr.setAttribute('startRow', row);
1081 sr.setAttribute('endRow', row + rowspan -1);
1084 var st = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Style');
1086 // do we need some defaults..
1089 st.setAttribute(k, ent[k]);
1092 var fo = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Font');
1094 // do we need some defaults..
1095 for(var k in fent) {
1096 fo.setAttribute(k, fent[k]);
1098 fo.textContent = font;
1102 Roo.each(['top','left','bottom','right'], function(p) {
1103 var w = el.getStyle('border-' + p + '-width').replace(/px/, '');
1104 if (!w || !w.length || (w*1) < 1) {
1108 sb= this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleBorder');
1110 var be = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:' + p[0].toUpperCase() + p.substring(1));
1111 be.setAttribute('Style', '1');
1112 be.setAttribute('Color', '0:0:0'); // fixme..
1116 // start adding them all together..
1131 * write an image (needs base64 data to write it)
1134 * @param {Number} row row to put it in (rows start at 0)
1135 * @param {Number} col column to put it in
1136 * @param {Number} data the base64 description of the images
1137 * @param {Number} width image width
1138 * @param {Number} width image height
1143 writeImage : function (row, col, data, width, height)
1147 throw "write Image called with missing data";
1149 // our default height width is 50/50 ?!
1150 //console.log('w='+width+',height='+height);
1156 var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1157 var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1159 //<gmr:SheetObjectImage
1160 // ObjectBound="A3:J8"
1161 // ObjectOffset="0.375 0.882 0.391 0.294"
1162 // ObjectAnchorType="16 16 16 16"
1164 // crop-top="0.000000"
1165 // crop-bottom="0.000000"
1166 // crop-left="0.000000"
1167 // crop-right="0.000000">
1170 //alert(gnumeric_colRowToName(row,col));
1172 // this is where we really have fun!!!...
1173 // since our design currently assumes the height is enough to fit
1174 // stuff in, we only really need to work out how wide it has to be..
1176 // note we should probably use centralized calcs if it fits in the first cell!
1178 // step 1 - work out how many columns it will span..
1179 // lets hope the spreadsheet is big enought..
1182 for ( endcol=col;endcol <100; endcol++) {
1183 if (!this.colInfo[endcol]) {
1184 this.colInfo[endcol] = 100; // eak fudge
1186 colwidth += this.colInfo[endcol];
1187 if (colwidth > width) {
1193 soi.setAttribute('ObjectBound',
1194 //gnumeric_colRowToName(row,col) + ':' + gnumeric_colRowToName(row+1,col+1));
1195 this.RCtoCell(row,col) + ':' + this.RCtoCell(row,endcol));
1197 var ww = 0.01; // offset a bit...
1200 var ww2 = 1 - ((colwidth - width) / this.colInfo[endcol]);
1203 var offset_str = ww + ' ' + hh + ' ' + ww2 + ' '+hh2;
1204 //console.log(offset_str );
1205 //alert(offset_str);
1206 soi.setAttribute('ObjectOffset', offset_str);
1207 soi.setAttribute('ObjectAnchorType','16 16 16 16');
1208 soi.setAttribute('Direction','17');
1209 soi.setAttribute('crop-top','0.000000');
1210 soi.setAttribute('crop-bottom','0.000000');
1211 soi.setAttribute('crop-left','0.000000');
1212 soi.setAttribute('crop-right','0.000000');
1213 // <Content image-type="jpeg" size-bytes="3900">...... < / Content>
1214 var content = this.doc.createElement('Content');
1215 content.setAttribute('image-type','jpeg');
1218 content.setAttribute('size-bytes',data.length);
1219 content.textContent = data;
1220 soi.appendChild(content);
1221 objs.appendChild(soi);
1223 //< /gnm:SheetObjectImage>
1230 * Merge cells in the spreadsheet. (does not check if existing merges exist..)
1232 * @param {Number} col1 first column
1233 * @param {Number} row1 first row
1234 * @param {Number} col2 to column
1235 * @param {Number} row2 to row
1238 mergeRegion : function (col1,row1,col2,row2)
1240 var cell = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Merge');
1241 //if (col1 > 50|| col2 > 50) { // do not merge cols off to right?
1245 cell.textContent = this.RCtoCell(row1,col1) + ':' + this.RCtoCell(row2,col2);
1247 //var merges = this.gnumeric.getElementsByTagNameNS('*','MergedRegions');
1248 var merges = this.sheet.getElementsByTagNameNS('*','MergedRegions');
1249 if (!merges || !merges.length) {
1250 merges = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:MergedRegions');
1251 var sl = this.sheet.getElementsByTagNameNS('*','SheetLayout')[0];
1252 this.sheet.insertBefore(merges,sl);
1256 merges.appendChild(cell);
1261 * Sets the height of a row.
1263 * @param {Number} r the row to set the height of. (rows start at 0)
1264 * @param {Number} height (in pixels)
1266 setRowHeight : function (r,height)
1269 //<gmr:Rows DefaultSizePts="12.75">
1270 // <gmr:RowInfo No="2" Unit="38.25" MarginA="0" MarginB="0" HardSize="1"/>
1273 // this doesnt handle row ranges very well.. - with 'count in them..'
1275 if (this.rowInfoDom[r]) {
1276 this.rowInfoDom[r].setAttribute('Unit', height);
1280 var rows = this.sheet.getElementsByTagNameNS('*','Rows')[0]; // assume this exists..
1281 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:RowInfo');
1282 // assume we have no rows..
1283 ri.setAttribute('No', r-1);
1284 ri.setAttribute('Unit', height);
1285 ri.setAttribute('MarginA', 0);
1286 ri.setAttribute('MarginB', 0);
1287 ri.setAttribute('HardSize', 1);
1288 rows.appendChild(ri);
1289 this.rowInfoDom[r] = ri;
1294 * Set the sheet name.
1295 * @param {String} title for sheet
1297 setSheetName : function(name,sheet)
1301 <gnm:SheetNameIndex>
1302 <gnm:SheetName>Sheet1</gnm:SheetName>
1303 <gnm:SheetName>Sheet2</gnm:SheetName>
1304 <gnm:SheetName>Sheet3</gnm:SheetName>
1305 </gnm:SheetNameIndex>
1307 // has to set sheet name on index and body..
1310 var sheetnames = this.doc.getElementsByTagNameNS('*','SheetName');
1311 if (sheet >= sheetnames.length) {
1313 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1315 sheetnames = this.doc.getElementsByTagNameNS('*','Sheet');
1316 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1317 var sn = this.doc.getElementsByTagNameNS('*','Sheet')[sheet];
1318 var cls = sn.getElementsByTagNameNS('*','Cells')[0];
1319 while (cls.childNodes.length) {
1320 cls.removeChild(cls.firstChild);
1325 var sheetn = this.doc.getElementsByTagNameNS('*','SheetName')[sheet];
1326 sheetn.textContent = name;
1327 var sheetb = this.doc.getElementsByTagNameNS('*','Sheet')[sheet].getElementsByTagNameNS('*','Name')[0];
1328 sheetb.textContent = name;
1329 this.parseDoc(sheet);
1337 * Set the column width
1338 * @param {Number} column number (starts at '0')
1339 * @param {Number} width size of column
1341 setColumnWidth : function(column, width)
1345 if (typeof(this.colInfoDom[column]) == 'undefined') {
1346 var cols = this.doc.getElementsByTagNameNS('*','Cols')[0];
1347 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:ColInfo');
1348 ri.setAttribute('No', column);
1349 ri.setAttribute('Unit', width);
1350 ri.setAttribute('MarginA', 2);
1351 ri.setAttribute('MarginB', 2);
1352 ri.setAttribute('HardSize', 1);
1353 cols.appendChild(ri);
1354 this.colInfo[column] = width;
1355 this.colInfoDom[column] = ri;
1358 this.colInfoDom[column].setAttribute('Unit', width);
1368 * Convert spreadsheet into a HTML table.
1374 function calcWidth(sc, span)
1377 for(var i =sc; i< sc+span;i++) {
1383 var grid = this.grid;
1384 // lets do a basic dump..
1385 var out = '<table style="table-layout:fixed;" cellpadding="0" cellspacing="0">';
1386 for (var r = 0; r < this.rmax;r++) {
1387 out += '<tr style="height:'+this.rowInfo[r]+'px;">';
1388 for (var c = 0; c < this.cmax;c++) {
1389 if (typeof(grid[r][c]) == 'undefined') {
1390 this.createCell(r,c);
1395 if (typeof(g.cls) =='undefined') {
1398 var w= calcWidth(c,g.colspan);
1400 var value = g.value[0] == '=' ? 'CALCULATED' : g.value;
1404 g.styles[0].firstElementChild.getAttribute('Format') == "D\\-MMM\\-YYYY;@" &&
1405 g.value[0] != '=' &&
1406 !isNaN(value * 1) &&
1409 value = new Date(value * 24 * 60 * 60 * 1000 + new Date('1899-12-30').getTime()).format('d-M-Y');
1416 out+=String.format('<td colspan="{0}" rowspan="{1}" class="{4}"><div style="{3}">{2}</div></td>',
1417 g.colspan, g.rowspan, value,
1418 'overflow:hidden;' +
1421 'text-overflow:ellipsis;' +
1422 'white-space:nowrap;',
1432 return out+'</table>';
1439 * @param {String} name filename to downlaod (without xls)
1440 * @param {String} callback (optional) - callback to call after callback is complete.
1442 download : function(name,callback)
1444 name = name || "Missing_download_filename";
1446 if (this.downloadURL && this.downloadURL.charAt(this.downloadURL.length-1) != '/') {
1447 this.downloadURL += '/';
1450 var ser = new XMLSerializer();
1451 var x = new Pman.Download({
1453 timeout : 120000, // quite a long wait.. 2 minutes.
1455 xml : ser.serializeToString(this.doc),
1456 format : 'xls', //xml
1460 url : (this.downloadURL || (baseURL + '/GnumericToExcel/')) + name + '.xls',
1461 success : function() {
1462 Roo.MessageBox.alert("Alert", "File should have downloaded now");