Skip to content

Commit

Permalink
fix saveCells
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed Feb 27, 2020
1 parent 0102f1c commit f6cfcc3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/GoogleSpreadsheetCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,18 @@ class GoogleSpreadsheetCell {
// used by worksheet when saving cells
// returns an individual batchUpdate request to update the cell
_getUpdateRequest() {
// this logic should match the _isDirty logic above
// but we need it broken up to build the request below
const isValueUpdated = this._draftData.value !== undefined;
const isNoteUpdated = this._draftData.note !== undefined;
const isFormatUpdated = _.keys(this._draftData.userEnteredFormat || {}).length;
const isFormatCleared = this._draftData.clearFormat;

// if no updates, we return null, which we can filter out later before sending requests
if (!_.some([isValueUpdated, isNoteUpdated, isFormatUpdated, isFormatCleared])) {
return null;
}

return {
updateCells: {
rows: [{
Expand Down
7 changes: 7 additions & 0 deletions lib/GoogleSpreadsheetWorksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ class GoogleSpreadsheetWorksheet {
// and we dont want to accidentally overwrite something
const requests = _.map(cellsToUpdate, (cell) => cell._getUpdateRequest());
const responseRanges = _.map(cellsToUpdate, (c) => `${this.a1SheetName}!${c.a1Address}`);

// if nothing is being updated the request returned is just `null`
// so we make sure at least 1 request is valid - otherwise google throws a 400
if (!_.compact(requests).length) {
throw new Error('At least one cell must have something to update');
}

await this._spreadsheet._makeBatchUpdateRequest(requests, responseRanges);
}

Expand Down
16 changes: 16 additions & 0 deletions test/cells.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ describe('Cell-based operations', () => {
expect(() => { c1.value = { foo: 1 }; }).toThrow();
});

describe('calling saveCells directly', () => {
it('can save an array of cells', async () => {
_.each([c1, c2, c3], (cell) => { cell.value = 'calling saveCells'; });
await sheet.saveCells([c1, c2, c3]);
});

it('can save a mix of dirty and non-dirty', async () => {
c2.value = 'saveCells again';
await sheet.saveCells([c1, c2, c3]);
});

it('will throw an error if no cells are dirty', async () => {
await expect(sheet.saveCells([c1, c2, c3])).rejects.toThrow();
});
});

describe('cell formulas', () => {
it('can update a cell with a formula via .value', async () => {
c1.value = '=2';
Expand Down

0 comments on commit f6cfcc3

Please sign in to comment.