Permalink
Browse files

fixed #44

  • Loading branch information...
myliang committed Feb 9, 2019
1 parent 7f1b7da commit 8bd8494da7a1f3dfb28a2639d51fbd6618734018
Showing with 21 additions and 15 deletions.
  1. +9 −4 src/cell.js
  2. +3 −2 src/component/sheet.js
  3. +1 −1 src/component/table.js
  4. +6 −6 test/cell_test.js
  5. +2 −2 test/format_test.js
@@ -162,14 +162,19 @@ const evalSuffixExpr = (srcStack, formulaMap, cellRender) => {
return stack[0];
};

const cellRender = (src, formulaMap, getCellText) => {
// console.log(':::::::::::::src:', src);
const cellRender = (sri, sci, src, formulaMap, getCellText) => {
// const srcExpr = alphabet.stringAt(sci) + (sri + 1);
// console.log(':::::::::::::src:', src, srcExpr);
if (src[0] === '=') {
const stack = infixExprToSuffixExpr(src.substring(1));
// if (stack.includes(srcExpr)) return '';
// console.log('suffixExpr:', stack);
if (stack.length <= 0) return src;
const cb = (x, y) => cellRender(getCellText(x, y - 1), formulaMap, getCellText);
return evalSuffixExpr(stack, formulaMap, cb);
// const cb = (x, y) => cellRender(sri, sci, getCellText(x, y - 1), formulaMap, getCellText);
return evalSuffixExpr(stack, formulaMap, (x, y) => {
const cellText = (sri === y - 1 && sci === x) ? 0 : getCellText(x, y - 1);
return cellRender(sri, sci, cellText, formulaMap, getCellText);
});
}
return src;
};
@@ -496,7 +496,7 @@ function sheetInitEvents() {
bind(window, 'keydown', (evt) => {
if (!this.focusing) return;
const keyCode = evt.keyCode || evt.which;
const key = evt.key;
const { key } = evt;
// console.log('keydown.evt: ', keyCode);
if (evt.ctrlKey) {
// const { sIndexes, eIndexes } = selector;
@@ -578,7 +578,7 @@ function sheetInitEvents() {
evt.preventDefault();
break;
case 13:
//shift + enter, moves up
// shift + enter, moves up
selectorMove.call(this, false, 'up');
evt.preventDefault();
break;
@@ -621,6 +621,7 @@ function sheetInitEvents() {
case 8: // backspace
insertDeleteRowColumn.call(this, 'delete-cell-text');
evt.preventDefault();
break;
default:
break;
}
@@ -38,7 +38,7 @@ function renderCell(rindex, cindex) {
draw.rect(dbox);
if (cell !== null) {
// render text
let cellText = _cell.render(cell.text || '', data.formulam, (y, x) => (cellmm[x] && cellmm[x][y] && cellmm[x][y].text) || '');
let cellText = _cell.render(rindex, cindex, cell.text || '', data.formulam, (y, x) => (cellmm[x] && cellmm[x][y] && cellmm[x][y].text) || '');
if (cell.format) {
// console.log(data.formatm, '>>', cell.format);
cellText = data.formatm[cell.format].render(cellText);
@@ -38,14 +38,14 @@ describe('infixExprToSuffixExpr', () => {

describe('cell', () => {
describe('.render()', () => {
it('should return 1 + 3 + 3 + 7 + 50 + 21 when the value is =SUM(A1,B2, C1, C5) + 50 + B20', () => {
assert.equal(cell.render('=SUM(A1,B2, C1, C5) + 50 + B20', formulas(), (x, y) => x + y), 1 + 3 + 3 + 7 + 50 + 21);
it('should return 0 + 2 + 2 + 6 + 49 + 20 when the value is =SUM(A1,B2, C1, C5) + 50 + B20', () => {
assert.equal(cell.render(0, 0, '=SUM(A1,B2, C1, C5) + 50 + B20', formulas(), (x, y) => x + y), 0 + 2 + 2 + 6 + 50 + 20);
});
it('should return 50 + 21 when the value is =50 + B20', () => {
assert.equal(cell.render('=50 + B20', formulas(), (x, y) => x + y), 50 + 21);
it('should return 50 + 20 when the value is =50 + B20', () => {
assert.equal(cell.render(0, 0, '=50 + B20', formulas(), (x, y) => x + y), 50 + 20);
});
it('should return 2 + 500 - 21 when the value is =AVERAGE(A1:A3) + 50 * 10 - B20', () => {
assert.equal(cell.render('=AVERAGE(A1:A3) + 50 * 10 - B20', formulas(), (x, y) => x + y), (2 + 500) - 21);
it('should return 1 + 500 - 20 when the value is =AVERAGE(A1:A3) + 50 * 10 - B20', () => {
assert.equal(cell.render(0, 0, '=AVERAGE(A1:A3) + 50 * 10 - B20', formulas(), (x, y) => x + y), (1 + 500) - 20);
});
});
});
@@ -29,10 +29,10 @@ describe('format', () => {
assert.equal(gformats.percent.render('50.456'), '50.456%');
});
it('RMB: should return ¥1,200.33 when the value is 1200.333', () => {
assert.equal(gformats.RMB.render('1200.333'), '¥1,200.33');
assert.equal(gformats.rmb.render('1200.333'), '¥1,200.33');
});
it('USD: should return $1,200.33 when the value is 1200.333', () => {
assert.equal(gformats.USD.render('1200.333'), '$1,200.33');
assert.equal(gformats.usd.render('1200.333'), '$1,200.33');
});
});
});

0 comments on commit 8bd8494

Please sign in to comment.