Skip to content

Commit

Permalink
Formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusmr13 committed Jul 10, 2016
1 parent e40c109 commit 6b7fc30
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/SheetsTemplater.gs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
QuickDrive.annotationFunctions = {
REPLACE_TEXT: replaceValue,
FOR_EACH: processForEach,
INSERT_FORMULA: insertFormula,
NONE: function (properties) {
return;
}
};
var annotationType = {
'=': QuickDrive.annotationFunctions.REPLACE_TEXT,
'~': QuickDrive.annotationFunctions.FOR_EACH
'~': QuickDrive.annotationFunctions.FOR_EACH,
'#': QuickDrive.annotationFunctions.INSERT_FORMULA
};
this._config = {
folderId: '0ByQE0cDEoa0qLUlPU21xVzNqZVk',
Expand Down Expand Up @@ -174,6 +176,15 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
sheet.getRange(row, col).setValue(getValueOnJson(json, command.substring(2, command.length - 1)));
};

function insertFormula (properties) {
var row = properties.i + 1,
col = properties.j + 1,
command = properties.values[properties.i][properties.j],
sheet = properties.sheet,
json = properties.json;
sheet.getRange(row, col).setFormula('=' + getValueOnJson(json, command.substring(2, command.length - 1)));
};

QuickDrive.processCell = function (properties) {
var cellValue = properties.values[properties.i][properties.j];
var annotationFunction = QuickDrive.getAnnotationType(cellValue)(properties);
Expand Down
6 changes: 5 additions & 1 deletion test/mock/SpreadsheetApp/Cell.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Cell = function (firstValue) {
var value = firstValue || "",
var value = firstValue || '',
backgroundColor = 'white',
borderStyle = 'SOLID';

Expand All @@ -8,9 +8,13 @@ var Cell = function (firstValue) {
this.setValue = function (newValue) {
value = newValue;
};
this.setFormula = function (newValue) {
value = newValue;
};
this.getValue = function () {
return value;
};

this.setBackground = function(newBackground) {
backgroundColor = newBackground;
};
Expand Down
2 changes: 2 additions & 0 deletions test/mock/SpreadsheetApp/Range.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var Range = function (myMatrix) {
}
};

this.setFormula = this.setValue;

this._getCells = function() {
return matrix;
};
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ describe('QuickDrive functions', function () {
assert.equal(QuickDrive.annotationFunctions.FOR_EACH, QuickDrive.getAnnotationType('{~foo.myList}'));
assert.equal(QuickDrive.annotationFunctions.FOR_EACH, QuickDrive.getAnnotationType('{~foo.bar.with.many.properties.myList}'));
});
it('it should insert formula annotation', function () {
assert.equal(QuickDrive.annotationFunctions.INSERT_FORMULA, QuickDrive.getAnnotationType('{#myFormula}'));
assert.equal(QuickDrive.annotationFunctions.INSERT_FORMULA, QuickDrive.getAnnotationType('{#my.formula}'));
assert.equal(QuickDrive.annotationFunctions.INSERT_FORMULA, QuickDrive.getAnnotationType('{#foo.bar.with.many.properties.myList}'));
});
});
describe('validateConfig', function () {
var createQuickDriveWithConfig = function (propertie, value) {
Expand Down Expand Up @@ -79,12 +84,14 @@ var matrixMockWithReplaceAnnotations = [
[new Cell('{=this.reportHeader}'), new Cell('My random text'), new Cell('another random text'), new Cell('')],
[new Cell('{=this.user.name}'), new Cell('{=this.user.lastName}'), new Cell('{=this.user.age}'), new Cell('My user')],
[new Cell('{=this.user.address.cep}'), new Cell('{=this.user.address.city.name}'), new Cell('{=this.user.address.city.state.name}'), new Cell('{=this.user.address.city.state.country.name}')],
[new Cell('{#this.myFormula}'), new Cell(''), new Cell(''), new Cell('')],
[new Cell('Languages that he likes'), new Cell(''), new Cell(''), new Cell('')],
[new Cell('{~this.curriculum.languagesThatLikes : languages}'), new Cell('{=languages}'), new Cell('{~}'), new Cell('text that will stay on last line')]
];

var jsonMock = {
reportHeader: 'My cool header',
myFormula: 'SUM(10,30)',
user: {
name: 'Matheus',
lastName: 'Martins do Rego',
Expand Down Expand Up @@ -122,6 +129,7 @@ describe('QuickDrive functions', function () {
['My cool header', 'My random text', 'another random text', ''],
['Matheus', 'Martins do Rego', 20, 'My user'],
['12345-678', 'Campinas', 'São Paulo', 'Brasil'],
['=SUM(10,30)', '','',''],
['Languages that he likes', '', '', ''],
['', 'Java', '', ''],
['', 'JavaScript', '', ''],
Expand Down

0 comments on commit 6b7fc30

Please sign in to comment.