Skip to content

Commit

Permalink
Merge pull request #428 from johnie/feature/insert-dimension
Browse files Browse the repository at this point in the history
Added function insertDimenion
  • Loading branch information
theoephraim authored Nov 6, 2021
2 parents 1da839d + 57efde1 commit 9232db5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/classes/google-spreadsheet-worksheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ Param|Type|Required|Description

-**Side effects** - sheet is updated

#### `insertDimension(columnsOrRows, bounds, inheritFromBefore)` (async) :id=fn-insertDimension

> Update sheet "dimension properties"
| Param | Type | Required | Description |
| --- | --- | --- | --- |
| `columnsOrRows` | String (enum)<br>_"COLUMNS" or "ROWS"_ || Which dimension |
| `bounds` | Object | - |
| `bounds.startIndex` | Number<br>_int >= 0_ | - | Start row/column |
| `bounds.endIndex` | Number<br>_int >= 0_ | - | End row/column |
| `inheritFromBefore` | Boolean<br>_default false_ | - | If true, tells the API to give the new columns or rows the same properties as the prior row or column |

-**Side effects** - sheet is updated

### Other

Expand Down
18 changes: 17 additions & 1 deletion lib/GoogleSpreadsheetWorksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,25 @@ class GoogleSpreadsheetWorksheet {
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#FindReplaceRequest
}

async insertDimension() {
async insertDimension(columnsOrRows, bounds, inheritFromBefore = false) {
// Request type = `insertDimension`
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#InsertDimensionRequest

if (!columnsOrRows) {
throw new Error('You need to specify a dimension. i.e. COLUMNS|ROWS');
}

return this._makeSingleUpdateRequest('insertDimension', {
range: {
sheetId: this.sheetId,
dimension: columnsOrRows,
...bounds && {
startIndex: bounds.startIndex,
endIndex: bounds.endIndex,
},
},
inheritFromBefore,
});
}

async insertRange() {
Expand Down
32 changes: 32 additions & 0 deletions test/manage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,36 @@ describe('Managing doc info and sheets', () => {
expect(newDoc.sheetsByIndex[0]).toBeInstanceOf(GoogleSpreadsheetWorksheet);
});
});

describe('Insert a column or row to a document', () => {
let sheet;

beforeAll(async () => {
sheet = await doc.addSheet({
title: `Sheet to copy ${+new Date()}`,
headerValues: ['a', 'b'],
});
await sheet.addRow({
a: 'a',
b: 'b',
});
});

afterAll(async () => {
await sheet.delete();
});

it('Should insert a new empty row at index', async () => {
await sheet.insertDimension('ROWS', {
startIndex: 1,
endIndex: 2,
});

// read rows
const rows = await sheet.getRows();

expect(rows[0].a).toEqual('');
expect(rows[0].b).toEqual('');
});
});
});

0 comments on commit 9232db5

Please sign in to comment.