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) {
192 _t.doc = resp.responseXML;
199 _t.fireEvent('load', _t);
203 Roo.MessageBox.alert("Error", "Failed to Load Template for Spreadsheet");
212 RCtoCell : function(r,c)
214 // we wil only support AA not AAA
215 var top = Math.floor(c/26);
217 var cc = top > 0 ? String.fromCharCode('A'.charCodeAt(0) + top) : '';
218 cc += String.fromCharCode('A'.charCodeAt(0) + bot);
225 * convert 'A1' style position to row/column reference
227 * @arg {String} k cell name
228 * @return {Object} as { r: {Number} , c: {Number} }
233 var c = k.charCodeAt(0)-64;
234 var n = k.substring(1);
235 if (k.charCodeAt(1) > 64) {
237 c+=k.charCodeAt(1)-64;
240 return { c:c -1 ,r: (n*1)-1 }
244 * convert 'A1:B1' style position to array of row/column references
246 * @arg {String} k cell range
247 * @return {Array} as [ { r: {Number} , c: {Number} }. { r: {Number} , c: {Number} } ]
249 rangeToRC : function(s) {
250 var ar = s.split(':');
251 return [ this.toRC(ar[0]) , this.toRC(ar[1])]
260 * convert XML document into cells and other data..
263 parseDoc : function(sheetnum)
270 this.sheet = _t.doc.getElementsByTagNameNS('*','Sheet')[sheetnum];
273 this.cellholder = this.sheet.getElementsByTagNameNS('*','Cells')[0];
274 var cells = this.sheet.getElementsByTagNameNS('*','Cell');
278 Roo.each(cells, function(c) {
280 var row = c.getAttribute('Row') * 1;
281 var col = c.getAttribute('Col') * 1;
282 _t.cmax = Math.max(col+1, _t.cmax);
283 _t.rmax = Math.max(row+1, _t.rmax);
284 var vt = c.getAttribute('ValueType');
285 var vf = c.getAttribute('ValueFormat');
286 var val = c.textContent;
288 if (typeof(_t.grid[row]) == 'undefined') {
291 _t.grid[row][col] = Roo.applyIf({
301 for (var r = 0; r < this.rmax;r++) {
302 if (typeof(this.grid[r]) == 'undefined') {
305 for (var c = 0; c < this.cmax;c++) {
306 if (typeof(this.grid[r][c]) == 'undefined') {
309 //this.print( "[" + r + "]["+c+"]=" + grid[r][c].value +'<br/>');
313 var merge = this.sheet.getElementsByTagNameNS('*','Merge');
315 Roo.each(merge, function(c) {
316 var rc = _t.rangeToRC(c.textContent);
317 //Roo.log(JSON.stringify(rc));
318 if (typeof(_t.grid[rc[0].r][rc[0].c]) == 'undefined') {
319 //Roo.log(["creating empty cell for ",rc[0].r, rc[0].c ]);
320 _t.createCell(rc[0].r, rc[0].c );
321 //_t.grid[rc[0].r][rc[0].c] = //Roo.applyIf({ r : rc[0].r, c : rc[0].c }, _t.defaultCell);
324 _t.grid[rc[0].r][rc[0].c].colspan = (rc[1].c - rc[0].c) + 1;
325 _t.grid[rc[0].r][rc[0].c].rowspan = (rc[1].r - rc[0].r) + 1;
326 for(var r = (rc[0].r); r < (rc[1].r+1); r++) {
327 for(var cc = rc[0].c; cc < (rc[1].c+1); cc++) {
328 //Roo.log('adding alias : ' + r+','+c);
329 _t.grid[r][cc] = _t.grid[rc[0].r][rc[0].c];
336 var ci = this.sheet.getElementsByTagNameNS('*','ColInfo');
338 this.colInfoDom = {};
340 Roo.each(ci, function(c) {
341 var count = c.getAttribute('Count') || 1;
342 var s = c.getAttribute('No')*1;
343 for(var i =0; i < count; i++) {
344 _t.colInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
345 _t.colInfoDom[s+i] = c;
350 ci = this.sheet.getElementsByTagNameNS('*','RowInfo');
353 this.rowInfoDom = {};
354 Roo.each(ci, function(c) {
355 var count = c.getAttribute('Count') || 1;
356 var s = c.getAttribute('No')*1;
357 for(var i =0; i < count; i++) {
358 _t.rowInfoDom[s+i] = c;
359 _t.rowInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
372 * put the style info onto the cell data.
375 overlayStyles : function ()
379 Roo.each(this.styles, function(s) {
381 for (var r = s.r; r < s.r1;r++) {
382 if (typeof(_t.grid[r]) == 'undefined') {
385 for (var c = s.c; c < s.c1;c++) {
390 if (typeof(_t.grid[r][c]) == 'undefined') {
392 //_t.grid[r][c] = Roo.applyIf({ r: r , c : c }, _t.defaultCell);
395 if (typeof(g.cls) =='undefined') {
399 if (g.cls.indexOf(s.name) > -1) {
403 g.styles.push(s.dom);
411 * read the style information
412 * generates a stylesheet for the current file
413 * this should be disposed of really.....
416 parseStyles : function() {
418 var srs = this.sheet.getElementsByTagNameNS('*','StyleRegion');
423 HAlign : function(ent,v) {
424 ent['text-align'] = { '1' : 'left', '8': 'center', '4' : 'right'}[v] || 'left';
426 VAlign : function(ent,v) {
427 ent['vertical-align'] = { '1' : 'top', '4': 'middle', '8' : 'bottom'}[v] || 'top'
429 Fore : function(ent,v) {
431 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
432 ent['color'] = 'rgb(' + col.join(',') + ')';
434 Back : function(ent,v) {
436 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
437 ent['background-color'] = 'rgb(' + col.join(',') + ')';
439 FontUnit : function(ent,v) {
440 ent['font-size'] = v + 'px';
442 FontBold : function(ent,v) {
443 if (v*1 < 1) { return; }
444 ent['font-weight'] = 'bold';
446 FontItalic : function(ent,v) {
447 if (v*0 < 1) { return; }
448 //ent['font-weight'] = 'bold';
450 FontName : function(ent,v) {
451 ent['font-family'] = v;
453 BorderStyle : function(ent,v) {
454 var vv = v.split('-');
455 ent['border-'+vv[0]+'-style'] = 'solid';
456 ent['border-'+vv[0]+'-width'] = vv[1]+'px';
458 BorderColor : function(ent,v) {
459 var vv = v.split('-');
461 Roo.each(vv[1].split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); });
462 ent['border-'+vv[0]+'-color'] = 'rgb(' + col.join(',') + ')';
465 function add(e, k, v) {
468 if (typeof(map[k]) == 'undefined') {
478 Roo.each(srs, function(sr,n)
481 c : sr.getAttribute('startCol') *1,
482 r : sr.getAttribute('startRow')*1,
483 c1 : (sr.getAttribute('endCol')*1) +1,
484 r1 : (sr.getAttribute('endRow')*1) +1,
485 style : {}, // key val of style for HTML..
486 gstyle : {}, // key val of attributes used..
487 name : sid +'-gstyle-' + n,
492 Roo.each(sr.getElementsByTagNameNS('*','Style')[0].attributes, function(e) {
493 add(ent, e.name, e.value);
495 if (sr.getElementsByTagNameNS('*','Font').length) {
496 Roo.each(sr.getElementsByTagNameNS('*','Font')[0].attributes, function(e) {
497 add(ent, 'Font'+e.name, e.value);
500 add(ent, 'FontName', sr.getElementsByTagNameNS('*','Font')[0].textContent);
503 if (sr.getElementsByTagNameNS('*','StyleBorder').length) {
504 Roo.each(sr.getElementsByTagNameNS('*','StyleBorder')[0].childNodes, function(e) {
508 Roo.each(e.attributes, function(ea) {
509 add(ent, 'Border'+ea.name, e.tagName.split(':')[1].toLowerCase() + '-' + ea.value);
515 css['.'+ent.name] = ent.style;
518 this.styles = styles;
520 this.stylesheetID = sid;
521 Roo.util.CSS.createStyleSheet(css, sid);
527 /* --------------------------------------- AFTER LOAD METHODS... ----------------------- */
530 * Set the value of a cell..
531 * @param {String} cell name of cell, eg. C10 or { c: 1, r :1 }
533 * @param {Value} value to put in cell..
534 * @param {ValueType} type of value
535 * @param {ValueFormat} value format of cell
537 * Cells should exist at present, we do not make them up...
541 set : function(cell, v, vt, vf) {
543 var cs= typeof(cell) == 'string' ? this.toRC(cell) : cell;
546 Roo.log( cs.r+ ',' + cs.c + ' = '+ v);
547 // need to generate clell if it doe
548 if (typeof(this.grid[cs.r]) == 'undefined') {
549 Roo.log('no row:' + cell);
550 this.grid[cs.r] = []; // create a row..
553 if (typeof(this.grid[cs.r][cs.c]) == 'undefined') {
554 Roo.log('cell not defined:' + cell);
555 this.createCell(cs.r,cs.c);
557 // cell might not be rendered yet... so if we try and create a cell, it overrides the default formating..
559 if (typeof(this.grid[cs.r][cs.c].dom) == 'undefined') {
560 Roo.log('no default content for cell:' + cell);
561 Roo.log(this.grid[cs.r][cs.c]);
562 //this.createCell(cs.r,cs.c);
565 this.grid[cs.r][cs.c].value= v;
566 if (this.grid[cs.r][cs.c].dom) {
567 this.grid[cs.r][cs.c].dom.textContent= v;
571 if (typeof(vt) != 'undefined') {
572 this.grid[cs.r][cs.c].valueType = vt;
573 this.grid[cs.r][cs.c].dom.setAttribute('ValueType', vt);
574 if (vt === '' || vt === false) { // value type is empty for formula's
575 this.grid[cs.r][cs.c].dom.removeAttribute('ValueType');
578 if (typeof(vf) != 'undefined' && vf !== false) {
579 this.grid[cs.r][cs.c].valueFormat = vf;
580 this.grid[cs.r][cs.c].dom.setAttribute('ValueFormat', vf);
581 if (vf === '' || vf === false) { // value type is empty for formula's
582 this.grid[cs.r][cs.c].dom.removeAttribute('ValueFormat');
589 copyRow : function(src, dest) {
593 // Roo.log('create Row' + dest);
594 if (typeof(this.grid[dest]) == 'undefined') {
599 for (var c = 0; c < this.cmax; c++) {
601 this.copyCell({ r: src, c: c } , { r: dest, c: c});
604 this.rmax = Math.max(this.rmax, dest +1);
610 createCell: function(r,c)
612 //<gnm:Cell Row="6" Col="5" ValueType="60">Updated</gnm:Cell>
613 var nc = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Cell');
614 this.cellholder.appendChild(nc);
615 var lb = this.doc.createTextNode("\n");// add a line break..
616 this.cellholder.appendChild(lb);
618 nc.setAttribute('Row', new String(r));
619 nc.setAttribute('Col', new String(c));
620 nc.setAttribute('ValueType', '60');
623 this.grid[r][c] = Roo.applyIf({
637 copyCell : function(src, dest)
639 var old = this.grid[src.r][src.c];
641 if ((old.c != src.c) || (old.r != src.r)) {
642 // only really works on horizonatal merges..
644 this.grid[dest.r][dest.c] = this.grid[desc.r][old.c]; // let's hope it exists.
649 var nc = Roo.apply({}, this.grid[src.r][src.c]);
652 if (typeof(old.dom) == 'undefined') {
653 Roo.log("No cell to copy for " + Roo.encode(src));
656 this.grid[dest.r][dest.c] = nc;
657 nc.dom = old.dom.cloneNode(true);
658 nc.dom.setAttribute('Row', dest.r);
659 nc.dom.setAttribute('Cell', dest.c);
660 nc.dom.textContent = '';
661 old.dom.parentNode.appendChild(nc.dom);
662 if (!old.styles || !old.styles.length) {
669 Roo.each(old.styles, function(s) {
670 // try and extend existing styles..
671 var er = s.getAttribute('endRow') * 1;
672 var ec = s.getAttribute('endCol') * 1;
675 s.setAttribute('endRow', dest.r + 1);
678 s.setAttribute('endCol', dest.c + 1);
680 /*var ns = s.cloneNode(true);
681 s.parentNode.appendChild(ns);
682 ns.setAttribute('startCol', dest.c);
683 ns.setAttribute('startRow', dest.r);
684 ns.setAttribute('endCol', dest.c + 1);
685 ns.setAttribute('endRow', dest.r +1);
694 * Set the value of a cell..
695 * @param {String} cell name of cell, eg. C10
696 * @param {Value} value to put in cell..
698 * Cells should exist at present, we do not make them up...
701 applyData : function(data)
704 data = data || this.data;
705 for (var r = 0; r < this.rmax;r++) {
706 if (typeof(this.grid[r]) == 'undefined') {
709 for (var c = 0; c < this.cmax;c++) {
710 if (typeof(this.grid[r][c]) == 'undefined') {
713 if (!this.grid[r][c].value.length
714 || !this.grid[r][c].value.match(/\{/)) {
718 var x = new Roo.Template({ html: this.grid[r][c].value });
720 var res = x.applyTemplate(data);
721 //Roo.log("set " + r + "," + c + ":"+res)
722 this.set({ r: r, c: c}, x.applyTemplate(data));
724 // Roo.log(e.toString());
734 readTableData : function(table)
736 // read the first row.
737 var tds = Roo.get(table).select('tr').item(0).select('td');
740 Roo.get(table).select('tr').each(function(trs) {
743 trs.select('td').each(function(td) {
744 var cs = td.dom.getAttribute('colspan');
745 cs = cs ? cs * 1 : 1;
748 maxnc = Math.max(nc, maxnc);
751 var tr = document.createElement('tr');
752 table.appendChild(tr);
754 for (i =0; i < maxnc; i++) {
755 ar[i] = document.createElement('td');
756 tr.appendChild(ar[i]);
759 var ret = { cols : maxnc, pos : {} };
760 for (i =0; i < maxnc; i++) {
761 ret.pos[ Roo.get(ar[i]).getLeft()] =i;
763 ret.near = function(p) {
764 // which one is nearest..
771 for(var i in this.pos) {
772 var dis = Math.abs(p-i);
781 table.removeChild(tr);
790 * Import a table and put it into the spreadsheet
791 * @param {HTMLTable} datagrid dom element of html table.
792 * @param {Number} xoff X offset to start rendering to
793 * @param {Number} yoff Y offset to start rendering to
797 importTable : function (datagrid, xoff,yoff)
800 Roo.log("Error table not found!?");
807 var table_data = this.readTableData(datagrid);
809 // oroginally this cleaned line breaks, but we acutally need them..
810 var cleanHTML = function (str) {
813 ret = ret.replace(/ /g,' ');
814 // ret = ret.replace(/\n/g,'.');
815 // ret = ret.replace(/\r/g,'.');
822 // <cell col="A" row="1">Test< / cell>
823 // <cell col="B" row="2" type="Number" format="test1">30< / cell>
825 var rows = datagrid.getElementsByTagName('tr');
826 //alert(rows.length);
829 for(var row=0;row<rows.length;row++) {
831 // let's see what affect this has..
832 // it might mess things up..
834 if (rows[row].getAttribute('xls:height')) {
835 this.setRowHeight(row + yoff +1, 1* rows[row].getAttribute('xls:height'));
837 this.setRowHeight( row + yoff +1, Roo.get(rows[row]).getHeight());
841 var cols = rows[row].getElementsByTagName('td');
844 for(var col=0;col < cols.length; col++) {
849 var colspan = cols[col].getAttribute('colspan');
850 colspan = colspan ? colspan *1 : 1;
852 var rowspan = cols[col].getAttribute('rowspan');
853 rowspan = rowspan ? rowspan * 1 : 1;
855 var realcol = table_data.near( Roo.get(cols[col]).getLeft() );
859 if (colspan > 1 || rowspan > 1) {
861 // getting thisese right is tricky..
865 realcol+ xoff + (colspan -1),
872 // set the style first..
873 this.parseHtmlStyle( cols[col], row + yoff, realcol + xoff , colspan, rowspan);
875 if (!cols[col].childNodes.length) {
885 var xlstype = cols[col].getAttribute('xls:type');
893 vt = 40; // float!!!!
894 if (cols[col].getAttribute('xls:floatformat')) {
895 vf = cols[col].getAttribute('xls:floatformat');
901 //ValueFormat="d/m/yyyy" 38635
903 if (cols[col].getAttribute('xls:dateformat')) {
904 vf= cols[col].getAttribute('xls:dateformat');
916 if (!cols[col].childNodes[0].nodeValue) {
920 if (!cols[col].childNodes[0].nodeValue.replace(/^\s*|\s*$/g,"").length) {
925 var cell_value_text = cleanHTML(cols[col].childNodes[0].nodeValue);
927 if (cols[col].getAttribute('xls:percent')) {
928 cell_value_text = '' + ((cell_value_text * 1) / 100);
931 if (cell_value_text.length && (vt == 30) && xlstype == 'date') {
932 var bits = cell_value_text.split(/-/);
933 var cur = new Date(bits[0],bits[1]-1,bits[2]);
934 cell_value_text = '' + Math.round((cur.getTime() - Date.UTC(1899,11,30)) / (24 * 60 * 60 * 1000));
939 if (cols[col].getAttribute('xls:formula')) {
940 var s = cols[col].getAttribute('xls:formula');
942 cell_value_text = s.replace(/#row#/g,(row + yoff + 1));
944 this.set({ r: row + yoff, c : realcol + xoff }, cell_value_text, vt, vf);
952 this.rowOffset += rows.length;
958 parseHtmlStyle : function(dom, row, col, colspan, rowspan) {
960 function toCol (rgb) {
962 var ar = rgb.replace(/rgb[a]?\(/, '').replace(/\)/, '').replace(/ /, '').split(',');
965 Roo.each(ar, function(c) {
966 rcs.push((c*c).toString(16)) ;
968 return rcs.join(':');
972 var el = Roo.get(dom);
974 'text-align' : function(ent,v) {
975 ent['HAlign'] = { 'left' : '1', 'center' : '8' , 'right' : '4' }[v] || '1';
977 'vertical-align': function(ent,v) {
978 ent['VAlign'] = { 'top' : '1', 'middel' : '8' , 'bottom' : '4' }[v] || '1';
981 'color': function(ent,v) {
982 ent['Fore'] = toCol(v);
983 // this is a bit dumb.. we assume that if it's not black text, then it's shaded..
984 if (ent['Fore'] != '0:0:0') {
989 'background-color' : function(ent,v) {
990 ent['Back'] = toCol(v);
1007 Back:"FFFF:FFFF:FFFF",
1008 PatternColor:"0:0:0",
1013 var val = el.getStyle(k);
1014 if (!val || !val.length) {
1020 if (el.dom.getAttribute('xls:wraptext')) {
1023 if (el.dom.getAttribute('xls:valign')) {
1026 if (el.dom.getAttribute('xls:halign')) {
1033 'font-size' : function(ent,v) {
1034 ent['Unit'] = v.replace(/px/, '');
1036 'font-weight' : function(ent,v) {
1042 'font-style' : function(ent,v) {
1043 if (v != 'italic') {
1058 for(var k in fmap) {
1059 var val = el.getStyle(k);
1060 if (!val || !val.length) {
1065 var font = el.getStyle('font-family') || 'Sans';
1066 if (font.split(',').length > 1) {
1067 font = font.split(',')[1].replace(/\s+/, '');
1071 /// -- now create elements..
1073 var objs = this.sheet.getElementsByTagNameNS('*','Styles')[0];
1075 //<gnm:StyleRegion startCol="0" startRow="0" endCol="255" endRow="65535"
1076 var sr = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleRegion');
1077 objs.appendChild(sr);
1078 objs.appendChild(this.doc.createTextNode("\n"));// add a line break..
1080 sr.setAttribute('startCol', col);
1081 sr.setAttribute('endCol', col+ colspan-1);
1082 sr.setAttribute('startRow', row);
1083 sr.setAttribute('endRow', row + rowspan -1);
1086 var st = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Style');
1088 // do we need some defaults..
1091 st.setAttribute(k, ent[k]);
1094 var fo = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Font');
1096 // do we need some defaults..
1097 for(var k in fent) {
1098 fo.setAttribute(k, fent[k]);
1100 fo.textContent = font;
1104 Roo.each(['top','left','bottom','right'], function(p) {
1105 var w = el.getStyle('border-' + p + '-width').replace(/px/, '');
1106 if (!w || !w.length || (w*1) < 1) {
1110 sb= this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleBorder');
1112 var be = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:' + p[0].toUpperCase() + p.substring(1));
1113 be.setAttribute('Style', '1');
1114 be.setAttribute('Color', '0:0:0'); // fixme..
1118 // start adding them all together..
1133 * write an image (needs base64 data to write it)
1136 * @param {Number} row row to put it in (rows start at 0)
1137 * @param {Number} col column to put it in
1138 * @param {Number} data the base64 description of the images
1139 * @param {Number} width image width
1140 * @param {Number} width image height
1145 writeImage : function (row, col, data, width, height)
1149 throw "write Image called with missing data";
1151 // our default height width is 50/50 ?!
1152 //console.log('w='+width+',height='+height);
1158 var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1159 var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1161 //<gmr:SheetObjectImage
1162 // ObjectBound="A3:J8"
1163 // ObjectOffset="0.375 0.882 0.391 0.294"
1164 // ObjectAnchorType="16 16 16 16"
1166 // crop-top="0.000000"
1167 // crop-bottom="0.000000"
1168 // crop-left="0.000000"
1169 // crop-right="0.000000">
1172 //alert(gnumeric_colRowToName(row,col));
1174 // this is where we really have fun!!!...
1175 // since our design currently assumes the height is enough to fit
1176 // stuff in, we only really need to work out how wide it has to be..
1178 // note we should probably use centralized calcs if it fits in the first cell!
1180 // step 1 - work out how many columns it will span..
1181 // lets hope the spreadsheet is big enought..
1184 for ( endcol=col;endcol <100; endcol++) {
1185 if (!this.colInfo[endcol]) {
1186 this.colInfo[endcol] = 100; // eak fudge
1188 colwidth += this.colInfo[endcol];
1189 if (colwidth > width) {
1195 soi.setAttribute('ObjectBound',
1196 //gnumeric_colRowToName(row,col) + ':' + gnumeric_colRowToName(row+1,col+1));
1197 this.RCtoCell(row,col) + ':' + this.RCtoCell(row,endcol));
1199 var ww = 0.01; // offset a bit...
1202 var ww2 = 1 - ((colwidth - width) / this.colInfo[endcol]);
1205 var offset_str = ww + ' ' + hh + ' ' + ww2 + ' '+hh2;
1206 //console.log(offset_str );
1207 //alert(offset_str);
1208 soi.setAttribute('ObjectOffset', offset_str);
1209 soi.setAttribute('ObjectAnchorType','16 16 16 16');
1210 soi.setAttribute('Direction','17');
1211 soi.setAttribute('crop-top','0.000000');
1212 soi.setAttribute('crop-bottom','0.000000');
1213 soi.setAttribute('crop-left','0.000000');
1214 soi.setAttribute('crop-right','0.000000');
1215 // <Content image-type="jpeg" size-bytes="3900">...... < / Content>
1216 var content = this.doc.createElement('Content');
1217 content.setAttribute('image-type','jpeg');
1220 content.setAttribute('size-bytes',data.length);
1221 content.textContent = data;
1222 soi.appendChild(content);
1223 objs.appendChild(soi);
1225 //< /gnm:SheetObjectImage>
1232 * Merge cells in the spreadsheet. (does not check if existing merges exist..)
1234 * @param {Number} col1 first column
1235 * @param {Number} row1 first row
1236 * @param {Number} col2 to column
1237 * @param {Number} row2 to row
1240 mergeRegion : function (col1,row1,col2,row2)
1242 var cell = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Merge');
1243 //if (col1 > 50|| col2 > 50) { // do not merge cols off to right?
1247 cell.textContent = this.RCtoCell(row1,col1) + ':' + this.RCtoCell(row2,col2);
1249 //var merges = this.gnumeric.getElementsByTagNameNS('*','MergedRegions');
1250 var merges = this.sheet.getElementsByTagNameNS('*','MergedRegions');
1251 if (!merges || !merges.length) {
1252 merges = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:MergedRegions');
1253 var sl = this.sheet.getElementsByTagNameNS('*','SheetLayout')[0];
1254 this.sheet.insertBefore(merges,sl);
1258 merges.appendChild(cell);
1263 * Sets the height of a row.
1265 * @param {Number} r the row to set the height of. (rows start at 0)
1266 * @param {Number} height (in pixels)
1268 setRowHeight : function (r,height)
1271 //<gmr:Rows DefaultSizePts="12.75">
1272 // <gmr:RowInfo No="2" Unit="38.25" MarginA="0" MarginB="0" HardSize="1"/>
1275 // this doesnt handle row ranges very well.. - with 'count in them..'
1277 if (this.rowInfoDom[r]) {
1278 this.rowInfoDom[r].setAttribute('Unit', height);
1282 var rows = this.sheet.getElementsByTagNameNS('*','Rows')[0]; // assume this exists..
1283 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:RowInfo');
1284 // assume we have no rows..
1285 ri.setAttribute('No', r-1);
1286 ri.setAttribute('Unit', height);
1287 ri.setAttribute('MarginA', 0);
1288 ri.setAttribute('MarginB', 0);
1289 ri.setAttribute('HardSize', 1);
1290 rows.appendChild(ri);
1291 this.rowInfoDom[r] = ri;
1296 * Set the sheet name.
1297 * @param {String} title for sheet
1299 setSheetName : function(name,sheet)
1303 <gnm:SheetNameIndex>
1304 <gnm:SheetName>Sheet1</gnm:SheetName>
1305 <gnm:SheetName>Sheet2</gnm:SheetName>
1306 <gnm:SheetName>Sheet3</gnm:SheetName>
1307 </gnm:SheetNameIndex>
1309 // has to set sheet name on index and body..
1312 var sheetnames = this.doc.getElementsByTagNameNS('*','SheetName');
1313 if (sheet >= sheetnames.length) {
1315 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1317 sheetnames = this.doc.getElementsByTagNameNS('*','Sheet');
1318 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1319 var sn = this.doc.getElementsByTagNameNS('*','Sheet')[sheet];
1320 var cls = sn.getElementsByTagNameNS('*','Cells')[0];
1321 while (cls.childNodes.length) {
1322 cls.removeChild(cls.firstChild);
1327 var sheetn = this.doc.getElementsByTagNameNS('*','SheetName')[sheet];
1328 sheetn.textContent = name;
1329 var sheetb = this.doc.getElementsByTagNameNS('*','Sheet')[sheet].getElementsByTagNameNS('*','Name')[0];
1330 sheetb.textContent = name;
1331 this.parseDoc(sheet);
1339 * Set the column width
1340 * @param {Number} column number (starts at '0')
1341 * @param {Number} width size of column
1343 setColumnWidth : function(column, width)
1347 if (typeof(this.colInfoDom[column]) == 'undefined') {
1348 var cols = this.doc.getElementsByTagNameNS('*','Cols')[0];
1349 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:ColInfo');
1350 ri.setAttribute('No', column);
1351 ri.setAttribute('Unit', width);
1352 ri.setAttribute('MarginA', 2);
1353 ri.setAttribute('MarginB', 2);
1354 ri.setAttribute('HardSize', 1);
1355 cols.appendChild(ri);
1356 this.colInfo[column] = width;
1357 this.colInfoDom[column] = ri;
1360 this.colInfoDom[column].setAttribute('Unit', width);
1370 * Convert spreadsheet into a HTML table.
1376 function calcWidth(sc, span)
1379 for(var i =sc; i< sc+span;i++) {
1385 var grid = this.grid;
1386 // lets do a basic dump..
1387 var out = '<table style="table-layout:fixed;" cellpadding="0" cellspacing="0">';
1388 for (var r = 0; r < this.rmax;r++) {
1389 out += '<tr style="height:'+this.rowInfo[r]+'px;">';
1390 for (var c = 0; c < this.cmax;c++) {
1391 if (typeof(grid[r][c]) == 'undefined') {
1392 this.createCell(r,c);
1397 if (typeof(g.cls) =='undefined') {
1400 var w= calcWidth(c,g.colspan);
1402 var value = g.value[0] == '=' ? 'CALCULATED' : g.value;
1406 g.styles[0].firstElementChild.getAttribute('Format') == "D\\-MMM\\-YYYY;@" &&
1407 g.value[0] != '=' &&
1408 !isNaN(value * 1) &&
1411 value = new Date(value * 24 * 60 * 60 * 1000 + new Date('1899-12-30').getTime()).format('d-M-Y');
1418 out+=String.format('<td colspan="{0}" rowspan="{1}" class="{4}"><div style="{3}">{2}</div></td>',
1419 g.colspan, g.rowspan, value,
1420 'overflow:hidden;' +
1423 'text-overflow:ellipsis;' +
1424 'white-space:nowrap;',
1434 return out+'</table>';
1441 * @param {String} name filename to downlaod (without xls)
1442 * @param {String} callback (optional) - callback to call after callback is complete.
1444 download : function(name,callback)
1446 name = name || "Missing_download_filename";
1448 if (this.downloadURL && this.downloadURL.charAt(this.downloadURL.length-1) != '/') {
1449 this.downloadURL += '/';
1452 var ser = new XMLSerializer();
1453 var x = new Pman.Download({
1455 timeout : 120000, // quite a long wait.. 2 minutes.
1457 xml : ser.serializeToString(this.doc),
1458 format : 'xls', //xml
1462 url : (this.downloadURL || (baseURL + '/GnumericToExcel/')) + name + '.xls',
1463 success : function() {
1464 Roo.MessageBox.alert("Alert", "File should have downloaded now");