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 _t.grid[rc[0].r][rc[0].c] = Roo.applyIf({ r : rc[0].r, c : rc[0].c }, _t.defaultCell);
319 _t.grid[rc[0].r][rc[0].c].colspan = (rc[1].c - rc[0].c) + 1;
320 _t.grid[rc[0].r][rc[0].c].rowspan = (rc[1].r - rc[0].r) + 1;
321 for(var r = (rc[0].r); r < (rc[1].r+1); r++) {
322 for(var c = rc[0].c; c < (rc[1].c+1); c++) {
323 //Roo.log('adding alias : ' + r+','+c);
324 _t.grid[r][c] = _t.grid[rc[0].r][rc[0].c];
331 var ci = this.sheet.getElementsByTagNameNS('*','ColInfo');
333 this.colInfoDom = {};
335 Roo.each(ci, function(c) {
336 var count = c.getAttribute('Count') || 1;
337 var s = c.getAttribute('No')*1;
338 for(var i =0; i < count; i++) {
339 _t.colInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
340 _t.colInfoDom[s+i] = c;
345 ci = this.sheet.getElementsByTagNameNS('*','RowInfo');
348 this.rowInfoDom = {};
349 Roo.each(ci, function(c) {
350 var count = c.getAttribute('Count') || 1;
351 var s = c.getAttribute('No')*1;
352 for(var i =0; i < count; i++) {
353 _t.rowInfoDom[s+i] = c;
354 _t.rowInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
367 * put the style info onto the cell data.
370 overlayStyles : function ()
374 Roo.each(this.styles, function(s) {
376 for (var r = s.r; r < s.r1;r++) {
377 if (typeof(_t.grid[r]) == 'undefined') {
380 for (var c = s.c; c < s.c1;c++) {
381 if (c > _t.cmax) continue;
383 if (typeof(_t.grid[r][c]) == 'undefined') _t.grid[r][c] = Roo.applyIf({ r: r , c : c }, _t.defaultCell);
385 if (typeof(g.cls) =='undefined') {
389 if (g.cls.indexOf(s.name) > -1) continue;
391 g.styles.push(s.dom);
399 * read the style information
400 * generates a stylesheet for the current file
401 * this should be disposed of really.....
404 parseStyles : function() {
406 var srs = this.sheet.getElementsByTagNameNS('*','StyleRegion');
411 HAlign : function(ent,v) {
412 ent['text-align'] = { '1' : 'left', '8': 'center', '4' : 'right'}[v] || 'left';
414 VAlign : function(ent,v) {
415 ent['vertical-align'] = { '1' : 'top', '4': 'middle', '8' : 'bottom'}[v] || 'top'
417 Fore : function(ent,v) {
419 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
420 ent['color'] = 'rgb(' + col.join(',') + ')';
422 Back : function(ent,v) {
424 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
425 ent['background-color'] = 'rgb(' + col.join(',') + ')';
427 FontUnit : function(ent,v) {
428 ent['font-size'] = v + 'px';
430 FontBold : function(ent,v) {
432 ent['font-weight'] = 'bold';
434 FontItalic : function(ent,v) {
436 //ent['font-weight'] = 'bold';
438 FontName : function(ent,v) {
439 ent['font-family'] = v;
441 BorderStyle : function(ent,v) {
442 var vv = v.split('-');
443 ent['border-'+vv[0]+'-style'] = 'solid';
444 ent['border-'+vv[0]+'-width'] = vv[1]+'px';
446 BorderColor : function(ent,v) {
447 var vv = v.split('-');
449 Roo.each(vv[1].split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
450 ent['border-'+vv[0]+'-color'] = 'rgb(' + col.join(',') + ')';
453 function add(e, k, v) {
456 if (typeof(map[k]) == 'undefined') {
466 Roo.each(srs, function(sr,n)
469 c : sr.getAttribute('startCol') *1,
470 r : sr.getAttribute('startRow')*1,
471 c1 : (sr.getAttribute('endCol')*1) +1,
472 r1 : (sr.getAttribute('endRow')*1) +1,
473 style : {}, // key val of style for HTML..
474 gstyle : {}, // key val of attributes used..
475 name : sid +'-gstyle-' + n,
480 Roo.each(sr.getElementsByTagNameNS('*','Style')[0].attributes, function(e) {
481 add(ent, e.name, e.value);
483 if (sr.getElementsByTagNameNS('*','Font').length) {
484 Roo.each(sr.getElementsByTagNameNS('*','Font')[0].attributes, function(e) {
485 add(ent, 'Font'+e.name, e.value);
488 add(ent, 'FontName', sr.getElementsByTagNameNS('*','Font')[0].textContent);
491 if (sr.getElementsByTagNameNS('*','StyleBorder').length) {
492 Roo.each(sr.getElementsByTagNameNS('*','StyleBorder')[0].childNodes, function(e) {
496 Roo.each(e.attributes, function(ea) {
497 add(ent, 'Border'+ea.name, e.tagName.split(':')[1].toLowerCase() + '-' + ea.value);
503 css['.'+ent.name] = ent.style;
506 this.styles = styles;
508 this.stylesheetID = sid;
509 Roo.util.CSS.createStyleSheet(css, sid);
515 /* --------------------------------------- AFTER LOAD METHODS... ----------------------- */
518 * Set the value of a cell..
519 * @param {String} cell name of cell, eg. C10 or { c: 1, r :1 }
521 * @param {Value} value to put in cell..
522 * @param {ValueType} type of value
523 * @param {ValueFormat} value format of cell
525 * Cells should exist at present, we do not make them up...
529 set : function(cell, v, vt, vf) {
531 var cs= typeof(cell) == 'string' ? this.toRC(cell) : cell;
534 Roo.log( cs.r+ ',' + cs.c + ' = '+ v);
535 // need to generate clell if it doe
536 if (typeof(this.grid[cs.r]) == 'undefined') {
537 Roo.log('no row:' + cell);
538 this.grid[cs.r] = []; // create a row..
541 if (typeof(this.grid[cs.r][cs.c]) == 'undefined') {
542 Roo.log('cell not defined:' + cell);
543 this.createCell(cs.r,cs.c);
545 // cell might not be rendered yet... so if we try and create a cell, it overrides the default formating..
547 if (typeof(this.grid[cs.r][cs.c].dom) == 'undefined') {
548 Roo.log('no default content for cell:' + cell);
549 //this.createCell(cs.r,cs.c);
552 this.grid[cs.r][cs.c].value= v;
553 if (this.grid[cs.r][cs.c].dom) {
554 this.grid[cs.r][cs.c].dom.textContent= v;
558 if (typeof(vt) != 'undefined') {
559 this.grid[cs.r][cs.c].valueType = vt;
560 this.grid[cs.r][cs.c].dom.setAttribute('ValueType', vt);
561 if (vt === '' || vt === false) { // value type is empty for formula's
562 this.grid[cs.r][cs.c].dom.removeAttribute('ValueType');
565 if (typeof(vf) != 'undefined' && vf !== false) {
566 this.grid[cs.r][cs.c].valueFormat = vf;
567 this.grid[cs.r][cs.c].dom.setAttribute('ValueFormat', vf);
568 if (vf === '' || vf === false) { // value type is empty for formula's
569 this.grid[cs.r][cs.c].dom.removeAttribute('ValueFormat');
576 copyRow : function(src, dest) {
580 // Roo.log('create Row' + dest);
581 if (typeof(this.grid[dest]) == 'undefined') {
586 for (var c = 0; c < this.cmax; c++) {
588 this.copyCell({ r: src, c: c } , { r: dest, c: c});
591 this.rmax = Math.max(this.rmax, dest +1);
597 createCell: function(r,c)
599 //<gnm:Cell Row="6" Col="5" ValueType="60">Updated</gnm:Cell>
600 var nc = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Cell');
601 this.cellholder.appendChild(nc);
602 var lb = this.doc.createTextNode("\n");// add a line break..
603 this.cellholder.appendChild(lb);
605 nc.setAttribute('Row', new String(r));
606 nc.setAttribute('Col', new String(c));
607 nc.setAttribute('ValueType', '60');
610 this.grid[r][c] = Roo.applyIf({
624 copyCell : function(src, dest)
626 var old = this.grid[src.r][src.c];
628 if ((old.c != src.c) || (old.r != src.r)) {
629 // only really works on horizonatal merges..
631 this.grid[dest.r][dest.c] = this.grid[desc.r][old.c]; // let's hope it exists.
636 var nc = Roo.apply({}, this.grid[src.r][src.c]);
639 if (typeof(old.dom) == 'undefined') {
640 Roo.log("No cell to copy for " + Roo.encode(src));
643 this.grid[dest.r][dest.c] = nc;
644 nc.dom = old.dom.cloneNode(true);
645 nc.dom.setAttribute('Row', dest.r);
646 nc.dom.setAttribute('Cell', dest.c);
647 nc.dom.textContent = '';
648 old.dom.parentNode.appendChild(nc.dom);
649 if (!old.styles || !old.styles.length) {
656 Roo.each(old.styles, function(s) {
657 // try and extend existing styles..
658 var er = s.getAttribute('endRow') * 1;
659 var ec = s.getAttribute('endCol') * 1;
662 s.setAttribute('endRow', dest.r + 1);
665 s.setAttribute('endCol', dest.c + 1);
667 /*var ns = s.cloneNode(true);
668 s.parentNode.appendChild(ns);
669 ns.setAttribute('startCol', dest.c);
670 ns.setAttribute('startRow', dest.r);
671 ns.setAttribute('endCol', dest.c + 1);
672 ns.setAttribute('endRow', dest.r +1);
681 * Set the value of a cell..
682 * @param {String} cell name of cell, eg. C10
683 * @param {Value} value to put in cell..
685 * Cells should exist at present, we do not make them up...
688 applyData : function(data)
691 data = data || this.data;
692 for (var r = 0; r < this.rmax;r++) {
693 if (typeof(this.grid[r]) == 'undefined') continue;
694 for (var c = 0; c < this.cmax;c++) {
695 if (typeof(this.grid[r][c]) == 'undefined') {
698 if (!this.grid[r][c].value.length
699 || !this.grid[r][c].value.match(/\{/)) {
703 var x = new Roo.Template({ html: this.grid[r][c].value });
705 var res = x.applyTemplate(data);
706 //Roo.log("set " + r + "," + c + ":"+res)
707 this.set({ r: r, c: c}, x.applyTemplate(data));
709 // Roo.log(e.toString());
719 readTableData : function(table)
721 // read the first row.
722 var tds = Roo.get(table).select('tr').item(0).select('td');
725 Roo.get(table).select('tr').each(function(trs) {
728 trs.select('td').each(function(td) {
729 var cs = td.dom.getAttribute('colspan');
730 cs = cs ? cs * 1 : 1;
733 maxnc = Math.max(nc, maxnc);
736 var tr = document.createElement('tr');
737 table.appendChild(tr);
739 for (i =0; i < maxnc; i++) {
740 ar[i] = document.createElement('td');
741 tr.appendChild(ar[i]);
744 var ret = { cols : maxnc, pos : {} };
745 for (i =0; i < maxnc; i++) {
746 ret.pos[ Roo.get(ar[i]).getLeft()] =i;
748 ret.near = function(p) {
749 // which one is nearest..
756 for(var i in this.pos) {
757 var dis = Math.abs(p-i);
766 table.removeChild(tr);
775 * Import a table and put it into the spreadsheet
776 * @param {HTMLTable} datagrid dom element of html table.
777 * @param {Number} xoff X offset to start rendering to
778 * @param {Number} yoff Y offset to start rendering to
782 importTable : function (datagrid, xoff,yoff)
785 Roo.log("Error table not found!?");
792 var table_data = this.readTableData(datagrid);
794 // oroginally this cleaned line breaks, but we acutally need them..
795 var cleanHTML = function (str) {
798 ret = ret.replace(/ /g,' ');
799 // ret = ret.replace(/\n/g,'.');
800 // ret = ret.replace(/\r/g,'.');
807 // <cell col="A" row="1">Test< / cell>
808 // <cell col="B" row="2" type="Number" format="test1">30< / cell>
810 var rows = datagrid.getElementsByTagName('tr');
811 //alert(rows.length);
814 for(var row=0;row<rows.length;row++) {
816 // let's see what affect this has..
817 // it might mess things up..
819 if (rows[row].getAttribute('xls:height')) {
820 this.setRowHeight(row + yoff +1, 1* rows[row].getAttribute('xls:height'));
822 this.setRowHeight( row + yoff +1, Roo.get(rows[row]).getHeight());
826 var cols = rows[row].getElementsByTagName('td');
829 for(var col=0;col < cols.length; col++) {
834 var colspan = cols[col].getAttribute('colspan');
835 colspan = colspan ? colspan *1 : 1;
837 var rowspan = cols[col].getAttribute('rowspan');
838 rowspan = rowspan ? rowspan * 1 : 1;
840 var realcol = table_data.near( Roo.get(cols[col]).getLeft() );
844 if (colspan > 1 || rowspan > 1) {
846 // getting thisese right is tricky..
850 realcol+ xoff + (colspan -1),
857 // set the style first..
858 this.parseHtmlStyle( cols[col], row + yoff, realcol + xoff , colspan, rowspan);
860 if (!cols[col].childNodes.length) {
870 var xlstype = cols[col].getAttribute('xls:type');
878 vt = 40; // float!!!!
879 if (cols[col].getAttribute('xls:floatformat')) {
880 vf = cols[col].getAttribute('xls:floatformat');
886 //ValueFormat="d/m/yyyy" 38635
888 if (cols[col].getAttribute('xls:dateformat')) {
889 vf= cols[col].getAttribute('xls:dateformat');
901 if (!cols[col].childNodes[0].nodeValue) {
905 if (!cols[col].childNodes[0].nodeValue.replace(/^\s*|\s*$/g,"").length) {
910 var cell_value_text = cleanHTML(cols[col].childNodes[0].nodeValue);
912 if (cols[col].getAttribute('xls:percent')) {
913 cell_value_text = '' + ((cell_value_text * 1) / 100);
916 if (cell_value_text.length && (vt == 30) && xlstype == 'date') {
917 var bits = cell_value_text.split(/-/);
918 var cur = new Date(bits[0],bits[1]-1,bits[2]);
919 cell_value_text = '' + Math.round((cur.getTime() - Date.UTC(1899,11,30)) / (24 * 60 * 60 * 1000));
924 if (cols[col].getAttribute('xls:formula')) {
925 var s = cols[col].getAttribute('xls:formula');
927 cell_value_text = s.replace(/#row#/g,(row + yoff + 1));
929 this.set({ r: row + yoff, c : realcol + xoff }, cell_value_text, vt, vf);
937 this.rowOffset += rows.length;
943 parseHtmlStyle : function(dom, row, col, colspan, rowspan) {
945 function toCol (rgb) {
947 var ar = rgb.replace(/rgb[a]?\(/, '').replace(/\)/, '').replace(/ /, '').split(',');
950 Roo.each(ar, function(c) {
951 rcs.push((c*c).toString(16)) ;
953 return rcs.join(':');
957 var el = Roo.get(dom);
959 'text-align' : function(ent,v) {
960 ent['HAlign'] = { 'left' : '1', 'center' : '8' , 'right' : '4' }[v] || '1';
962 'vertical-align': function(ent,v) {
963 ent['VAlign'] = { 'top' : '1', 'middel' : '8' , 'bottom' : '4' }[v] || '1';
966 'color': function(ent,v) {
967 ent['Fore'] = toCol(v);
968 // this is a bit dumb.. we assume that if it's not black text, then it's shaded..
969 if (ent['Fore'] != '0:0:0') {
974 'background-color' : function(ent,v) {
975 ent['Back'] = toCol(v);
992 Back:"FFFF:FFFF:FFFF",
993 PatternColor:"0:0:0",
998 var val = el.getStyle(k);
999 if (!val || !val.length) {
1005 if (el.dom.getAttribute('xls:wraptext')) {
1008 if (el.dom.getAttribute('xls:valign')) {
1011 if (el.dom.getAttribute('xls:halign')) {
1018 'font-size' : function(ent,v) {
1019 ent['Unit'] = v.replace(/px/, '');
1021 'font-weight' : function(ent,v) {
1022 if (v != 'bold') return;
1025 'font-style' : function(ent,v) {
1026 if (v != 'italic') return;
1039 for(var k in fmap) {
1040 var val = el.getStyle(k);
1041 if (!val || !val.length) {
1046 var font = el.getStyle('font-family') || 'Sans';
1047 if (font.split(',').length > 1) {
1048 font = font.split(',')[1].replace(/\s+/, '');
1052 /// -- now create elements..
1054 var objs = this.sheet.getElementsByTagNameNS('*','Styles')[0];
1056 //<gnm:StyleRegion startCol="0" startRow="0" endCol="255" endRow="65535"
1057 var sr = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleRegion');
1058 objs.appendChild(sr);
1059 objs.appendChild(this.doc.createTextNode("\n"));// add a line break..
1061 sr.setAttribute('startCol', col);
1062 sr.setAttribute('endCol', col+ colspan-1);
1063 sr.setAttribute('startRow', row);
1064 sr.setAttribute('endRow', row + rowspan -1);
1067 var st = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Style');
1069 // do we need some defaults..
1072 st.setAttribute(k, ent[k]);
1075 var fo = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Font');
1077 // do we need some defaults..
1078 for(var k in fent) {
1079 fo.setAttribute(k, fent[k]);
1081 fo.textContent = font;
1085 Roo.each(['top','left','bottom','right'], function(p) {
1086 var w = el.getStyle('border-' + p + '-width').replace(/px/, '');
1087 if (!w || !w.length || (w*1) < 1) {
1091 sb= this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleBorder');
1093 var be = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:' + p[0].toUpperCase() + p.substring(1));
1094 be.setAttribute('Style', '1');
1095 be.setAttribute('Color', '0:0:0'); // fixme..
1099 // start adding them all together..
1114 * write an image (needs base64 data to write it)
1117 * @param {Number} row row to put it in (rows start at 0)
1118 * @param {Number} col column to put it in
1119 * @param {Number} data the base64 description of the images
1120 * @param {Number} width image width
1121 * @param {Number} width image height
1126 writeImage : function (row, col, data, width, height)
1129 // our default height width is 50/50 ?!
1130 //console.log('w='+width+',height='+height);
1136 var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1137 var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1139 //<gmr:SheetObjectImage
1140 // ObjectBound="A3:J8"
1141 // ObjectOffset="0.375 0.882 0.391 0.294"
1142 // ObjectAnchorType="16 16 16 16"
1144 // crop-top="0.000000"
1145 // crop-bottom="0.000000"
1146 // crop-left="0.000000"
1147 // crop-right="0.000000">
1150 //alert(gnumeric_colRowToName(row,col));
1152 // this is where we really have fun!!!...
1153 // since our design currently assumes the height is enough to fit
1154 // stuff in, we only really need to work out how wide it has to be..
1156 // note we should probably use centralized calcs if it fits in the first cell!
1158 // step 1 - work out how many columns it will span..
1159 // lets hope the spreadsheet is big enought..
1162 for ( endcol=col;endcol <100; endcol++) {
1163 if (!this.colInfo[endcol]) {
1164 this.colInfo[endcol] = 100; // eak fudge
1166 colwidth += this.colInfo[endcol];
1167 if (colwidth > width) {
1173 soi.setAttribute('ObjectBound',
1174 //gnumeric_colRowToName(row,col) + ':' + gnumeric_colRowToName(row+1,col+1));
1175 this.RCtoCell(row,col) + ':' + this.RCtoCell(row,endcol));
1177 var ww = 0.01; // offset a bit...
1180 var ww2 = 1 - ((colwidth - width) / this.colInfo[endcol]);
1183 var offset_str = ww + ' ' + hh + ' ' + ww2 + ' '+hh2;
1184 //console.log(offset_str );
1185 //alert(offset_str);
1186 soi.setAttribute('ObjectOffset', offset_str);
1187 soi.setAttribute('ObjectAnchorType','16 16 16 16');
1188 soi.setAttribute('Direction','17');
1189 soi.setAttribute('crop-top','0.000000');
1190 soi.setAttribute('crop-bottom','0.000000');
1191 soi.setAttribute('crop-left','0.000000');
1192 soi.setAttribute('crop-right','0.000000');
1193 // <Content image-type="jpeg" size-bytes="3900">...... < / Content>
1194 var content = this.doc.createElement('Content');
1195 content.setAttribute('image-type','jpeg');
1198 content.setAttribute('size-bytes',data.length);
1199 content.textContent = data;
1200 soi.appendChild(content);
1201 objs.appendChild(soi);
1203 //< /gnm:SheetObjectImage>
1210 * Merge cells in the spreadsheet. (does not check if existing merges exist..)
1212 * @param {Number} col1 first column
1213 * @param {Number} row1 first row
1214 * @param {Number} col2 to column
1215 * @param {Number} row2 to row
1218 mergeRegion : function (col1,row1,col2,row2)
1220 var cell = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Merge');
1221 //if (col1 > 50|| col2 > 50) { // do not merge cols off to right?
1225 cell.textContent = this.RCtoCell(row1,col1) + ':' + this.RCtoCell(row2,col2);
1227 //var merges = this.gnumeric.getElementsByTagNameNS('*','MergedRegions');
1228 var merges = this.sheet.getElementsByTagNameNS('*','MergedRegions');
1229 if (!merges || !merges.length) {
1230 merges = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:MergedRegions');
1231 var sl = this.sheet.getElementsByTagNameNS('*','SheetLayout')[0];
1232 this.sheet.insertBefore(merges,sl);
1236 merges.appendChild(cell);
1241 * Sets the height of a row.
1243 * @param {Number} r the row to set the height of. (rows start at 0)
1244 * @param {Number} height (in pixels)
1246 setRowHeight : function (r,height)
1249 //<gmr:Rows DefaultSizePts="12.75">
1250 // <gmr:RowInfo No="2" Unit="38.25" MarginA="0" MarginB="0" HardSize="1"/>
1253 // this doesnt handle row ranges very well.. - with 'count in them..'
1255 if (this.rowInfoDom[r]) {
1256 this.rowInfoDom[r].setAttribute('Unit', height);
1260 var rows = this.sheet.getElementsByTagNameNS('*','Rows')[0]; // assume this exists..
1261 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:RowInfo');
1262 // assume we have no rows..
1263 ri.setAttribute('No', r-1);
1264 ri.setAttribute('Unit', height);
1265 ri.setAttribute('MarginA', 0);
1266 ri.setAttribute('MarginB', 0);
1267 ri.setAttribute('HardSize', 1);
1268 rows.appendChild(ri);
1269 this.rowInfoDom[r] = ri;
1274 * Set the sheet name.
1275 * @param {String} title for sheet
1277 setSheetName : function(name,sheet)
1281 <gnm:SheetNameIndex>
1282 <gnm:SheetName>Sheet1</gnm:SheetName>
1283 <gnm:SheetName>Sheet2</gnm:SheetName>
1284 <gnm:SheetName>Sheet3</gnm:SheetName>
1285 </gnm:SheetNameIndex>
1287 // has to set sheet name on index and body..
1290 var sheetnames = this.doc.getElementsByTagNameNS('*','SheetName');
1291 if (sheet >= sheetnames.length) {
1293 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1295 sheetnames = this.doc.getElementsByTagNameNS('*','Sheet');
1296 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1297 var sn = this.doc.getElementsByTagNameNS('*','Sheet')[sheet];
1298 var cls = sn.getElementsByTagNameNS('*','Cells')[0];
1299 while (cls.childNodes.length) {
1300 cls.removeChild(cls.firstChild);
1305 var sheetn = this.doc.getElementsByTagNameNS('*','SheetName')[sheet];
1306 sheetn.textContent = name;
1307 var sheetb = this.doc.getElementsByTagNameNS('*','Sheet')[sheet].getElementsByTagNameNS('*','Name')[0];
1308 sheetb.textContent = name;
1309 this.parseDoc(sheet);
1317 * Set the column width
1318 * @param {Number} column number (starts at '0')
1319 * @param {Number} width size of column
1321 setColumnWidth : function(column, width)
1325 if (typeof(this.colInfoDom[column]) == 'undefined') {
1326 var cols = this.doc.getElementsByTagNameNS('*','Cols')[0];
1327 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:ColInfo');
1328 ri.setAttribute('No', column);
1329 ri.setAttribute('Unit', width);
1330 ri.setAttribute('MarginA', 2);
1331 ri.setAttribute('MarginB', 2);
1332 ri.setAttribute('HardSize', 1);
1333 cols.appendChild(ri);
1334 this.colInfo[column] = width;
1335 this.colInfoDom[column] = ri;
1338 this.colInfoDom[column].setAttribute('Unit', width);
1348 * Convert spreadsheet into a HTML table.
1354 function calcWidth(sc, span)
1357 for(var i =sc; i< sc+span;i++) {
1363 var grid = this.grid;
1364 // lets do a basic dump..
1365 var out = '<table style="table-layout:fixed;" cellpadding="0" cellspacing="0">';
1366 for (var r = 0; r < this.rmax;r++) {
1367 out += '<tr style="height:'+this.rowInfo[r]+'px;">';
1368 for (var c = 0; c < this.cmax;c++) {
1369 var g = (typeof(grid[r][c]) == 'undefined') ? this.defaultCell : grid[r][c];
1371 if (typeof(g.cls) =='undefined') g.cls = [];
1372 var w= calcWidth(c,g.colspan);
1373 out+=String.format('<td colspan="{0}" rowspan="{1}" class="{4}"><div style="{3}">{2}</div></td>',
1374 g.colspan, g.rowspan, g.value,
1375 'overflow:hidden;' +
1378 'text-overflow:ellipsis;' +
1379 'white-space:nowrap;',
1389 return out+'</table>';
1396 * @param {String} name filename to downlaod (without xls)
1397 * @param {String} callback (optional) - callback to call after callback is complete.
1399 download : function(name,callback)
1401 name = name || "Missing_download_filename";
1403 if (this.downloadURL && this.downloadURL.charAt(this.downloadURL .length-1) != '/') {
1404 this.downloadURL += '/';
1407 var ser = new XMLSerializer();
1408 var x = new Pman.Download({
1410 timeout : 120000, // quite a long wait.. 2 minutes.
1412 xml : ser.serializeToString(this.doc),
1413 format : 'xls', //xml
1417 url : (this.downloadURL || (baseURL + '/GnumericToExcel/')) + name + '.xls',
1418 success : function() {
1419 Roo.MessageBox.alert("Alert", "File should have downloaded now");