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
131 * @type {Number} cmax - maximum number of columns
135 * @type {Object} rmax - maximum number of rows
139 * @type {String} stylesheetID id of stylesheet created to render spreadsheat
141 stylesheetID : false,
143 * @type {Number} rowOffset - used by table importer to enable multiple tables to be improted
150 * run the connection, parse document and fire load event..
151 * can be run multiple times with new data..
157 this.url = url || this.url;
165 this.colInfo = false;
166 this.rowInfo = false;
170 if (this.stylesheetID) {
172 Roo.util.CSS.removeStyleSheet(this.stylesheetID);
173 this.stylesheetID = false;
178 var c = new Roo.data.Connection();
182 success : function(resp, opts) {
184 _t.doc = resp.responseXML;
191 _t.fireEvent('load', _t);
195 Roo.MessageBox.alert("Error", "Failed to Load Template for Spreadsheet");
204 RCtoCell : function(r,c)
206 // we wil only support AA not AAA
207 var top = Math.floor(c/26);
209 var cc = top > 0 ? String.fromCharCode('A'.charCodeAt(0) + top) : '';
210 cc += String.fromCharCode('A'.charCodeAt(0) + bot);
217 * convert 'A1' style position to row/column reference
219 * @arg {String} k cell name
220 * @return {Object} as { r: {Number} , c: {Number} }
225 var c = k.charCodeAt(0)-64;
226 var n = k.substring(1);
227 if (k.charCodeAt(1) > 64) {
229 c+=k.charCodeAt(1)-64;
232 return { c:c -1 ,r: (n*1)-1 }
236 * convert 'A1:B1' style position to array of row/column references
238 * @arg {String} k cell range
239 * @return {Array} as [ { r: {Number} , c: {Number} }. { r: {Number} , c: {Number} } ]
241 rangeToRC : function(s) {
242 var ar = s.split(':');
243 return [ this.toRC(ar[0]) , this.toRC(ar[1])]
252 * convert XML document into cells and other data..
255 parseDoc : function(sheetnum)
262 this.sheet = _t.doc.getElementsByTagNameNS('*','Sheet')[sheetnum];
265 this.cellholder = this.sheet.getElementsByTagNameNS('*','Cells')[0];
266 var cells = this.sheet.getElementsByTagNameNS('*','Cell');
270 Roo.each(cells, function(c) {
272 var row = c.getAttribute('Row') * 1;
273 var col = c.getAttribute('Col') * 1;
274 _t.cmax = Math.max(col+1, _t.cmax);
275 _t.rmax = Math.max(row+1, _t.rmax);
276 var vt = c.getAttribute('ValueType');
277 var vf = c.getAttribute('ValueFormat');
278 var val = c.textContent;
280 if (typeof(_t.grid[row]) == 'undefined') {
283 _t.grid[row][col] = Roo.applyIf({
293 for (var r = 0; r < this.rmax;r++) {
294 if (typeof(this.grid[r]) == 'undefined') {
297 for (var c = 0; c < this.cmax;c++) {
298 if (typeof(this.grid[r][c]) == 'undefined') {
301 //this.print( "[" + r + "]["+c+"]=" + grid[r][c].value +'<br/>');
305 var merge = this.sheet.getElementsByTagNameNS('*','Merge');
307 Roo.each(merge, function(c) {
308 var rc = _t.rangeToRC(c.textContent);
309 //Roo.log(JSON.stringify(rc))
310 if (typeof(_t.grid[rc[0].r][rc[0].c]) == 'undefined') {
311 _t.grid[rc[0].r][rc[0].c] = Roo.applyIf({ r : rc[0].r, c : rc[0].c }, _t.defaultCell);
314 _t.grid[rc[0].r][rc[0].c].colspan = (rc[1].c - rc[0].c) + 1;
315 _t.grid[rc[0].r][rc[0].c].rowspan = (rc[1].r - rc[0].r) + 1;
316 for(var r = (rc[0].r); r < (rc[1].r+1); r++) {
317 for(var c = rc[0].c; c < (rc[1].c+1); c++) {
318 //Roo.log('adding alias : ' + r+','+c);
319 _t.grid[r][c] = _t.grid[rc[0].r][rc[0].c];
326 var ci = this.sheet.getElementsByTagNameNS('*','ColInfo');
328 this.colInfoDom = {};
330 Roo.each(ci, function(c) {
331 var count = c.getAttribute('Count') || 1;
332 var s = c.getAttribute('No')*1;
333 for(var i =0; i < count; i++) {
334 _t.colInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
335 _t.colInfoDom[s+i] = c;
340 ci = this.sheet.getElementsByTagNameNS('*','RowInfo');
343 Roo.each(ci, function(c) {
344 var count = c.getAttribute('Count') || 1;
345 var s = c.getAttribute('No')*1;
346 for(var i =0; i < count; i++) {
347 _t.rowInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
360 * put the style info onto the cell data.
363 overlayStyles : function ()
367 Roo.each(this.styles, function(s) {
369 for (var r = s.r; r < s.r1;r++) {
370 if (typeof(_t.grid[r]) == 'undefined') {
373 for (var c = s.c; c < s.c1;c++) {
374 if (c > _t.cmax) continue;
376 if (typeof(_t.grid[r][c]) == 'undefined') _t.grid[r][c] = Roo.applyIf({ r: r , c : c }, _t.defaultCell);
378 if (typeof(g.cls) =='undefined') {
382 if (g.cls.indexOf(s.name) > -1) continue;
384 g.styles.push(s.dom);
392 * read the style information
393 * generates a stylesheet for the current file
394 * this should be disposed of really.....
397 parseStyles : function() {
399 var srs = this.sheet.getElementsByTagNameNS('*','StyleRegion');
404 HAlign : function(ent,v) {
405 ent['text-align'] = { '1' : 'left', '8': 'center', '4' : 'right'}[v] || 'left';
407 VAlign : function(ent,v) {
408 ent['vertical-align'] = { '1' : 'top', '4': 'middle', '8' : 'bottom'}[v] || 'top'
410 Fore : function(ent,v) {
412 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
413 ent['color'] = 'rgb(' + col.join(',') + ')';
415 Back : function(ent,v) {
417 Roo.each(v.split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
418 ent['background-color'] = 'rgb(' + col.join(',') + ')';
420 FontUnit : function(ent,v) {
421 ent['font-size'] = v + 'px';
423 FontBold : function(ent,v) {
425 ent['font-weight'] = 'bold';
427 FontItalic : function(ent,v) {
429 //ent['font-weight'] = 'bold';
431 FontName : function(ent,v) {
432 ent['font-family'] = v;
434 BorderStyle : function(ent,v) {
435 var vv = v.split('-');
436 ent['border-'+vv[0]+'-style'] = 'solid';
437 ent['border-'+vv[0]+'-width'] = vv[1]+'px';
439 BorderColor : function(ent,v) {
440 var vv = v.split('-');
442 Roo.each(vv[1].split(':'), function(c) { col.push(Math.round(parseInt(c,16)/256)); })
443 ent['border-'+vv[0]+'-color'] = 'rgb(' + col.join(',') + ')';
446 function add(e, k, v) {
449 if (typeof(map[k]) == 'undefined') {
459 Roo.each(srs, function(sr,n)
462 c : sr.getAttribute('startCol') *1,
463 r : sr.getAttribute('startRow')*1,
464 c1 : (sr.getAttribute('endCol')*1) +1,
465 r1 : (sr.getAttribute('endRow')*1) +1,
466 style : {}, // key val of style for HTML..
467 gstyle : {}, // key val of attributes used..
468 name : sid +'-gstyle-' + n,
473 Roo.each(sr.getElementsByTagNameNS('*','Style')[0].attributes, function(e) {
474 add(ent, e.name, e.value);
476 if (sr.getElementsByTagNameNS('*','Font').length) {
477 Roo.each(sr.getElementsByTagNameNS('*','Font')[0].attributes, function(e) {
478 add(ent, 'Font'+e.name, e.value);
481 add(ent, 'FontName', sr.getElementsByTagNameNS('*','Font')[0].textContent);
484 if (sr.getElementsByTagNameNS('*','StyleBorder').length) {
485 Roo.each(sr.getElementsByTagNameNS('*','StyleBorder')[0].childNodes, function(e) {
489 Roo.each(e.attributes, function(ea) {
490 add(ent, 'Border'+ea.name, e.tagName.split(':')[1].toLowerCase() + '-' + ea.value);
496 css['.'+ent.name] = ent.style;
499 this.styles = styles;
501 this.stylesheetID = sid;
502 Roo.util.CSS.createStyleSheet(css, sid);
508 /* --------------------------------------- AFTER LOAD METHODS... ----------------------- */
511 * Set the value of a cell..
512 * @param {String} cell name of cell, eg. C10 or { c: 1, r :1 }
514 * @param {Value} value to put in cell..
515 * @param {ValueType} type of value
516 * @param {ValueFormat} value format of cell
518 * Cells should exist at present, we do not make them up...
522 set : function(cell, v, vt, vf) {
524 var cs= typeof(cell) == 'string' ? this.toRC(cell) : cell;
525 //Roo.log( cs.r+ ',' + cs.c + ' = '+ v);
526 // need to generate clell if it doe
527 if (typeof(this.grid[cs.r]) == 'undefined') {
528 Roo.log('no row:' + cell);
529 this.grid[cs.r] = []; // create a row..
532 if (typeof(this.grid[cs.r][cs.c]) == 'undefined') {
533 Roo.log('cell not defined:' + cell);
534 this.createCell(cs.r,cs.c);
536 if (typeof(this.grid[cs.r][cs.c].dom) == 'undefined') {
537 Roo.log('no default content for cell:' + cell);
538 this.createCell(cs.r,cs.c);
541 this.grid[cs.r][cs.c].value= v;
542 this.grid[cs.r][cs.c].dom.textContent= v;
543 if (typeof(vt) != 'undefined') {
544 this.grid[cs.r][cs.c].valueType = vt;
545 this.grid[cs.r][cs.c].dom.setAttribute('ValueType', vt);
546 if (vt === '' || vt === false) { // value type is empty for formula's
547 this.grid[cs.r][cs.c].dom.removeAttribute('ValueType');
550 if (typeof(vf) != 'undefined' && vf !== false) {
551 this.grid[cs.r][cs.c].valueFormat = vf;
552 this.grid[cs.r][cs.c].dom.setAttribute('ValueFormat', vf);
553 if (vf === '' || vf === false) { // value type is empty for formula's
554 this.grid[cs.r][cs.c].dom.removeAttribute('ValueFormat');
561 copyRow : function(src, dest) {
565 // Roo.log('create Row' + dest);
566 if (typeof(this.grid[dest]) == 'undefined') {
571 for (var c = 0; c < this.cmax; c++) {
573 this.copyCell({ r: src, c: c } , { r: dest, c: c});
576 this.rmax = Math.max(this.rmax, dest +1);
582 createCell: function(r,c)
584 //<gnm:Cell Row="6" Col="5" ValueType="60">Updated</gnm:Cell>
585 var nc = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Cell');
586 this.cellholder.appendChild(nc);
587 var lb = this.doc.createTextNode("\n");// add a line break..
588 this.cellholder.appendChild(lb);
590 nc.setAttribute('Row', new String(r));
591 nc.setAttribute('Col', new String(c));
592 nc.setAttribute('ValueType', '60');
595 this.grid[r][c] = Roo.applyIf({
609 copyCell : function(src, dest)
611 var old = this.grid[src.r][src.c];
613 if ((old.c != src.c) || (old.r != src.r)) {
614 // only really works on horizonatal merges..
616 this.grid[dest.r][dest.c] = this.grid[desc.r][old.c]; // let's hope it exists.
621 var nc = Roo.apply({}, this.grid[src.r][src.c]);
624 if (typeof(old.dom) == 'undefined') {
625 Roo.log("No cell to copy for " + Roo.encode(src));
628 this.grid[dest.r][dest.c] = nc;
629 nc.dom = old.dom.cloneNode(true);
630 nc.dom.setAttribute('Row', dest.r);
631 nc.dom.setAttribute('Cell', dest.c);
632 nc.dom.textContent = '';
633 old.dom.parentNode.appendChild(nc.dom);
634 if (!old.styles || !old.styles.length) {
641 Roo.each(old.styles, function(s) {
642 // try and extend existing styles..
643 var er = s.getAttribute('endRow') * 1;
644 var ec = s.getAttribute('endCol') * 1;
647 s.setAttribute('endRow', dest.r + 1);
650 s.setAttribute('endCol', dest.c + 1);
652 /*var ns = s.cloneNode(true);
653 s.parentNode.appendChild(ns);
654 ns.setAttribute('startCol', dest.c);
655 ns.setAttribute('startRow', dest.r);
656 ns.setAttribute('endCol', dest.c + 1);
657 ns.setAttribute('endRow', dest.r +1);
666 * Set the value of a cell..
667 * @param {String} cell name of cell, eg. C10
668 * @param {Value} value to put in cell..
670 * Cells should exist at present, we do not make them up...
673 applyData : function(data)
676 data = data || this.data;
677 for (var r = 0; r < this.rmax;r++) {
678 if (typeof(this.grid[r]) == 'undefined') continue;
679 for (var c = 0; c < this.cmax;c++) {
680 if (typeof(this.grid[r][c]) == 'undefined') {
683 if (!this.grid[r][c].value.length
684 || !this.grid[r][c].value.match(/\{/)) {
688 var x = new Roo.Template({ html: this.grid[r][c].value });
690 var res = x.applyTemplate(data);
691 //Roo.log("set " + r + "," + c + ":"+res)
692 this.set({ r: r, c: c}, x.applyTemplate(data));
694 // Roo.log(e.toString());
704 readTableData : function(table)
706 // read the first row.
707 var tds = Roo.get(table).select('tr').item(0).select('td');
709 tds.each(function(td) {
710 var cs = td.dom.getAttribute('colspan');
711 cs = cs ? cs * 1 : 1;
714 var tr = document.createElement('tr');
715 table.appendChild(tr);
717 for (i =0; i < nc; i++) {
718 ar[i] = document.createElement('td');
719 tr.appendChild(ar[i]);
722 var ret = { cols : nc, pos : {} };
723 for (i =0; i < nc; i++) {
724 ret.pos[ Roo.get(ar[i]).getLeft()] =i;
726 table.removeChild(tr);
735 * Import a table and put it into the spreadsheet
736 * @param {HTMLTable} datagrid dom element of html table.
737 * @param {Number} xoff X offset to start rendering to
738 * @param {Number} yoff Y offset to start rendering to
742 importTable : function (datagrid, xoff,yoff)
745 Roo.log("Error table not found!?");
752 var table_data = this.readTableData(datagrid);
754 var cleanHTML = function (str) {
757 ret = ret.replace(/ /g,'.');
758 ret = ret.replace(/\n/g,'.');
759 ret = ret.replace(/\r/g,'.');
761 while (-1 != (i = ret.indexOf(unescape('%A0')))) {
762 ret = ret.substring(0,i) + ' ' + ret.substring(i+1,str.length);
768 // <cell col="A" row="1">Test< / cell>
769 // <cell col="B" row="2" type="Number" format="test1">30< / cell>
771 var rows = datagrid.getElementsByTagName('tr');
772 //alert(rows.length);
775 for(var row=0;row<rows.length;row++) {
776 //var style = document.defaultView.getComputedStyle(rows[row], "");
778 //if (rows[row].getAttribute('xls:height')) {
779 // this.setRowHeight(row+y_offset, 0 + rows[row].getAttribute('xls:height'));
781 // this.setRowHeight(row+y_offset, 0 + style.height.replace(/[^0-9.]+/g,''));
785 var cols = rows[row].getElementsByTagName('td');
788 for(var col=0;col < cols.length; col++) {
791 //var colat = col + coloffset;
794 var colspan = cols[col].getAttribute('colspan');
795 colspan = colspan ? colspan *1 : 1;
797 var rowspan = cols[col].getAttribute('rowspan');
798 rowspan = rowspan ? rowspan * 1 : 1;
800 var realcol = table_data.pos[ Roo.get(cols[col]).getLeft() ];
804 if (colspan > 1 || rowspan > 1) {
806 //row + yoff, c : col + xoff + coloffset
810 realcol+ xoff + (colspan -1),
811 row + yoff + (rowspan -1)
817 // set the style first..
818 this.parseHtmlStyle( cols[col], row + yoff, realcol + xoff + coloffset , colspan, rowspan);
820 if (!cols[col].childNodes.length) {
830 var xlstype = cols[col].getAttribute('xls:type');
838 vt = 40; // float!!!!
839 if (cols[col].getAttribute('xls:floatformat')) {
840 vf = cols[col].getAttribute('xls:floatformat');
846 //ValueFormat="d/m/yyyy" 38635
848 if (cols[col].getAttribute('xls:dateformat')) {
849 vf= cols[col].getAttribute('xls:dateformat');
861 if (!cols[col].childNodes[0].nodeValue) {
865 if (!cols[col].childNodes[0].nodeValue.replace(/^\s*|\s*$/g,"").length) {
870 var cell_value_text = cleanHTML(cols[col].childNodes[0].nodeValue);
872 if (cols[col].getAttribute('xls:percent')) {
873 cell_value_text = '' + ((cell_value_text * 1) / 100);
876 if (cell_value_text.length && (vt == 30) && xlstype == 'date') {
877 var bits = cell_value_text.split(/-/);
878 var cur = new Date(bits[0],bits[1]-1,bits[2]);
879 cell_value_text = '' + Math.round((cur.getTime() - Date.UTC(1899,11,30)) / (24 * 60 * 60 * 1000));
884 if (cols[col].getAttribute('xls:formula')) {
885 var s = cols[col].getAttribute('xls:formula');
887 cell_value_text = s.replace(/#row#/g,(row + yoff + 1));
889 this.set({ r: row + yoff, c : realcol + xoff }, cell_value_text, vt, vf);
897 this.rowOffset += rows.length;
903 parseHtmlStyle : function(dom, row, col, cspan, rspan) {
905 function toCol (rgb) {
906 var ar = rgb.replace(/rgb\(/, '').replace(/\)/, '').replace(/ /, '').split(',');
908 Roo.each(ar, function(c) {
910 rcs.push((c*256).toString(16)) ;
912 return rcs.join(':');
916 var el = Roo.get(dom);
918 'text-align' : function(ent,v) {
919 ent['HAlign'] = { 'left' : '1', 'center' : '8' , 'right' : '4' }[v] || '1';
921 'vertical-align': function(ent,v) {
922 ent['VAlign'] = { 'top' : '1', 'middel' : '8' , 'bottom' : '4' }[v] || '1';
925 'color': function(ent,v) {
926 ent['Fore'] = toCol(v);
928 'background-color' : function(ent,v) {
929 ent['Back'] = toCol(v);
946 Back:"FFFF:FFFF:FFFF",
947 PatternColor:"0:0:0",
952 var val = el.getStyle(k);
953 if (!val || !val.length) {
962 'font-size' : function(ent,v) {
963 ent['FontUnit'] = v.replace(/px/, '');
965 'font-weight' : function(ent,v) {
966 if (v != 'bold') return;
969 //FontItalic : function(ent,v) {
970 // if (v*0 < 1) return;
971 // //ent['font-weight'] = 'bold';
985 var val = el.getStyle(k);
986 if (!val || !val.length) {
991 var font = el.getStyle('font-family') || 'Sans';
995 /// -- now create elements..
997 var objs = this.sheet.getElementsByTagNameNS('*','Styles')[0];
999 //<gnm:StyleRegion startCol="0" startRow="0" endCol="255" endRow="65535"
1000 var sr = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleRegion');
1001 objs.appendChild(sr);
1002 sr.setAttribute('startCol', col);
1003 sr.setAttribute('endCol', col+ colspan-1);
1004 sr.setAttribute('startRow', row);
1005 sr.setAttribute('endRow', row +rowspan -1);
1008 var st = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Style');
1010 // do we need some defaults..
1013 st.setAttribute(k, ent[k]);
1016 var fo = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Font');
1018 // do we need some defaults..
1019 for(var k in fent) {
1020 fo.setAttribute(k, ent[k]);
1022 fo.textContent = font;
1026 Roo.each(['top','left','bottom','right'], function(p) {
1027 var w = el.getStyle('border-' + p + '-width').replace(/px/, '');
1028 if (!w || !w.length || (w*1) < 1) {
1032 sb= this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleBorder');
1034 var be = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:' + p[0].toUpperCase() + p.substring(1));
1035 be.setAttribute('Style', '1');
1036 be.setAttribute('Color', '0:0:0'); // fixme..
1040 // start adding them all together..
1055 * write an image (needs base64 data to write it)
1058 * @param {Number} row row to put it in (rows start at 0)
1059 * @param {Number} col column to put it in
1060 * @param {Number} data the base64 description of the images
1061 * @param {Number} width image width
1062 * @param {Number} width image height
1067 writeImage : function (row, col, data, width, height)
1070 // our default height width is 50/50 ?!
1071 //console.log('w='+width+',height='+height);
1077 var objs = this.sheet.getElementsByTagNameNS('*','Objects')[0];
1078 var soi = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:SheetObjectImage');
1080 //<gmr:SheetObjectImage
1081 // ObjectBound="A3:J8"
1082 // ObjectOffset="0.375 0.882 0.391 0.294"
1083 // ObjectAnchorType="16 16 16 16"
1085 // crop-top="0.000000"
1086 // crop-bottom="0.000000"
1087 // crop-left="0.000000"
1088 // crop-right="0.000000">
1091 //alert(gnumeric_colRowToName(row,col));
1093 // this is where we really have fun!!!...
1094 // since our design currently assumes the height is enough to fit
1095 // stuff in, we only really need to work out how wide it has to be..
1097 // note we should probably use centralized calcs if it fits in the first cell!
1099 // step 1 - work out how many columns it will span..
1100 // lets hope the spreadsheet is big enought..
1103 for ( endcol=col;endcol <100; endcol++) {
1104 if (!this.colInfo[endcol]) {
1105 this.colInfo[endcol] = 100; // eak fudge
1107 colwidth += this.colInfo[endcol];
1108 if (colwidth > width) {
1114 soi.setAttribute('ObjectBound',
1115 //gnumeric_colRowToName(row,col) + ':' + gnumeric_colRowToName(row+1,col+1));
1116 this.RCtoCell(row,col) + ':' + this.RCtoCell(row,endcol));
1118 var ww = 0.01; // offset a bit...
1121 var ww2 = 1 - ((colwidth - width) / this.colInfo[endcol]);
1124 var offset_str = ww + ' ' + hh + ' ' + ww2 + ' '+hh2;
1125 //console.log(offset_str );
1126 //alert(offset_str);
1127 soi.setAttribute('ObjectOffset', offset_str);
1128 soi.setAttribute('ObjectAnchorType','16 16 16 16');
1129 soi.setAttribute('Direction','17');
1130 soi.setAttribute('crop-top','0.000000');
1131 soi.setAttribute('crop-bottom','0.000000');
1132 soi.setAttribute('crop-left','0.000000');
1133 soi.setAttribute('crop-right','0.000000');
1134 // <Content image-type="jpeg" size-bytes="3900">...... < / Content>
1135 var content = this.doc.createElement('Content');
1136 content.setAttribute('image-type','jpeg');
1139 content.setAttribute('size-bytes',data.length);
1140 content.textContent = data;
1141 soi.appendChild(content);
1142 objs.appendChild(soi);
1144 //< /gnm:SheetObjectImage>
1151 * Merge cells in the spreadsheet. (does not check if existing merges exist..)
1153 * @param {Number} col1 first column
1154 * @param {Number} row1 first row
1155 * @param {Number} col2 to column
1156 * @param {Number} row2 to row
1159 mergeRegion : function (col1,row1,col2,row2)
1161 var cell = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Merge');
1162 //if (col1 > 50|| col2 > 50) { // do not merge cols off to right?
1166 cell.textContent = this.RCtoCell(row1,col1) + ':' + this.RCtoCell(row2,col2)
1168 //var merges = this.gnumeric.getElementsByTagNameNS('*','MergedRegions');
1169 var merges = this.sheet.getElementsByTagNameNS('*','MergedRegions');
1170 if (!merges || !merges.length) {
1171 merges = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:MergedRegions');
1172 var sl = this.sheet.getElementsByTagNameNS('*','SheetLayout')[0];
1173 this.sheet.insertBefore(merges,sl);
1177 merges.appendChild(cell);
1182 * Sets the height of a row.
1184 * @param {Number} r the row to set the height of. (rows start at 0)
1185 * @param {Number} height (in pixels)
1187 setRowHeight : function (r,height)
1190 //<gmr:Rows DefaultSizePts="12.75">
1191 // <gmr:RowInfo No="2" Unit="38.25" MarginA="0" MarginB="0" HardSize="1"/>
1193 var rows = this.sheet.getElementsByTagNameNS('*','Rows')[0]; // assume this exists..
1194 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:RowInfo');
1195 // assume we have no rows..
1196 ri.setAttribute('No', r-1);
1197 ri.setAttribute('Unit', height);
1198 ri.setAttribute('MarginA', 0);
1199 ri.setAttribute('MarginB', 0);
1200 ri.setAttribute('HardSize', 1);
1201 rows.appendChild(ri);
1206 * Set the sheet name.
1207 * @param {String} title for sheet
1209 setSheetName : function(name,sheet)
1213 <gnm:SheetNameIndex>
1214 <gnm:SheetName>Sheet1</gnm:SheetName>
1215 <gnm:SheetName>Sheet2</gnm:SheetName>
1216 <gnm:SheetName>Sheet3</gnm:SheetName>
1217 </gnm:SheetNameIndex>
1219 // has to set sheet name on index and body..
1222 var sheetnames = this.doc.getElementsByTagNameNS('*','SheetName');
1223 if (sheet >= sheetnames.length) {
1225 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1227 sheetnames = this.doc.getElementsByTagNameNS('*','Sheet');
1228 sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
1229 var sn = this.doc.getElementsByTagNameNS('*','Sheet')[sheet];
1230 var cls = sn.getElementsByTagNameNS('*','Cells')[0]
1231 while (cls.childNodes.length) {
1232 cls.removeChild(cls.firstChild);
1237 var sheetn = this.doc.getElementsByTagNameNS('*','SheetName')[sheet];
1238 sheetn.textContent = name;
1239 var sheetb = this.doc.getElementsByTagNameNS('*','Sheet')[sheet].getElementsByTagNameNS('*','Name')[0];
1240 sheetb.textContent = name;
1241 this.parseDoc(sheet);
1249 * Set the column width
1250 * @param {Number} column number (starts at '0')
1251 * @param {Number} width size of column
1253 setColumnWidth : function(column, width)
1257 if (typeof(this.colInfoDom[column]) == 'undefined') {
1258 var cols = this.doc.getElementsByTagNameNS('*','Cols')[0];
1259 var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:ColInfo');
1260 ri.setAttribute('No', column);
1261 ri.setAttribute('Unit', width);
1262 ri.setAttribute('MarginA', 2);
1263 ri.setAttribute('MarginB', 2);
1264 ri.setAttribute('HardSize', 1);
1265 cols.appendChild(ri);
1266 this.colInfo[column] = width;
1267 this.colInfoDom[column] = ri;
1270 this.colInfoDom[column].setAttribute('Unit', width);
1280 * Convert spreadsheet into a HTML table.
1286 function calcWidth(sc, span)
1289 for(var i =sc; i< sc+span;i++) {
1295 var grid = this.grid;
1296 // lets do a basic dump..
1297 var out = '<table style="table-layout:fixed;" cellpadding="0" cellspacing="0">';
1298 for (var r = 0; r < this.rmax;r++) {
1299 out += '<tr style="height:'+this.rowInfo[r]+'px;">';
1300 for (var c = 0; c < this.cmax;c++) {
1301 var g = (typeof(grid[r][c]) == 'undefined') ? defaultCell : grid[r][c];
1303 if (typeof(g.cls) =='undefined') g.cls = [];
1304 var w= calcWidth(c,g.colspan);
1305 out+=String.format('<td colspan="{0}" rowspan="{1}" class="{4}"><div style="{3}">{2}</div></td>',
1306 g.colspan, g.rowspan, g.value,
1307 'overflow:hidden;' +
1310 'text-overflow:ellipsis;' +
1311 'white-space:nowrap;',
1321 return out+'</table>';
1328 * @param {String} name filename to downlaod (without xls)
1329 * @param {String} callback (optional) - callback to call after callback is complete.
1331 download : function(name,callback)
1333 name = name || "Missing_download_filename";
1335 if (this.downloadURL && this.downloadURL.charAt(this.downloadURL .length-1) != '/') {
1336 this.downloadURL += '/';
1339 var ser = new XMLSerializer();
1340 var x = new Pman.Download({
1343 xml : ser.serializeToString(this.doc),
1344 format : 'xls', //xml
1348 url : (this.downloadURL || (baseURL + '/GnumericToExcel/')) + name + '.xls',
1349 success : function() {
1350 Roo.MessageBox.alert("Alert", "File should have downloaded now");