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++) {
383 if (c > _t.cmax) continue;
385 if (typeof(_t.grid[r][c]) == 'undefined') {
387 //_t.grid[r][c] = Roo.applyIf({ r: r , c : c }, _t.defaultCell);
390 if (typeof(g.cls) =='undefined') {
394 if (g.cls.indexOf(s.name) > -1) continue;
396 g.styles.push(s.dom);
404 * read the style information
405 * generates a stylesheet for the current file
406 * this should be disposed of really.....
409 parseStyles : function() {
411 var srs = this.sheet.getElementsByTagNameNS('*','StyleRegion');
416 HAlign : function(ent,v) {
417 ent['text-align'] = { '1' : 'left', '8': 'center', '4' : 'right'}[v] || 'left';
419 VAlign : function(ent,v) {
420 ent['vertical-align'] = { '1' : 'top', '4': 'middle', '8' : 'bottom'}[v] || 'top'
422 Fore : function(ent,v) {
424 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
425 ent['color'] = 'rgb(' + col.join(',') + ')';
427 Back : function(ent,v) {
429 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
430 ent['background-color'] = 'rgb(' + col.join(',') + ')';
432 FontUnit : function(ent,v) {
433 ent['font-size'] = v + 'px';
435 FontBold : function(ent,v) {
437 ent['font-weight'] = 'bold';
439 FontItalic : function(ent,v) {
441 //ent['font-weight'] = 'bold';
443 FontName : function(ent,v) {
444 ent['font-family'] = v;
446 BorderStyle : function(ent,v) {
447 var vv = v.split('-');
448 ent['border-'+vv[0]+'-style'] = 'solid';
449 ent['border-'+vv[0]+'-width'] = vv[1]+'px';
451 BorderColor : function(ent,v) {
452 var vv = v.split('-');
454 Roo.each(vv[1].split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
455 ent['border-'+vv[0]+'-color'] = 'rgb(' + col.join(',') + ')';
458 function add(e, k, v) {
461 if (typeof(map[k]) == 'undefined') {
471 Roo.each(srs, function(sr,n)
474 c : sr.getAttribute('startCol') *1,
475 r : sr.getAttribute('startRow')*1,
476 c1 : (sr.getAttribute('endCol')*1) +1,
477 r1 : (sr.getAttribute('endRow')*1) +1,
478 style : {}, // key val of style for HTML..
479 gstyle : {}, // key val of attributes used..
480 name : sid +'-gstyle-' + n,
485 Roo.each(sr.getElementsByTagNameNS('*','Style')[0].attributes, function(e) {
486 add(ent, e.name, e.value);
488 if (sr.getElementsByTagNameNS('*','Font').length) {
489 Roo.each(sr.getElementsByTagNameNS('*','Font')[0].attributes, function(e) {
490 add(ent, 'Font'+e.name, e.value);
493 add(ent, 'FontName', sr.getElementsByTagNameNS('*','Font')[0].textContent);
496 if (sr.getElementsByTagNameNS('*','StyleBorder').length) {
497 Roo.each(sr.getElementsByTagNameNS('*','StyleBorder')[0].childNodes, function(e) {
501 Roo.each(e.attributes, function(ea) {
502 add(ent, 'Border'+ea.name, e.tagName.split(':')[1].toLowerCase() + '-' + ea.value);
508 css['.'+ent.name] = ent.style;
511 this.styles = styles;
513 this.stylesheetID = sid;
514 Roo.util.CSS.createStyleSheet(css, sid);
520 /* --------------------------------------- AFTER LOAD METHODS... ----------------------- */
523 * Set the value of a cell..
524 * @param {String} cell name of cell, eg. C10 or { c: 1, r :1 }
526 * @param {Value} value to put in cell..
527 * @param {ValueType} type of value
528 * @param {ValueFormat} value format of cell
530 * Cells should exist at present, we do not make them up...
534 set : function(cell, v, vt, vf) {
536 var cs= typeof(cell) == 'string' ? this.toRC(cell) : cell;
539 Roo.log( cs.r+ ',' + cs.c + ' = '+ v);
540 // need to generate clell if it doe
541 if (typeof(this.grid[cs.r]) == 'undefined') {
542 Roo.log('no row:' + cell);
543 this.grid[cs.r] = []; // create a row..
546 if (typeof(this.grid[cs.r][cs.c]) == 'undefined') {
547 Roo.log('cell not defined:' + cell);
548 this.createCell(cs.r,cs.c);
550 // cell might not be rendered yet... so if we try and create a cell, it overrides the default formating..
552 if (typeof(this.grid[cs.r][cs.c].dom) == 'undefined') {
553 Roo.log('no default content for cell:' + cell);
554 Roo.log(this.grid[cs.r][cs.c]);
555 //this.createCell(cs.r,cs.c);
558 this.grid[cs.r][cs.c].value= v;
559 if (this.grid[cs.r][cs.c].dom) {
560 this.grid[cs.r][cs.c].dom.textContent= v;
564 if (typeof(vt) != 'undefined') {
565 this.grid[cs.r][cs.c].valueType = vt;
566 this.grid[cs.r][cs.c].dom.setAttribute('ValueType', vt);
567 if (vt === '' || vt === false) { // value type is empty for formula's
568 this.grid[cs.r][cs.c].dom.removeAttribute('ValueType');
571 if (typeof(vf) != 'undefined' && vf !== false) {
572 this.grid[cs.r][cs.c].valueFormat = vf;
573 this.grid[cs.r][cs.c].dom.setAttribute('ValueFormat', vf);
574 if (vf === '' || vf === false) { // value type is empty for formula's
575 this.grid[cs.r][cs.c].dom.removeAttribute('ValueFormat');
582 copyRow : function(src, dest) {
586 // Roo.log('create Row' + dest);
587 if (typeof(this.grid[dest]) == 'undefined') {
592 for (var c = 0; c < this.cmax; c++) {
594 this.copyCell({ r: src, c: c } , { r: dest, c: c});
597 this.rmax = Math.max(this.rmax, dest +1);
603 createCell: function(r,c)
605 //<gnm:Cell Row="6" Col="5" ValueType="60">Updated</gnm:Cell>
606 var nc = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Cell');
607 this.cellholder.appendChild(nc);
608 var lb = this.doc.createTextNode("\n");// add a line break..
609 this.cellholder.appendChild(lb);
611 nc.setAttribute('Row', new String(r));
612 nc.setAttribute('Col', new String(c));
613 nc.setAttribute('ValueType', '60');
616 this.grid[r][c] = Roo.applyIf({
630 copyCell : function(src, dest)
632 var old = this.grid[src.r][src.c];
634 if ((old.c != src.c) || (old.r != src.r)) {
635 // only really works on horizonatal merges..
637 this.grid[dest.r][dest.c] = this.grid[desc.r][old.c]; // let's hope it exists.
642 var nc = Roo.apply({}, this.grid[src.r][src.c]);
645 if (typeof(old.dom) == 'undefined') {
646 Roo.log("No cell to copy for " + Roo.encode(src));
649 this.grid[dest.r][dest.c] = nc;
650 nc.dom = old.dom.cloneNode(true);
651 nc.dom.setAttribute('Row', dest.r);
652 nc.dom.setAttribute('Cell', dest.c);
653 nc.dom.textContent = '';
654 old.dom.parentNode.appendChild(nc.dom);
655 if (!old.styles || !old.styles.length) {
662 Roo.each(old.styles, function(s) {
663 // try and extend existing styles..
664 var er = s.getAttribute('endRow') * 1;
665 var ec = s.getAttribute('endCol') * 1;
668 s.setAttribute('endRow', dest.r + 1);
671 s.setAttribute('endCol', dest.c + 1);
673 /*var ns = s.cloneNode(true);
674 s.parentNode.appendChild(ns);
675 ns.setAttribute('startCol', dest.c);
676 ns.setAttribute('startRow', dest.r);
677 ns.setAttribute('endCol', dest.c + 1);
678 ns.setAttribute('endRow', dest.r +1);
687 * Set the value of a cell..
688 * @param {String} cell name of cell, eg. C10
689 * @param {Value} value to put in cell..
691 * Cells should exist at present, we do not make them up...
694 applyData : function(data)
697 data = data || this.data;
698 for (var r = 0; r < this.rmax;r++) {
699 if (typeof(this.grid[r]) == 'undefined') continue;
700 for (var c = 0; c < this.cmax;c++) {
701 if (typeof(this.grid[r][c]) == 'undefined') {
704 if (!this.grid[r][c].value.length
705 || !this.grid[r][c].value.match(/\{/)) {
709 var x = new Roo.Template({ html: this.grid[r][c].value });
711 var res = x.applyTemplate(data);
712 //Roo.log("set " + r + "," + c + ":"+res)
713 this.set({ r: r, c: c}, x.applyTemplate(data));
715 // Roo.log(e.toString());
725 readTableData : function(table)
727 // read the first row.
728 var tds = Roo.get(table).select('tr').item(0).select('td');
731 Roo.get(table).select('tr').each(function(trs) {
734 trs.select('td').each(function(td) {
735 var cs = td.dom.getAttribute('colspan');
736 cs = cs ? cs * 1 : 1;
739 maxnc = Math.max(nc, maxnc);
742 var tr = document.createElement('tr');
743 table.appendChild(tr);
745 for (i =0; i < maxnc; i++) {
746 ar[i] = document.createElement('td');
747 tr.appendChild(ar[i]);
750 var ret = { cols : maxnc, pos : {} };
751 for (i =0; i < maxnc; i++) {
752 ret.pos[ Roo.get(ar[i]).getLeft()] =i;
754 ret.near = function(p) {
755 // which one is nearest..
762 for(var i in this.pos) {
763 var dis = Math.abs(p-i);
772 table.removeChild(tr);
781 * Import a table and put it into the spreadsheet
782 * @param {HTMLTable} datagrid dom element of html table.
783 * @param {Number} xoff X offset to start rendering to
784 * @param {Number} yoff Y offset to start rendering to
788 importTable : function (datagrid, xoff,yoff)
791 Roo.log("Error table not found!?");
798 var table_data = this.readTableData(datagrid);
800 // oroginally this cleaned line breaks, but we acutally need them..
801 var cleanHTML = function (str) {
804 ret = ret.replace(/ /g,' ');
805 // ret = ret.replace(/\n/g,'.');
806 // ret = ret.replace(/\r/g,'.');
813 // <cell col="A" row="1">Test< / cell>
814 // <cell col="B" row="2" type="Number" format="test1">30< / cell>
816 var rows = datagrid.getElementsByTagName('tr');
817 //alert(rows.length);
820 for(var row=0;row<rows.length;row++) {
822 // let's see what affect this has..
823 // it might mess things up..
825 if (rows[row].getAttribute('xls:height')) {
826 this.setRowHeight(row + yoff +1, 1* rows[row].getAttribute('xls:height'));
828 this.setRowHeight( row + yoff +1, Roo.get(rows[row]).getHeight());
832 var cols = rows[row].getElementsByTagName('td');
835 for(var col=0;col < cols.length; col++) {
840 var colspan = cols[col].getAttribute('colspan');
841 colspan = colspan ? colspan *1 : 1;
843 var rowspan = cols[col].getAttribute('rowspan');
844 rowspan = rowspan ? rowspan * 1 : 1;
846 var realcol = table_data.near( Roo.get(cols[col]).getLeft() );
850 if (colspan > 1 || rowspan > 1) {
852 // getting thisese right is tricky..
856 realcol+ xoff + (colspan -1),
863 // set the style first..
864 this.parseHtmlStyle( cols[col], row + yoff, realcol + xoff , colspan, rowspan);
866 if (!cols[col].childNodes.length) {
876 var xlstype = cols[col].getAttribute('xls:type');
884 vt = 40; // float!!!!
885 if (cols[col].getAttribute('xls:floatformat')) {
886 vf = cols[col].getAttribute('xls:floatformat');
892 //ValueFormat="d/m/yyyy" 38635
894 if (cols[col].getAttribute('xls:dateformat')) {
895 vf= cols[col].getAttribute('xls:dateformat');
907 if (!cols[col].childNodes[0].nodeValue) {
911 if (!cols[col].childNodes[0].nodeValue.replace(/^\s*|\s*$/g,"").length) {
916 var cell_value_text = cleanHTML(cols[col].childNodes[0].nodeValue);
918 if (cols[col].getAttribute('xls:percent')) {
919 cell_value_text = '' + ((cell_value_text * 1) / 100);
922 if (cell_value_text.length && (vt == 30) && xlstype == 'date') {
923 var bits = cell_value_text.split(/-/);
924 var cur = new Date(bits[0],bits[1]-1,bits[2]);
925 cell_value_text = '' + Math.round((cur.getTime() - Date.UTC(1899,11,30)) / (24 * 60 * 60 * 1000));
930 if (cols[col].getAttribute('xls:formula')) {
931 var s = cols[col].getAttribute('xls:formula');
933 cell_value_text = s.replace(/#row#/g,(row + yoff + 1));
935 this.set({ r: row + yoff, c : realcol + xoff }, cell_value_text, vt, vf);
943 this.rowOffset += rows.length;
949 parseHtmlStyle : function(dom, row, col, colspan, rowspan) {
951 function toCol (rgb) {
953 var ar = rgb.replace(/rgb[a]?\(/, '').replace(/\)/, '').replace(/ /, '').split(',');
956 Roo.each(ar, function(c) {
957 rcs.push((c*c).toString(16)) ;
959 return rcs.join(':');
963 var el = Roo.get(dom);
965 'text-align' : function(ent,v) {
966 ent['HAlign'] = { 'left' : '1', 'center' : '8' , 'right' : '4' }[v] || '1';
968 'vertical-align': function(ent,v) {
969 ent['VAlign'] = { 'top' : '1', 'middel' : '8' , 'bottom' : '4' }[v] || '1';
972 'color': function(ent,v) {
973 ent['Fore'] = toCol(v);
974 // this is a bit dumb.. we assume that if it's not black text, then it's shaded..
975 if (ent['Fore'] != '0:0:0') {
980 'background-color' : function(ent,v) {
981 ent['Back'] = toCol(v);
998 Back:"FFFF:FFFF:FFFF",
999 PatternColor:"0:0:0",
1004 var val = el.getStyle(k);
1005 if (!val || !val.length) {
1011 if (el.dom.getAttribute('xls:wraptext')) {
1014 if (el.dom.getAttribute('xls:valign')) {
1017 if (el.dom.getAttribute('xls:halign')) {
1024 'font-size' : function(ent,v) {
1025 ent['Unit'] = v.replace(/px/, '');
1027 'font-weight' : function(ent,v) {
1028 if (v != 'bold') return;
1031 'font-style' : function(ent,v) {
1032 if (v != 'italic') return;
1045 for(var k in fmap) {
1046 var val = el.getStyle(k);
1047 if (!val || !val.length) {
1052 var font = el.getStyle('font-family') || 'Sans';
1053 if (font.split(',').length > 1) {
1054 font = font.split(',')[1].replace(/\s+/, '');
1058 /// -- now create elements..
1060 var objs = this.sheet.getElementsByTagNameNS('*','Styles')[0];
1062 //<gnm:StyleRegion startCol="0" startRow="0" endCol="255" endRow="65535"
1063 var sr = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleRegion');
1064 objs.appendChild(sr);
1065 objs.appendChild(this.doc.createTextNode("\n"));// add a line break..
1067 sr.setAttribute('startCol', col);
1068 sr.setAttribute('endCol', col+ colspan-1);
1069 sr.setAttribute('startRow', row);
1070 sr.setAttribute('endRow', row + rowspan -1);
1073 var st = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Style');
1075 // do we need some defaults..
1078 st.setAttribute(k, ent[k]);
1081 var fo = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Font');
1083 // do we need some defaults..
1084 for(var k in fent) {
1085 fo.setAttribute(k, fent[k]);
1087 fo.textContent = font;
1091 Roo.each(['top','left','bottom','right'], function(p) {
1092 var w = el.getStyle('border-' + p + '-width').replace(/px/, '');
1093 if (!w || !w.length || (w*1) < 1) {
1097 sb= this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleBorder');
1099 var be = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:' + p[0].toUpperCase() + p.substring(1));
1100 be.setAttribute('Style', '1');
1101 be.setAttribute('Color', '0:0:0'); // fixme..
1105 // start adding them all together..
1120 * write an image (needs base64 data to write it)
1123 * @param {Number} row row to put it in (rows start at 0)
1124 * @param {Number} col column to put it in
1125 * @param {Number} data the base64 description of the images
1126 * @param {Number} width image width
1127 * @param {Number} width image height
1132 writeImage : function (row, col, data, width, height)
1135 // our default height width is 50/50 ?!
1136 //console.log('w='+width+',height='+height);
1142 var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1143 var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1145 //<gmr:SheetObjectImage
1146 // ObjectBound="A3:J8"
1147 // ObjectOffset="0.375 0.882 0.391 0.294"
1148 // ObjectAnchorType="16 16 16 16"
1150 // crop-top="0.000000"
1151 // crop-bottom="0.000000"
1152 // crop-left="0.000000"
1153 // crop-right="0.000000">
1156 //alert(gnumeric_colRowToName(row,col));
1158 // this is where we really have fun!!!...
1159 // since our design currently assumes the height is enough to fit
1160 // stuff in, we only really need to work out how wide it has to be..
1162 // note we should probably use centralized calcs if it fits in the first cell!
1164 // step 1 - work out how many columns it will span..
1165 // lets hope the spreadsheet is big enought..
1168 for ( endcol=col;endcol <100; endcol++) {
1169 if (!this.colInfo[endcol]) {
1170 this.colInfo[endcol] = 100; // eak fudge
1172 colwidth += this.colInfo[endcol];
1173 if (colwidth > width) {
1179 soi.setAttribute('ObjectBound',
1180 //gnumeric_colRowToName(row,col) + ':' + gnumeric_colRowToName(row+1,col+1));
1181 this.RCtoCell(row,col) + ':' + this.RCtoCell(row,endcol));
1183 var ww = 0.01; // offset a bit...
1186 var ww2 = 1 - ((colwidth - width) / this.colInfo[endcol]);
1189 var offset_str = ww + ' ' + hh + ' ' + ww2 + ' '+hh2;
1190 //console.log(offset_str );
1191 //alert(offset_str);
1192 soi.setAttribute('ObjectOffset', offset_str);
1193 soi.setAttribute('ObjectAnchorType','16 16 16 16');
1194 soi.setAttribute('Direction','17');
1195 soi.setAttribute('crop-top','0.000000');
1196 soi.setAttribute('crop-bottom','0.000000');
1197 soi.setAttribute('crop-left','0.000000');
1198 soi.setAttribute('crop-right','0.000000');
1199 // <Content image-type="jpeg" size-bytes="3900">...... < / Content>
1200 var content = this.doc.createElement('Content');
1201 content.setAttribute('image-type','jpeg');
1204 content.setAttribute('size-bytes',data.length);
1205 content.textContent = data;
1206 soi.appendChild(content);
1207 objs.appendChild(soi);
1209 //< /gnm:SheetObjectImage>
1216 * Merge cells in the spreadsheet. (does not check if existing merges exist..)
1218 * @param {Number} col1 first column
1219 * @param {Number} row1 first row
1220 * @param {Number} col2 to column
1221 * @param {Number} row2 to row
1224 mergeRegion : function (col1,row1,col2,row2)
1226 var cell = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Merge');
1227 //if (col1 > 50|| col2 > 50) { // do not merge cols off to right?
1231 cell.textContent = this.RCtoCell(row1,col1) + ':' + this.RCtoCell(row2,col2);
1233 //var merges = this.gnumeric.getElementsByTagNameNS('*','MergedRegions');
1234 var merges = this.sheet.getElementsByTagNameNS('*','MergedRegions');
1235 if (!merges || !merges.length) {
1236 merges = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:MergedRegions');
1237 var sl = this.sheet.getElementsByTagNameNS('*','SheetLayout')[0];
1238 this.sheet.insertBefore(merges,sl);
1242 merges.appendChild(cell);
1247 * Sets the height of a row.
1249 * @param {Number} r the row to set the height of. (rows start at 0)
1250 * @param {Number} height (in pixels)
1252 setRowHeight : function (r,height)
1255 //<gmr:Rows DefaultSizePts="12.75">
1256 // <gmr:RowInfo No="2" Unit="38.25" MarginA="0" MarginB="0" HardSize="1"/>
1259 // this doesnt handle row ranges very well.. - with 'count in them..'
1261 if (this.rowInfoDom[r]) {
1262 this.rowInfoDom[r].setAttribute('Unit', height);
1266 var rows = this.sheet.getElementsByTagNameNS('*','Rows')[0]; // assume this exists..
1267 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:RowInfo');
1268 // assume we have no rows..
1269 ri.setAttribute('No', r-1);
1270 ri.setAttribute('Unit', height);
1271 ri.setAttribute('MarginA', 0);
1272 ri.setAttribute('MarginB', 0);
1273 ri.setAttribute('HardSize', 1);
1274 rows.appendChild(ri);
1275 this.rowInfoDom[r] = ri;
1280 * Set the sheet name.
1281 * @param {String} title for sheet
1283 setSheetName : function(name,sheet)
1287 <gnm:SheetNameIndex>
1288 <gnm:SheetName>Sheet1</gnm:SheetName>
1289 <gnm:SheetName>Sheet2</gnm:SheetName>
1290 <gnm:SheetName>Sheet3</gnm:SheetName>
1291 </gnm:SheetNameIndex>
1293 // has to set sheet name on index and body..
1296 var sheetnames = this.doc.getElementsByTagNameNS('*','SheetName');
1297 if (sheet >= sheetnames.length) {
1299 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1301 sheetnames = this.doc.getElementsByTagNameNS('*','Sheet');
1302 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1303 var sn = this.doc.getElementsByTagNameNS('*','Sheet')[sheet];
1304 var cls = sn.getElementsByTagNameNS('*','Cells')[0];
1305 while (cls.childNodes.length) {
1306 cls.removeChild(cls.firstChild);
1311 var sheetn = this.doc.getElementsByTagNameNS('*','SheetName')[sheet];
1312 sheetn.textContent = name;
1313 var sheetb = this.doc.getElementsByTagNameNS('*','Sheet')[sheet].getElementsByTagNameNS('*','Name')[0];
1314 sheetb.textContent = name;
1315 this.parseDoc(sheet);
1323 * Set the column width
1324 * @param {Number} column number (starts at '0')
1325 * @param {Number} width size of column
1327 setColumnWidth : function(column, width)
1331 if (typeof(this.colInfoDom[column]) == 'undefined') {
1332 var cols = this.doc.getElementsByTagNameNS('*','Cols')[0];
1333 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:ColInfo');
1334 ri.setAttribute('No', column);
1335 ri.setAttribute('Unit', width);
1336 ri.setAttribute('MarginA', 2);
1337 ri.setAttribute('MarginB', 2);
1338 ri.setAttribute('HardSize', 1);
1339 cols.appendChild(ri);
1340 this.colInfo[column] = width;
1341 this.colInfoDom[column] = ri;
1344 this.colInfoDom[column].setAttribute('Unit', width);
1354 * Convert spreadsheet into a HTML table.
1360 function calcWidth(sc, span)
1363 for(var i =sc; i< sc+span;i++) {
1369 var grid = this.grid;
1370 // lets do a basic dump..
1371 var out = '<table style="table-layout:fixed;" cellpadding="0" cellspacing="0">';
1372 for (var r = 0; r < this.rmax;r++) {
1373 out += '<tr style="height:'+this.rowInfo[r]+'px;">';
1374 for (var c = 0; c < this.cmax;c++) {
1375 if (typeof(grid[r][c]) == 'undefined') {
1376 this.createCell(r,c);
1381 if (typeof(g.cls) =='undefined') g.cls = [];
1382 var w= calcWidth(c,g.colspan);
1383 out+=String.format('<td colspan="{0}" rowspan="{1}" class="{4}"><div style="{3}">{2}</div></td>',
1384 g.colspan, g.rowspan, g.value,
1385 'overflow:hidden;' +
1388 'text-overflow:ellipsis;' +
1389 'white-space:nowrap;',
1399 return out+'</table>';
1406 * @param {String} name filename to downlaod (without xls)
1407 * @param {String} callback (optional) - callback to call after callback is complete.
1409 download : function(name,callback)
1411 name = name || "Missing_download_filename";
1413 if (this.downloadURL && this.downloadURL.charAt(this.downloadURL .length-1) != '/') {
1414 this.downloadURL += '/';
1417 var ser = new XMLSerializer();
1418 var x = new Pman.Download({
1420 timeout : 120000, // quite a long wait.. 2 minutes.
1422 xml : ser.serializeToString(this.doc),
1423 format : 'xls', //xml
1427 url : (this.downloadURL || (baseURL + '/GnumericToExcel/')) + name + '.xls',
1428 success : function() {
1429 Roo.MessageBox.alert("Alert", "File should have downloaded now");