Skip to content

Commit

Permalink
add sheet.duplicate, clean up dependencies, docus updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo Ephraim committed Nov 14, 2021
1 parent 60c53dc commit 4cec425
Show file tree
Hide file tree
Showing 7 changed files with 841 additions and 535 deletions.
2 changes: 1 addition & 1 deletion docs/classes/google-spreadsheet-cell.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Property|Type|Description
---|---|---
`rowIndex`|Number<br>_int >= 0_|Row in the sheet this cell is in<br>_first row is 0_
`columnIndex`|Number<br>_int >= 0_|Column in the sheet this cell is in
`a1Row`|Number<br>_int > 0_|Row number used in A1 addresses<br>_This matches what you see in the UI_
`a1Row`|Number<br>_int >= 1_|Row number used in A1 addresses<br>_This matches what you see in the UI_
`a1Column`|String|Column letter used in the sheet<br>_starts at A, goes up to Z, then AA..._
`a1Address`|String|Full A1 address of the cell<br>_for example "B5"_

Expand Down
29 changes: 21 additions & 8 deletions docs/classes/google-spreadsheet-worksheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Usually this is called automatically when loading rows via `getRows()` if the he

Param|Type|Required|Description
---|---|---|---
`headerRowIndex`|Number|-|Optionally set custom header row index, if headers are not in first row<br>NOTE - not zero-indexed
`headerRowIndex`|Number<br>_int >= 1_|-|Optionally set custom header row index, if headers are not in first row<br>NOTE - not zero-indexed, 1 = first

-**Side effects** - `sheet.headerValues` is populated

Expand All @@ -82,7 +82,7 @@ Param|Type|Required|Description
Param|Type|Required|Description
---|---|---|---
`headerValues`|[String]|✅|Array of strings to set as cell values in first row
`headerRowIndex`|Number|-|Optionally set custom header row index, if headers are not in first row<br>NOTE - not zero-indexed
`headerRowIndex`|Number<br>_int >= 1_|-|Optionally set custom header row index, if headers are not in first row<br>NOTE - not zero-indexed, 1 = first

-**Side effects** - header row of the sheet is filled, `sheet.headerValues` is populated

Expand Down Expand Up @@ -122,7 +122,7 @@ Param|Type|Required|Description
---|---|---|---
`options`|Object|-|Options object
`options.offset`|Number<br>_int >= 0_|-|How many rows to skip from the top
`options.limit`|Number<br>_int > 0_|-|Max number of rows to fetch
`options.limit`|Number<br>_int >= 1_|-|Max number of rows to fetch

- ↩️ **Returns** - [[GoogleSpreadsheetRow](classes/google-spreadsheet-row)] (in a promise)

Expand Down Expand Up @@ -213,7 +213,7 @@ Param|Type|Required|Description
Param|Type|Required|Description
---|---|---|---
`range`|Object<br>[GridRange](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#GridRange)|✅|Range of cells to merge, sheetId not required!
`mergeType`|String (enum)<br>[MergeType](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#MergeType)|-|defaults to MERGE_ALL
`mergeType`|String (enum)<br>[MergeType](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#MergeType)|-|_defaults to `MERGE_ALL`_

- 🚨 **Warning** - Reading values from merged cells other than the top-left one will show a null value

Expand Down Expand Up @@ -267,7 +267,7 @@ Param|Type|Required|Description
| `range` | Object ||
| `range.startIndex` | Number<br>_int >= 0_ || Start row/column (inclusive) |
| `range.endIndex` | Number<br>_int >= 1_ || End row/column (exclusive), must be greater than startIndex |
| `inheritFromBefore` | Boolean<br>_default true_ | - | If true, tells the API to give the new columns or rows the same properties as the prior row or column<br>NOTE - defaults to false if inserting in first row/column |
| `inheritFromBefore` | Boolean | - | If true, tells the API to give the new columns or rows the same properties as the prior row or column<br><br>_defaults to true, unless inserting in first row/column_ |

-**Side effects** - new row(s) or column(s) are inserted into the sheet
- 🚨 **Warning** - Does not update cached rows/cells, so be sure to reload rows/cells before trying to make any updates to sheet contents
Expand All @@ -282,18 +282,31 @@ Param|Type|Required|Description
#### `delete()` (async) :id=fn-delete
> Delete this sheet
-**Side Effects -** sheet is deleted and removed from `doc.sheetsById` and `doc.sheetsByIndex`
-**Side Effects -** sheet is deleted and removed from `doc.sheetsById`, `doc.sheetsByIndex`, `doc.sheetsById`

_also available as `sheet.del()`_

#### `duplicate(options)` (async) :id=fn-duplicate
> Duplicate this sheet within this document
|Param|Type|Required|Description
|---|---|---|---
| `options` | Object | - |
| `options.title` | String | - | Name/title for new sheet, must be unique within the document<br>_defaults to something like "Copy of [sheet.title]" if not provided_ |
| `options.index` | Number<br>_int >= 0_ | - | Where to insert the new sheet (zero-indexed)<br>_defaults to 0 (first)_ |
| `options.id` | Number<br>_int >= 1_ | - | unique ID to use for new sheet<br>_defaults to new unique id generated by google_ |

- ↩️ **Returns** - [GoogleSpreadsheetRow](classes/google-spreadsheet-row) (in a promise)
-**Side Effects -** new sheet is creted, sheets in parent doc are updated (`sheetsByIndex`, `sheetsByTitle`, `sheetsById`)

#### `copyToSpreadsheet(destinationSpreadsheetId)` (async) :id=fn-copyToSpreadsheet
> Copy this sheet to another document
> Copy this sheet to a different document
Param|Type|Required|Description
---|---|---|---
`destinationSpreadsheetId`|String|✅|ID of another spreadsheet document

-**Side Effects -** sheet is copied to the other doc

?> The authentication method being used must have access to the destination document as well
?> The authentication method being used must have write access to the destination document as well

2 changes: 1 addition & 1 deletion docs/classes/google-spreadsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Param|Type|Required|Description
`props`|Object|-|Object of all sheet properties
`props.sheetId`|Number<br>_positive int_|-|Sheet ID, cannot be chagned after setting<br>_easiest to just let google handle it_
`props.headerValues`|[String]|-|Sets the contents of the first row, to be used in row-based interactions
`props.headerRowIndex`|Number|-|Set custom header row index (defaults to 1)
`props.headerRowIndex`|Number|-|Set custom header row index (1-indexed)<br>_defaults to 1 (first)_
`props.[more]`|...|-|_See [GoogleSpreadsheetWorksheet](classes/google-spreadsheet-worksheet#basic-document-properties) for more props_


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

async duplicateSheet() {
async duplicate(options = {}) {
// Request type = `duplicateSheet`
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#DuplicateSheetRequest
const response = await this._makeSingleUpdateRequest('duplicateSheet', {
sourceSheetId: this.sheetId,
...options.index !== undefined && { insertSheetIndex: options.index },
...options.id && { newSheetId: options.id },
...options.title && { newSheetName: options.title },
});
const newSheetId = response.properties.sheetId;
return this._spreadsheet.sheetsById[newSheetId];
}

async findReplace() {
Expand Down
Loading

0 comments on commit 4cec425

Please sign in to comment.