Browse Source

code sync

TonyKang 8 years ago
parent
commit
51b274ae7e
3 changed files with 95 additions and 5 deletions
  1. 45 5
      modules/reports/util/rpt_excel_util.js
  2. 12 0
      public/stringUtil.js
  3. 38 0
      test/demo/stringTest.js

+ 45 - 5
modules/reports/util/rpt_excel_util.js

@@ -3,6 +3,7 @@
  */
  */
 var JV = require('../rpt_component/Jpc_ValueDefine');
 var JV = require('../rpt_component/Jpc_ValueDefine');
 var fs = require('fs');
 var fs = require('fs');
+var strUtil = require('../../../public/stringUtil');
 var jpcCmnHelper = require('../rpt_component/helper/Jpc_Helper_Common');
 var jpcCmnHelper = require('../rpt_component/helper/Jpc_Helper_Common');
 var DPI = jpcCmnHelper.getScreenDPI()[0];
 var DPI = jpcCmnHelper.getScreenDPI()[0];
 const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
 const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
@@ -124,10 +125,10 @@ function writeTheme(){
     var rst = fs.readFileSync('./excel_base_files/theme1.xml', 'utf8', 'r');
     var rst = fs.readFileSync('./excel_base_files/theme1.xml', 'utf8', 'r');
     return rst;
     return rst;
 }
 }
-function writeStyles(pageData){
+function writeStyles(styleList){
     //
     //
 }
 }
-function writeSharedString(pageData){
+function writeSharedString(sharedStrList){
     //
     //
 }
 }
 function writeSheets(pageData){
 function writeSheets(pageData){
@@ -224,7 +225,9 @@ function writeSheet(sheetData, sharedStrList, styleList){
     };
     };
     private_setSheetData = function(){
     private_setSheetData = function(){
         rst.push('<sheetData>');
         rst.push('<sheetData>');
-        var spanX = xPos.length - 1, cellIdx = 0, h = 0, hasMoreCols = true;
+        var spanX = xPos.length - 1, cellIdx = 0, h = 0,
+            hasMoreCols = true, nextColIdx = -1,
+            nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
         for (var i = 0; i < yPos.length - 1; i++) {
         for (var i = 0; i < yPos.length - 1; i++) {
             if (i === 0) {
             if (i === 0) {
                 h = 1.0 * (yPos[i]) / DPI;
                 h = 1.0 * (yPos[i]) / DPI;
@@ -235,10 +238,47 @@ function writeSheet(sheetData, sharedStrList, styleList){
             rst.push('<row r="' + (i+1) + '" spans="1:' + spanX + '" ht="' + h + '" customHeight="1">');
             rst.push('<row r="' + (i+1) + '" spans="1:' + spanX + '" ht="' + h + '" customHeight="1">');
             //then put the cells of this row
             //then put the cells of this row
             var colIdxStr = '';
             var colIdxStr = '';
+            hasMoreCols = true;
+            while (nextRowIdx < i) {
+                if (cellIdx >= sheetData.cells.length || nextRowIdx > i) {
+                    break;
+                } else {
+                    cellIdx++;
+                    nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
+                }
+            }
+            if (nextRowIdx > i) {
+                hasMoreCols = false;
+            }
+            nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
             for (var j = 0; j < xPos.length; j++) {
             for (var j = 0; j < xPos.length; j++) {
                 colIdxStr = private_getCellIdxStr(j);
                 colIdxStr = private_getCellIdxStr(j);
                 if (hasMoreCols) {
                 if (hasMoreCols) {
-                    var ridx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
+                    if (nextColIdx == j) {
+                        var styleIdx = private_getStyleIdx(sheetData.cells[cellIdx]);
+                        if (strUtil.isEmptyString(sheetData.cells[cellIdx][JV.PROP_VALUE])) {
+                            rst.push('<c r="' + colIdxStr + (i+1) + '" s="' + styleIdx + '"/>');
+                            //should setup the right style instead!
+                        } else {
+                            var valIdx = private_getSharedStrIdx(sheetData.cells[cellIdx][JV.PROP_VALUE]);
+                            rst.push('<c r="' + colIdxStr + (i+1) + '" s="' + styleIdx + '">');
+                            rst.push('<v>' + valIdx + '</v>');
+                            rst.push('</c>');
+                        }
+                        cellIdx++;
+                        nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
+                        if (nextRowIdx > i) {
+                            hasMoreCols = false;
+                        } else {
+                            nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
+                        }
+                    } else if (nextColIdx < 0) {
+                        //impossible!
+                        console.log('has abnormal case!');
+                        hasMoreCols = false;
+                    } else {
+                        rst.push('<c r="' + colIdxStr + (i+1) + '" s="1"/>');
+                    }
                 } else {
                 } else {
                     rst.push('<c r="' + colIdxStr + (i+1) + '" s="1"/>');
                     rst.push('<c r="' + colIdxStr + (i+1) + '" s="1"/>');
                 }
                 }
@@ -257,7 +297,7 @@ function writeSheet(sheetData, sharedStrList, styleList){
     rst.push('<selection sqref="A1:' + colStr + '1"/>');
     rst.push('<selection sqref="A1:' + colStr + '1"/>');
     rst.push('</sheetView></sheetViews>');
     rst.push('</sheetView></sheetViews>');
     rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
     rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
-    private_setCols
+    private_setCols();
     private_setSheetData();
     private_setSheetData();
     private_setMergedCells();
     private_setMergedCells();
     rst.push('<phoneticPr fontId="1" type="noConversion"/>');
     rst.push('<phoneticPr fontId="1" type="noConversion"/>');

+ 12 - 0
public/stringUtil.js

@@ -0,0 +1,12 @@
+module.exports = {
+    isEmptyString: function(str) {
+        var rst = false;
+        if (str == null || str == undefined) {
+            rst = true;
+        } else if (typeof str) {
+            var reg = /^\s*$/;
+            rst = reg.test(str);
+        }
+        return rst;
+    }
+}

+ 38 - 0
test/demo/stringTest.js

@@ -0,0 +1,38 @@
+/**
+ * Created by Tony on 2017/4/7.
+ */
+
+var test = require('tape');
+
+test('some string test cases', function (t) {
+    var reg = /^\s*$/;
+    var foo = "   ";
+    t.equal(reg.test(foo), true);
+    foo = null;
+    t.equal(reg.test(foo), true);
+    foo = undefined;
+    t.equal(reg.test(foo), true);
+    foo = "";
+    t.equal(reg.test(foo), true);
+    foo = 0;
+    t.equal(reg.test(foo), true);
+    t.end();
+});
+
+test('check if string type', function(t){
+    var foo = "";
+    t.equal(typeof foo, 'string');
+    foo = " ";
+    t.equal(typeof foo, 'string');
+    foo = 0;
+    t.equal(typeof foo, 'number');
+    foo = true;
+    t.equal(typeof foo, 'boolean');
+    foo = {};
+    t.equal(typeof foo, 'object');
+    foo = function(){};
+    t.equal(typeof foo, 'function');
+    foo = [];
+    t.equal(Array.isArray(foo), true);
+    t.end();
+})