Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions lib/GoogleSpreadsheetWorksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,35 @@ class GoogleSpreadsheetWorksheet {
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#DeleteConditionalFormatRuleRequest
}

async sortRange() {
// Request type = `sortRange`
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#SortRangeRequest
// sortSpec is of the format
// [[rowIndex, order], [rowIndex, order]...] where order is 'desc' or 'asc'
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#SortRangeRequest
async sortRange(sortSpecs, range) {
if (!sortSpecs.length) return null;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better to throw an error?

if (!_.isArray(sortSpecs[0])) { sortSpecs = [sortSpecs]; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter will probably catch it but dont need {} around a one liner


if (!this.headerValues) await this.loadHeaderRow();

// Allow a few values for order
const dirmap = {
desc: 'DESCENDING',
asc: 'ASCENDING',
DESCENDING: 'DESCENDING',
ASCENDING: 'ASCENDING',
};

sortSpecs = sortSpecs.map(([idx, dir]) => ({
sortOrder: dirmap[dir],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe good to use toUpperCase or toLowerCase on what gets passed in to support 'desc' and 'DESC' etc

dimensionIndex: typeof idx === 'number' ? idx : this.headerValues.indexOf(idx),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if the header row doesnt exits? Maybe should throw an error?

}));

if (!range) { range = {}; }
range.sheetId = this.sheetId;

return this._makeSingleUpdateRequest('sortRange', {
range,
sortSpecs,
});
}

async setDataValidation() {
Expand Down
30 changes: 30 additions & 0 deletions test/cells.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,35 @@ describe('Cell-based operations', () => {
});
});
});

describe('sorting a sheet', () => {
beforeEach(async () => {
await sheet.loadCells('A1:C4');
const sheetData = [
['a', 'b', 'c'],
[1, 1, 2],
[2, 3, 1],
[3, 2, 3],
];
for (let row = 0; row < sheetData.length; row++) {
for (let col = 0; col < sheetData[row].length; col++) {
sheet.getCell(row, col).value = sheetData[row][col];
}
}
await sheet.saveUpdatedCells();
});

it('sorts by index', async () => {
await sheet.sortRange([[0, 'desc']]);
await sheet.loadCells('A1:C4');
expect(sheet.getCell(1, 0).value).toBe(3);
});

it('sorts by header name', async () => {
await sheet.sortRange([['b', 'DESCENDING']]);
await sheet.loadCells('A1:C4');
expect(sheet.getCell(1, 0).value).toBe(2);
});
});
});
});