1
0
Fork 0
mirror of synced 2024-07-28 17:46:09 +12:00

Move the -2 magic number in its right place

This commit is contained in:
Adria Navarro 2023-09-28 13:55:35 +02:00
parent 84be779b05
commit 8be76f1f97

View file

@ -322,14 +322,14 @@ class GoogleSheetsIntegration implements DatasourcePlus {
case Operation.UPDATE: case Operation.UPDATE:
return this.update({ return this.update({
// exclude the header row and zero index // exclude the header row and zero index
rowIndex: json.extra?.idFilter?.equal?.rowNumber - 2, rowIndex: json.extra?.idFilter?.equal?.rowNumber,
sheet, sheet,
row: json.body, row: json.body,
}) })
case Operation.DELETE: case Operation.DELETE:
return this.delete({ return this.delete({
// exclude the header row and zero index // exclude the header row and zero index
rowIndex: json.extra?.idFilter?.equal?.rowNumber - 2, rowIndex: json.extra?.idFilter?.equal?.rowNumber,
sheet, sheet,
}) })
case Operation.CREATE_TABLE: case Operation.CREATE_TABLE:
@ -540,12 +540,21 @@ class GoogleSheetsIntegration implements DatasourcePlus {
} }
} }
private async getRowByIndex(sheetTitle: string, rowIndex: number) {
const sheet = this.client.sheetsByTitle[sheetTitle]
const rows = await sheet.getRows()
// We substract 2, as the SDK is skipping the header automatically and Google Spreadsheets is base 1
const row = rows[rowIndex - 2]
return { sheet, row }
}
async update(query: { sheet: string; rowIndex: number; row: any }) { async update(query: { sheet: string; rowIndex: number; row: any }) {
try { try {
await this.connect() await this.connect()
const sheet = this.client.sheetsByTitle[query.sheet] const { sheet, row } = await this.getRowByIndex(
const rows = await sheet.getRows() query.sheet,
const row = rows[query.rowIndex] query.rowIndex
)
if (row) { if (row) {
const updateValues = const updateValues =
typeof query.row === "string" ? JSON.parse(query.row) : query.row typeof query.row === "string" ? JSON.parse(query.row) : query.row
@ -571,9 +580,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
async delete(query: { sheet: string; rowIndex: number }) { async delete(query: { sheet: string; rowIndex: number }) {
await this.connect() await this.connect()
const sheet = this.client.sheetsByTitle[query.sheet] const { row } = await this.getRowByIndex(query.sheet, query.rowIndex)
const rows = await sheet.getRows()
const row = rows[query.rowIndex]
if (row) { if (row) {
await row.delete() await row.delete()
return [ return [