-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add spreadsheet column and row inserting
- Loading branch information
Showing
6 changed files
with
123 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { readCell, readCellRange } from "./read"; | ||
export { writeCell, writeCellRange } from "./write"; | ||
export { insertColumn, insertRow } from "./insert"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import asyncExponentialBackoff from "../utils/async-exponential-backoff"; | ||
import { letterToColumn } from "./cell"; | ||
import { getSheetDimensions, getSheets } from "./sheets"; | ||
|
||
// newColumnLetter: What letter the new column will have | ||
// If not including a specific column to insert it at, then insert at the end | ||
async function insertColumn(spreadsheetId: string, sheetId: number, newColumnLetter?: string) { | ||
const sheets = await getSheets(); | ||
let columnLetter = newColumnLetter; | ||
|
||
if (!columnLetter) { | ||
columnLetter = (await getSheetDimensions(spreadsheetId, sheetId)).column; | ||
} | ||
|
||
await asyncExponentialBackoff( | ||
async () => | ||
await sheets.spreadsheets.batchUpdate({ | ||
spreadsheetId, | ||
requestBody: { | ||
requests: [ | ||
{ | ||
insertDimension: { | ||
range: { | ||
sheetId, | ||
dimension: "COLUMNS", | ||
startIndex: letterToColumn(columnLetter), | ||
endIndex: letterToColumn(columnLetter) + 1, | ||
}, | ||
inheritFromBefore: letterToColumn(columnLetter) > 0, | ||
}, | ||
}, | ||
], | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
// newRowNumber: What letter the new column will have | ||
// If not including a specific row to insert it at, then insert at the end | ||
async function insertRow(spreadsheetId: string, sheetId: number, newRowNumber?: number) { | ||
const sheets = await getSheets(); | ||
let rowNumber = newRowNumber; | ||
|
||
if (!rowNumber) { | ||
rowNumber = (await getSheetDimensions(spreadsheetId, sheetId)).row; | ||
} | ||
|
||
await asyncExponentialBackoff( | ||
async () => | ||
await sheets.spreadsheets.batchUpdate({ | ||
spreadsheetId, | ||
requestBody: { | ||
requests: [ | ||
{ | ||
insertDimension: { | ||
range: { | ||
sheetId, | ||
dimension: "ROWS", | ||
startIndex: rowNumber, | ||
endIndex: rowNumber + 1, | ||
}, | ||
inheritFromBefore: rowNumber > 0, | ||
}, | ||
}, | ||
], | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
export { insertColumn, insertRow }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters