diff --git a/src/SheetsTemplater.gs b/src/SheetsTemplater.gs index 466df1e..31e4b39 100644 --- a/src/SheetsTemplater.gs +++ b/src/SheetsTemplater.gs @@ -1,4 +1,4 @@ -var QuickDrive = function (DriveApp, newConfig) { +var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) { var QuickDrive = {}; QuickDrive.annotationFunctions = { @@ -12,7 +12,7 @@ var QuickDrive = function (DriveApp, newConfig) { '=': QuickDrive.annotationFunctions.REPLACE_TEXT, '~': QuickDrive.annotationFunctions.FOR_EACH }; - this.config = { + this._config = { folderId: '0ByQE0cDEoa0qLUlPU21xVzNqZVk', templateId: '1stc2xmCa3QB61bTR52tomteWUOwlVZ4s8OSKWG5dP_8', newDocumentName: 'My new sheet', @@ -23,16 +23,40 @@ var QuickDrive = function (DriveApp, newConfig) { permission: DriveApp.Permission.VIEW }] }; - for (var propertie in newConfig) { - config[propertie] = newConfig[propertie]; + var validateConfig = function (config) { + if (config.folderId && (typeof config.folderId != 'string' || config.folderId.length != 18)) { + throw new Error('invalid-folder-id'); + } + + if (config.templateId && (typeof config.templateId != 'string' || config.templateId.length != 45)) { + throw new Error('invalid-file-id'); + } + + if (config.newDocumentName && (typeof config.newDocumentName != 'string')) { + throw new Error('invalid-file-name'); + } + + 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')) + ))) { + throw new Error('invalid-stripe-color'); + } + }; + + if (newConfig) { + validateConfig(newConfig); + for (var propertie in newConfig) { + this._config[propertie] = newConfig[propertie]; + } } QuickDrive.getSheetNewDocument = function () { - var templateFile = DriveApp.getFileById(config.templateId); - var newFile = templateFile.makeCopy(config.newDocumentName, DriveApp.getFolderById(config.folderId)); + var templateFile = DriveApp.getFileById(_config.templateId); + var newFile = templateFile.makeCopy(_config.newDocumentName, DriveApp.getFolderById(_config.folderId)); - for (var i = 0; i < config.permissions.length; i++) { - newFile.setSharing(config.permissions[i].access, config.permissions[i].permission); + for (var i = 0; i < _config.permissions.length; i++) { + newFile.setSharing(_config.permissions[i].access, _config.permissions[i].permission); } var ss = SpreadsheetApp.open(newFile); @@ -157,6 +181,28 @@ var QuickDrive = function (DriveApp, newConfig) { var annotationFunction = QuickDrive.getAnnotationType(cellValue)(properties); }; + QuickDrive.processSheet = function(json) { + var newSpreadSheet = QuickDrive.getSheetNewDocument(); + var sheet = newSpreadSheet.sheet; + var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()); + var values = range.getValues(); + var myThis = {}; + myThis['this'] = json; + var properties = { + sheet: sheet, + json: myThis, + values: values, + i: 0, + j: 0 + }; + 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); + } + } + return newSpreadSheet; + }; + return QuickDrive; }; @@ -164,28 +210,8 @@ 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 newSpreadSheet = QuickDrive.getSheetNewDocument(json); - var sheet = newSpreadSheet.sheet; - - var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()); - var values = range.getValues(); - var myThis = {}; - myThis['this'] = json; - var properties = { - sheet: sheet, - json: myThis, - values: values, - i: 0, - j: 0 - }; - QuickDrive.setConfigs(config); - 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); - } - } - - return ContentService.createTextOutput(newSpreadSheet.fileId); + var newFile = QuickDrive.processSheet(json); + return ContentService.createTextOutput(newFile.fileId); }; if (typeof module !== 'undefined' && module.exports != null) { diff --git a/test/mock/DriveAppMock.js b/test/mock/DriveAppMock.js index effe25d..2c60ec6 100644 --- a/test/mock/DriveAppMock.js +++ b/test/mock/DriveAppMock.js @@ -36,7 +36,7 @@ var DriveApp = function () { this.setName = function (newName) { properties.name = newName; }; - this._setProperties = function(newProperties) { + this._setProperties = function (newProperties) { for (var propertie in newProperties) { properties[propertie] = newProperties[propertie]; } @@ -67,17 +67,10 @@ var DriveApp = function () { }; }; -var SpreadsheetApp = function () { - var SpreadSheetMock = function (mySheets) { - var sheets = mySheets || []; - - this.getSheets = function () { - return sheets; - }; +var SpreadsheetApp = function (mock) { + var SpreadSheetMock = function (mock) { var SheetMock = function (myMatrix) { - var matrix = myMatrix || [ - [] - ]; + var matrix = myMatrix; var CellMock = function () { var value = ""; @@ -95,7 +88,7 @@ var SpreadsheetApp = function () { [] ]; - this.getValues = function () { + this.getValues = function (row, col, lines, cols) { var values = []; for (var i = row; i < lines; i++) { values[i] = []; @@ -115,7 +108,7 @@ var SpreadsheetApp = function () { }; }; - SheetMock.getRange = function (row, col, lines, cols) { + this.getRange = function (row, col, lines, cols) { var newMatrix = [ [] ]; @@ -127,12 +120,26 @@ var SpreadsheetApp = function () { } return new CellGroupMock(newMatrix); }; + + this.getMaxRows = function () { + return matrix.length; + }; + + this.getMaxColumns = function () { + return matrix[0].length; + }; + }; + + var sheets = [new SheetMock(mock)]; + + this.getSheets = function () { + return sheets; }; }; return { open: function (file) { - return new SpreadSheetMock(); + return new SpreadSheetMock(mock); } } }; diff --git a/test/test.js b/test/test.js index f2a443b..70cad3d 100644 --- a/test/test.js +++ b/test/test.js @@ -1,8 +1,13 @@ -var DriveApp = require('./mock/DriveAppMock.js').DriveAppMock.DriveApp; -var QuickDrive = require('../src/SheetsTemplater.gs').QuickDrive(DriveApp()); -var assert = require('chai').assert; +evar DriveAppMock = require('./mock/DriveAppMock.js').DriveAppMock; +var DriveApp = DriveAppMock.DriveApp; +var SpreadsheetApp = DriveAppMock.SpreadsheetApp; +var QuickDriveConstructor = require('../src/SheetsTemplater.gs').QuickDrive; +var QuickDrive = QuickDriveConstructor(DriveApp(), SpreadsheetApp()); +var chai = require('chai'); +var assert = chai.assert; +var expect = chai.expect; -describe('QuickDrive', function () { +describe('QuickDrive functions', function () { describe('getAnnotationType', function () { it('it should return none annotation, just simple text', function () { assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('==simple text with no annotation==')); @@ -30,9 +35,62 @@ describe('QuickDrive', function () { assert.equal(QuickDrive.annotationFunctions.FOR_EACH, QuickDrive.getAnnotationType('{~foo.bar.with.many.properties.myList}')); }); }); - describe('merge configs', function () { - it('it should keep default config', function () { - var config = QuickDrive.confi + describe('validateConfig', function () { + var createQuickDriveWithConfig = function (propertie, value) { + var obj = {}; + obj[propertie] = value; + return function () { + QuickDriveConstructor(DriveApp(), SpreadsheetApp(), obj); + }; + }; + it('it should have default config with no error', function () { + QuickDriveConstructor(DriveApp(), QuickDrive._config); + }); + it('it should validate fileId', function () { + assert.throws(createQuickDriveWithConfig('templateId', 'myshortid'), Error, 'invalid-file-id'); + assert.throws(createQuickDriveWithConfig('templateId', 2313123), Error, 'invalid-file-id'); + assert.throws(createQuickDriveWithConfig('templateId', true), Error, 'invalid-file-id'); + createQuickDriveWithConfig('fileId', '123456789012345678901234567890123456789012345')(); + }); + it('it should validate folderId', function () { + assert.throws(createQuickDriveWithConfig('folderId', 'myshortid'), Error, 'invalid-folder-id'); + assert.throws(createQuickDriveWithConfig('folderId', 2313123), Error, 'invalid-folder-id'); + assert.throws(createQuickDriveWithConfig('folderId', true), Error, 'invalid-folder-id'); + createQuickDriveWithConfig('folderId', '123456789012345678')(); + }); + it('it should validate file name', function () { + assert.throws(createQuickDriveWithConfig('newDocumentName', 2313123), Error, 'invalid-file-name'); + assert.throws(createQuickDriveWithConfig('newDocumentName', true), Error, 'invalid-file-name'); + createQuickDriveWithConfig('newDocumentName', 'my new name')(); + }); + it('it should validate stripe color', function () { + assert.throws(createQuickDriveWithConfig('stripeColor', 2313123), Error, 'invalid-stripe-color'); + assert.throws(createQuickDriveWithConfig('stripeColor', true), Error, 'invalid-stripe-color'); + assert.throws(createQuickDriveWithConfig('stripeColor', '#3333'), Error, 'invalid-stripe-color'); + assert.throws(createQuickDriveWithConfig('stripeColor', '3333'), Error, 'invalid-stripe-color'); + createQuickDriveWithConfig('stripeColor', '#333333')(); + createQuickDriveWithConfig('stripeColor', '#333')(); + createQuickDriveWithConfig('stripeColor', 'rgb(123,123,123)')(); + }); + }); +}); + +var matrixMock = [ + ['asd', 'koko', '{=this.my_header}', 'my left header'], + ['asd', 'koko', '{=this.my_header}', 'my left header'], + ['asd', 'koko', '{=this.my_header}', 'my left header'] +]; + +var jsonMock = { + this: { + my_header: 'header title' + } +}; +describe('QuickDrive functions', function () { + describe('get new sheet', function () { + it('it should return sheet with text replaced', function () { + var QuickDriveMock = QuickDriveConstructor(DriveApp(), SpreadsheetApp(matrixMock)); + var file = QuickDriveMock.processSheet(jsonMock); }); }); });