Skip to content

Commit

Permalink
fix GoogleSpreadsheet.removeWorksheet with tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed May 27, 2017
1 parent 0b241d6 commit 868ad3f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ Add a new worksheet to the doc.
- `colCount` - number of columns (default = 20)
- `headers` - array of string keys to put in the first row

#### `GoogleSpreadsheet.removeWorksheet(worksheet_id, callback)`
#### `GoogleSpreadsheet.removeWorksheet(sheet, callback)`

Remove a worksheet from the doc.
Remove a worksheet from the doc - by id, index, or the SpreadsheetWorksheet object

- `worksheet_id` - the index of the sheet to add to (index starts at 1)
- `sheet` - can be a SpreadsheetWorksheet object, the id of the sheet, or the index (starts at 1)

----------------------------------

Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ var GoogleSpreadsheet = function( ss_key, auth_id, options ){
});
}

this.removeWorksheet = function ( worksheet_id, cb ){
this.removeWorksheet = function ( sheet_id, cb ){
if (!this.isAuthActive()) return cb(new Error(REQUIRE_AUTH_MESSAGE));
self.makeFeedRequest( ["worksheets", ss_key, worksheet_id], 'DELETE', cb );
if (sheet_id instanceof SpreadsheetWorksheet) return sheet_id.del(cb);
self.makeFeedRequest( GOOGLE_FEED_URL + "worksheets/" + ss_key + "/private/full/" + sheet_id, 'DELETE', null, cb );
}

this.getRows = function( worksheet_id, opts, cb ){
Expand Down
62 changes: 61 additions & 1 deletion test/manage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('Managing doc info and sheets', function() {
});
});

it('can delete a worksheet', function(done) {
it('can delete a worksheet with `SpreadsheetWorksheet.del()`', function(done) {
sheet.del(function(err) {
(!err).should.be.true;
// check if the sheet is really gone
Expand All @@ -169,6 +169,66 @@ describe('Managing doc info and sheets', function() {
});
});

it('can delete a worksheet with `GoogleSpreadsheet.removeWorksheet()` passing the sheet object', function(done) {
doc.addWorksheet({
title: sheet_title,
colCount: 10
}, function(err, _sheet) {
(!err).should.be.true;
doc.removeWorksheet(_sheet, function(err) {
(!err).should.be.true;
doc.getInfo(function(err, info) {
(!err).should.be.true;
var last_sheet = info.worksheets.pop();
last_sheet.title.should.not.equal(sheet_title);
done();
});
});
});
});

it('can delete a worksheet with `GoogleSpreadsheet.removeWorksheet()` passing the sheet ID', function(done) {
doc.addWorksheet({
title: sheet_title,
colCount: 10
}, function(err, _sheet) {
(!err).should.be.true;
doc.removeWorksheet(_sheet.id, function(err) {
(!err).should.be.true;
doc.getInfo(function(err, info) {
(!err).should.be.true;
var last_sheet = info.worksheets.pop();
last_sheet.title.should.not.equal(sheet_title);
done();
});
});
});
});

it('can delete a worksheet with `GoogleSpreadsheet.removeWorksheet()` passing the index of the sheet', function(done) {
doc.addWorksheet({
title: sheet_title,
colCount: 10
}, function(err, _sheet) {
(!err).should.be.true;

doc.getInfo(function(err, info) {
(!err).should.be.true;
var sheet_index = info.worksheets.length;

doc.removeWorksheet(sheet_index, function(err) {
(!err).should.be.true;
doc.getInfo(function(err, info) {
(!err).should.be.true;
var last_sheet = info.worksheets.pop();
last_sheet.title.should.not.equal(sheet_title);
done();
});
});
});
});
});

it('can add a sheet with specific number of rows and columns', function(done) {
doc.addWorksheet({
title: sheet_title,
Expand Down

0 comments on commit 868ad3f

Please sign in to comment.