From 5b1917eeb2247c1f52e492007ddc47023fb9a71b Mon Sep 17 00:00:00 2001 From: Theo Ephraim Date: Sun, 25 Oct 2015 14:22:05 -0400 Subject: [PATCH] rework bulk cell saving and updating formulas! --- .gitignore | 3 ++- README.md | 47 ++++++++++++++++++++++++++++++++++++++++++++++- index.js | 28 ++++++++++++++++++++++------ test.js | 40 ---------------------------------------- 4 files changed, 70 insertions(+), 48 deletions(-) delete mode 100644 test.js diff --git a/.gitignore b/.gitignore index 44f9365..9d89800 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ ignore/ -.env \ No newline at end of file +.env +test.js \ No newline at end of file diff --git a/README.md b/README.md index 27d0de0..df5bc5f 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,7 @@ Add a single row to the sheet. #### `GoogleSpreadsheet.getCells(worksheet_id, options, callback)` Get an array of cell objects. + - `worksheet_id` - the index of the sheet to add to (index starts at 1) - `options` (optional) - `min-row` - row range min (uses #s visible on the left) @@ -200,6 +201,27 @@ Get an array of cell objects. - `max-col` - column range max - `return-empty` - include empty cells (boolean) +#### `GoogleSpreadsheet.bulkUpdateCells(worksheet_id, cells, callback)` + +Do a bulk update on cells. + +- `worksheet_id` - the index of the sheet to add to (index starts at 1) +- `cells` - an array of SpreadsheetCell objects to save + +#### `GoogleSpreadsheet.addWorksheet(options, callback)` + +Add a new worksheet to the doc. + +- `options` (optional) + - `title` - title for the new sheet (default = 'New Worksheet') + - `rowCount` - number of rows (default = 50) + - `colCount` - number of columns (default = 10) + +#### `GoogleSpreadsheet.deleteWorksheet(worksheet_id, callback)` + +Remove a worksheet from the doc. + +- `worksheet_id` - the index of the sheet to add to (index starts at 1) ---------------------------------- @@ -210,6 +232,7 @@ Represents a single "sheet" from the spreadsheet. These are the different tabs/p This is a really just a wrapper to call the same functions on the spreadsheet without needing to include the worksheet id. __Properties:__ +- `url` - the URL for the sheet - `id` - the ID of the sheet - `title` - the title (visible on the tabs in google's interface) - `rowCount` - number of rows @@ -224,6 +247,12 @@ See above. ### `SpreadsheetWorksheet.addRow(new_row, callback)` See above. +#### `SpreadsheetWorksheet.bulkUpdateCells(cells, callback)` +See above. + +### `SpreadsheetWorksheet.del(callback)` +Remove this sheet from the doc. + ---------------------------------- ### `SpreadsheetRow` @@ -241,9 +270,25 @@ Deletes the row from the sheet. ### `SpreadsheetCell` Represents a single cell from the sheet. +Using cells is the only way to read and modify the formulas in your sheet. + +__Properties:__ +- `id` - the ID of the cell +- `row` - the row this cell is in +- `col` - the column this cell is in +- `value` - the value of the cell +- `numericValue` - the value of the cell as a number +- `inputValue` - the "raw" value of the cell which can be a formula + +__IMPORTANT__: +- Cells with regular values can be modified by setting `value` and calling `save` +- Cells with formulas in them can be modified by setting `inputValue` and calling `save` #### `SpreadsheetCell.setValue(val, callback)` -Set the value of the cell and save it. +Set the value of the cell and saves it. + +#### `SpreadsheetCell.save(callback)` +Saves the current value/formula #### `SpreadsheetCell.del(callback)` Clear the cell -- internally just calls `.setValue('', callback)` diff --git a/index.js b/index.js index 8c7bd48..8ec3d41 100644 --- a/index.js +++ b/index.js @@ -271,15 +271,16 @@ var GooogleSpreadsheet = function( ss_key, auth_id, options ){ }); } - // this.bulkUpdateCells = function (worksheet_id, cells, values, cb) { + // this.bulkUpdateCells = function (worksheet_id, cells, cb) { // var entries = cells.map((cell, i) => { + // cell._needsSave = false; // return ` // ${cell.id} // // ${cell.id} // - // + // // ` // }); // var worksheetUrl = `https://spreadsheets.google.com/feeds/cells/${ss_key}/${worksheet_id}/private/full`; @@ -294,9 +295,10 @@ var GooogleSpreadsheet = function( ss_key, auth_id, options ){ // 'POST', data_xml, cb) // } - this.bulkUpdateCells = function (worksheet_id, cells, values, cb) { + this.bulkUpdateCells = function (worksheet_id, cells, cb) { var entries = cells.map(function (cell, i) { - return "\n " + cell.id + "\n \n " + cell.id + "\n \n \n "; + cell._needsSave = false; + return "\n " + cell.id + "\n \n " + cell.id + "\n \n \n "; }); var worksheetUrl = "https://spreadsheets.google.com/feeds/cells/" + ss_key + "/" + worksheet_id + "/private/full"; var data_xml = "\n " + worksheetUrl + "\n " + entries.join("\n") + "\n "; @@ -323,6 +325,9 @@ var SpreadsheetWorksheet = function( spreadsheet, data ){ this.addRow = function( data, cb ){ spreadsheet.addRow( self.id, data, cb ); } + this.bulkUpdateCells = function( cells, cb ) { + spreadsheet.bulkUpdateCells( self.id, cells, cb ); + } this.del = function ( cb ){ spreadsheet.makeFeedRequest( self.url, 'DELETE', null, cb ); } @@ -389,24 +394,35 @@ var SpreadsheetCell = function( spreadsheet, worksheet_id, data ){ self.numericValue = data['gs:cell']['$']['numericValue']; self.inputValue = data['gs:cell']['$']['inputValue']; + var _hasFormula = self.inputValue.substr(0,1) === '='; + self['_links'] = []; links = forceArray( data.link ); links.forEach( function( link ){ self['_links'][ link['$']['rel'] ] = link['$']['href']; }); + self.getValueForSave = function(){ + if (_hasFormula){ + return self.inputValue; + } else { + return xmlSafeValue(self.value); + } + } + self.setValue = function(new_value, cb) { self.value = new_value; self.save(cb); }; self.save = function(cb) { - new_value = xmlSafeValue(self.value); + self._needsSave = false; + var edit_id = 'https://spreadsheets.google.com/feeds/cells/key/worksheetId/private/full/R'+self.row+'C'+self.col; var data_xml = ''+edit_id+''+ ''+ - '' + '' data_xml = data_xml.replace('', ""); diff --git a/test.js b/test.js deleted file mode 100644 index a08edfb..0000000 --- a/test.js +++ /dev/null @@ -1,40 +0,0 @@ -var async = require('async'); - -var GoogleSpreadsheet = require("./index.js"); -var doc = new GoogleSpreadsheet('1saRApLme-qgAH0nyfg2mqPcm980nzKDg5kES0tS90rg'); - -// doc.getInfo(function(err, info){ -// if (err) return console.log(err); -// console.log(info); -// }); -// return; - -var sheet; - -doc.useServiceAccountAuth(require('./creds'), function(err){ - if (err) { - console.log(err) - return; - } - doc.getInfo(function(err, info){ - if (err) return console.log(err); - console.log(info); - sheet = info.worksheets[0]; - - // info.worksheets[0].getRows(function(err, rows){ - // console.log(rows[0]); - // }); - addRow(); - setInterval(addRow, 1000*60); - }); -}); - -function addRow(){ - var now = new Date(); - sheet.addRow({time: now.toString(), test: 'foo'}, function(err, result){ - console.log(err); - console.log(result); - }); -} - -