Skip to content

Commit

Permalink
Formulas with test
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusmr13 committed Jul 14, 2016
1 parent c632190 commit 8a11278
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 19 deletions.
8 changes: 4 additions & 4 deletions test/annotationsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ var jsonMock = {
}
};

describe('QuickDrive functions', function () {
describe('get new sheet', function () {
it('it should return sheet with text replaced', function () {
describe('Annotations tests', function () {
describe('test specific annotations', function () {
it('all properties annotation', function () {
var QuickDriveMock = new QuickDriveConstructor(DriveApp(), SpreadsheetApp(matrixMockWithReplaceAnnotations));
var file = QuickDriveMock.processSheet(jsonMock);
var range = file.sheet.getRange(1,1,2,2).getCells();
var range = file.sheet._processFormulas().getRange(1,1,2,2).getCells();
var firstCell = range[0][0];
var secondCell = range[0][1];
var thirdCell = range[1][0];
Expand Down
6 changes: 3 additions & 3 deletions test/fullTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ describe('QuickDrive functions', function () {
var file = QuickDriveMock.processSheet(jsonMock);
assert.equal(JSON.stringify([
['My cool header', 'My random text', 'another random text', ''],
['Matheus', 'Martins do Rego', 20, 'My user'],
['Matheus', 'Martins do Rego', '20', 'My user'],
['12345-678', 'Campinas', 'São Paulo', 'Brasil'],
['=SUM(10,30)', '','',''],
['40', '','',''],
['Languages that he likes', '', '', ''],
['', 'Java', '', ''],
['', 'JavaScript', '', ''],
['', 'CSS', '', ''],
['', 'HTML', '', ''],
['', 'Python', '', 'text that will stay on last line']
]), JSON.stringify(file.sheet.getRange(1, 1, file.sheet.getMaxRows(), file.sheet.getMaxColumns()).getValues()));
]), JSON.stringify(file.sheet._processFormulas().getRange(1, 1, file.sheet.getMaxRows(), file.sheet.getMaxColumns()).getValues()));
});
});
});
18 changes: 15 additions & 3 deletions test/mock/SpreadsheetApp/Cell.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Cell = function (firstValue) {
var value = firstValue || '',
formula = '',
backgroundColor = 'default',
borderStyle = 'default',
color = 'default',
Expand All @@ -9,13 +10,24 @@ var Cell = function (firstValue) {
var links = {};

this.getValue = function () {
return value;
return value + '';
};
this.getNumberValue = function() {
if (isNaN(value)) {
return 0;
} else {
return parseInt(value || 0);
}
};
this.setValue = function (newValue) {
value = newValue;
};
this.setFormula = function (newValue) {
value = newValue;
this.getFormula = function () {
return formula;
};
this.setFormula = function (newFormula) {
value = '';
formula = newFormula;
};
this.getBackground = function () {
return backgroundColor;
Expand Down
39 changes: 39 additions & 0 deletions test/mock/SpreadsheetApp/Formulas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var Formulas = function (sheet) {

return {
SUM: function (params) {
var sum = 0,
range = sheet.getRange(1,1,sheet.getMaxRows(),sheet.getMaxColumns());

if (params) {
var oldParams = params;
params = []
for (var i =0;i<oldParams.length;i++) {
if (!isNaN(oldParams[i])) {
sum += parseInt(oldParams[i]);
} else {
params.push(oldParams[i]);
}
}
}

if (!params.length) {
return sum;
}
if (typeof params == 'string' && params && isNaN(params[0])) {
var parts = number1.split(':');
range = sheet.getRange(params[0], params[1]);
}
var matrix = range.getCells();

for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j].getNumberValue();
}
}
return sum;
}
}
};

exports.Formulas = Formulas;
36 changes: 27 additions & 9 deletions test/mock/SpreadsheetApp/Sheet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Range = require('./Range').Range;
var Cell = require('./Cell').Cell;
var Formulas = require('./Formulas').Formulas;
var Sheet = function (myMatrix) {
var matrix = myMatrix || [
[]
Expand Down Expand Up @@ -58,15 +59,6 @@ var Sheet = function (myMatrix) {
newMatrix.push(matrix[i]);
}

// for (var i =0; i < newMatrix.length;i++) {
// var a = '';
// for(var j =0;j < newMatrix[i].length;j++) {
// a = a + ' ' + newMatrix[i][j].getValue();
// }
// console.info(a);
// }
// console.info(newMatrix.length);
// console.info(newMatrix[0].length);
matrix = newMatrix;
};

Expand All @@ -77,6 +69,32 @@ var Sheet = function (myMatrix) {
this.getMaxColumns = function () {
return matrix[0].length;
};

this._processFormula = function (cell, formulas) {
if (cell.getFormula() && !cell.getValue()) {
cell.setFormula(cell.getFormula().trim());
var parts = cell.getFormula().split('('),
formula = parts[0].substring(1),
params = parts[1].substring(0, parts[1].length - 1).split(','),
formulaFunction = formulas[formula];
console.info(parts);
if (!formulaFunction) {
throw new Error('Not testable function: ' + formula);
} else {
cell.setValue(formulaFunction(params));
}
}
};

this._processFormulas = function () {
var formulas = Formulas(this);
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix[i].length; j++) {
this._processFormula(matrix[i][j], formulas);
}
}
return this;
};
};

exports.Sheet = Sheet;

0 comments on commit 8a11278

Please sign in to comment.