Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusmr13 committed Jul 14, 2016
2 parents bbd52c5 + e576b84 commit 9658151
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 33 deletions.
4 changes: 2 additions & 2 deletions gapps.config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"path": "src",
"fileId": "1tVx2IimBlEy63KKegPL_-a7vd0OLyfCfyz6GIi15Q96XIlgN6Yhcpxdm"
}
"fileId": "1kHl7JbG4w07oOgkrWfYSo-bs_9Ocb3GqIpWzfmKt1iH64zkwRE-uRAa6"
}
67 changes: 39 additions & 28 deletions src/SheetsTemplater.gs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
var QuickDrive = {};
QuickDrive.annotationFunctions = {
var annotationFunctions = {
REPLACE_TEXT: replaceValue,
FOR_EACH: processForEach,
INSERT_FORMULA: insertFormula,
NONE: function (properties) {
return;
}
};
this.annotationFunctions = annotationFunctions;
var annotationType = {
'=': QuickDrive.annotationFunctions.REPLACE_TEXT,
'~': QuickDrive.annotationFunctions.FOR_EACH
'=': annotationFunctions.REPLACE_TEXT,
'~': annotationFunctions.FOR_EACH,
'#': annotationFunctions.INSERT_FORMULA
};
this._config = {
folderId: '0ByQE0cDEoa0qLUlPU21xVzNqZVk',
templateId: '1stc2xmCa3QB61bTR52tomteWUOwlVZ4s8OSKWG5dP_8',
var _config = {
folderId: '0B8cJhvYlR-sCcGR4d3VYWGZaYWM',
templateId: '1l7sMxfD-qh4sbeu6Ax0z6v84YdahXDTG8hlPcE_vkEo',
newDocumentName: 'My new sheet',
stripeColor: '#EEEEEE',
stripeFirst: false,
Expand All @@ -22,6 +24,7 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
permission: DriveApp.Permission.VIEW
}]
};
this._config = _config;
var validateConfig = function (config) {
if (config.folderId && (typeof config.folderId != 'string' || config.folderId.length != 18)) {
throw new Error('invalid-folder-id');
Expand All @@ -36,21 +39,21 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
}

if (config.stripeColor && (typeof config.stripeColor != 'string' ||
(config.stripeColor[0] == '#' && (config.stripeColor.length != 4 && config.stripeColor.length != 7) ||
(config.stripeColor[0] != '#' && !(config.stripeColor[0] == 'r' && config.stripeColor[1] == 'g' && config.stripeColor[2] == 'b'))
))) {
(config.stripeColor[0] == '#' && (config.stripeColor.length != 4 && config.stripeColor.length != 7) ||
(config.stripeColor[0] != '#' && !(config.stripeColor[0] == 'r' && config.stripeColor[1] == 'g' && config.stripeColor[2] == 'b'))
))) {
throw new Error('invalid-stripe-color');
}
};

if (newConfig) {
validateConfig(newConfig);
for (var propertie in newConfig) {
this._config[propertie] = newConfig[propertie];
_config[propertie] = newConfig[propertie];
}
}

QuickDrive.getSheetNewDocument = function () {
var getSheetNewDocument = function () {
var templateFile = DriveApp.getFileById(_config.templateId);
var newFile = templateFile.makeCopy(_config.newDocumentName, DriveApp.getFolderById(_config.folderId));

Expand All @@ -64,6 +67,7 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
fileId: newFile.getId()
};
};
this.getSheetNewDocument = getSheetNewDocument;

function isAnottation(text) {
return text[0] == '{' && text[text.length - 1] == '}';
Expand All @@ -88,18 +92,19 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
return true;
};

QuickDrive.getAnnotationType = function (text) {
var getAnnotationType = function (text) {

if (isAnottation(text)) {
if (isValidAnnotation(text.split(':')[0])) {
return annotationType[text[1]] || QuickDrive.annotationFunctions.NONE;
return annotationType[text[1]] || annotationFunctions.NONE;
} else {
return QuickDrive.annotationFunctions.NONE;
return annotationFunctions.NONE;
}
} else {
return QuickDrive.annotationFunctions.NONE;
return annotationFunctions.NONE;
}
};
this.getAnnotationType = getAnnotationType;

var getValueOnJson = function (jsonObject, path) {
var pathSplit = path.split('.');
Expand Down Expand Up @@ -152,8 +157,7 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
properties.i = i - 1;
properties.j = j - 1;
properties.json[entityName] = array[index];
console.info(properties.json[entityName]);
QuickDrive.processCell(properties);
processCell(properties);

}
if (_config.stripeFirst == !(index % 2)) {
Expand All @@ -175,14 +179,23 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
sheet.getRange(row, col).setValue(getValueOnJson(json, command.substring(2, command.length - 1)));
};

QuickDrive.processCell = function (properties) {
console.info(' ' + properties.i + ' ' +properties.j);
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)));
};

var processCell = function (properties) {
var cellValue = properties.values[properties.i][properties.j];
var annotationFunction = QuickDrive.getAnnotationType(cellValue)(properties);
var annotationFunction = getAnnotationType(cellValue)(properties);
};

QuickDrive.processSheet = function(json) {
var newSpreadSheet = QuickDrive.getSheetNewDocument();
this.processSheet = function (json) {

var newSpreadSheet = getSheetNewDocument();
var sheet = newSpreadSheet.sheet;
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
var values = range.getValues();
Expand All @@ -197,20 +210,18 @@ var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
};
for (properties.i = 0; properties.i < properties.values.length; properties.i++) {
for (properties.j = 0; properties.j < properties.values[properties.i].length; properties.j++) {
QuickDrive.processCell(properties);
processCell(properties);
}
}
return newSpreadSheet;
};

return QuickDrive;
};

function doPost(e) {
var json = e ? JSON.parse(e.parameters.data[0]) : {};
var config = e ? (e.parameters.config ? JSON.parse(e.parameters.config[0]) : {}) : {};
var QuickDrive = new QuickDrive(DriveApp, SpreadsheetApp, config);
var newFile = QuickDrive.processSheet(json);
var QuickDriveObj = new QuickDrive(DriveApp, SpreadsheetApp, config);
var newFile = QuickDriveObj.processSheet(json);
return ContentService.createTextOutput(newFile.fileId);
};

Expand Down
5 changes: 4 additions & 1 deletion test/fullTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,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 @@ -48,12 +50,13 @@ var jsonMock = {
describe('QuickDrive functions', function () {
describe('get new sheet', function () {
it('it should return sheet with text replaced', function () {
var QuickDriveMock = QuickDriveConstructor(DriveApp(), SpreadsheetApp(matrixMockWithReplaceAnnotations));
var QuickDriveMock = new QuickDriveConstructor(DriveApp(), SpreadsheetApp(matrixMockWithReplaceAnnotations));
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'],
['12345-678', 'Campinas', 'São Paulo', 'Brasil'],
['=SUM(10,30)', '','',''],
['Languages that he likes', '', '', ''],
['', 'Java', '', ''],
['', 'JavaScript', '', ''],
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
7 changes: 6 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var DriveApp = require('./mock/DriveApp.js').DriveApp;
var Cell = require('./mock/SpreadsheetApp/Cell.js').Cell;
var SpreadsheetApp = require('./mock/SpreadsheetApp.js').SpreadsheetApp;
var QuickDriveConstructor = require('../src/SheetsTemplater.gs').QuickDrive;
var QuickDrive = QuickDriveConstructor(DriveApp(), SpreadsheetApp());
var QuickDrive = new QuickDriveConstructor(DriveApp(), SpreadsheetApp());
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;
Expand Down 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

0 comments on commit 9658151

Please sign in to comment.