Fix
This commit is contained in:
parent
f8c0a3b5c0
commit
004e13a349
|
@ -158,12 +158,12 @@ const SCHEMA: Integration = {
|
||||||
|
|
||||||
class GoogleSheetsIntegration implements DatasourcePlus {
|
class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
private readonly config: GoogleSheetsConfig
|
private readonly config: GoogleSheetsConfig
|
||||||
private client: GoogleSpreadsheet
|
private readonly spreadsheetId: string
|
||||||
|
private client: GoogleSpreadsheet = undefined!
|
||||||
|
|
||||||
constructor(config: GoogleSheetsConfig) {
|
constructor(config: GoogleSheetsConfig) {
|
||||||
this.config = config
|
this.config = config
|
||||||
const spreadsheetId = this.cleanSpreadsheetUrl(this.config.spreadsheetId)
|
this.spreadsheetId = this.cleanSpreadsheetUrl(this.config.spreadsheetId)
|
||||||
this.client = new GoogleSpreadsheet(spreadsheetId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async testConnection(): Promise<ConnectionInfo> {
|
async testConnection(): Promise<ConnectionInfo> {
|
||||||
|
@ -252,7 +252,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
access_token: tokenResponse.access_token,
|
access_token: tokenResponse.access_token,
|
||||||
})
|
})
|
||||||
|
|
||||||
this.client.useOAuth2Client(oauthClient)
|
this.client = new GoogleSpreadsheet(this.spreadsheetId, oauthClient)
|
||||||
await this.client.loadInfo()
|
await this.client.loadInfo()
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
// this happens for xlsx imports
|
// this happens for xlsx imports
|
||||||
|
@ -385,13 +385,17 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buildRowObject(headers: string[], values: string[], rowNumber: number) {
|
buildRowObject(
|
||||||
|
headers: string[],
|
||||||
|
values: Record<string, string>,
|
||||||
|
rowNumber: number
|
||||||
|
) {
|
||||||
const rowObject: { rowNumber: number } & Row = {
|
const rowObject: { rowNumber: number } & Row = {
|
||||||
rowNumber,
|
rowNumber,
|
||||||
_id: rowNumber.toString(),
|
_id: rowNumber.toString(),
|
||||||
}
|
}
|
||||||
for (let i = 0; i < headers.length; i++) {
|
for (let i = 0; i < headers.length; i++) {
|
||||||
rowObject[headers[i]] = values[i]
|
rowObject[headers[i]] = values[headers[i]]
|
||||||
}
|
}
|
||||||
return rowObject
|
return rowObject
|
||||||
}
|
}
|
||||||
|
@ -475,7 +479,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
typeof query.row === "string" ? JSON.parse(query.row) : query.row
|
typeof query.row === "string" ? JSON.parse(query.row) : query.row
|
||||||
const row = await sheet.addRow(rowToInsert)
|
const row = await sheet.addRow(rowToInsert)
|
||||||
return [
|
return [
|
||||||
this.buildRowObject(sheet.headerValues, row._rawData, row._rowNumber),
|
this.buildRowObject(sheet.headerValues, row.toObject(), row.rowNumber),
|
||||||
]
|
]
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error writing to google sheets", err)
|
console.error("Error writing to google sheets", err)
|
||||||
|
@ -493,7 +497,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
}
|
}
|
||||||
const rows = await sheet.addRows(rowsToInsert)
|
const rows = await sheet.addRows(rowsToInsert)
|
||||||
return rows.map(row =>
|
return rows.map(row =>
|
||||||
this.buildRowObject(sheet.headerValues, row._rawData, row._rowNumber)
|
this.buildRowObject(sheet.headerValues, row.toObject(), row.rowNumber)
|
||||||
)
|
)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error bulk writing to google sheets", err)
|
console.error("Error bulk writing to google sheets", err)
|
||||||
|
@ -548,7 +552,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
let response = []
|
let response = []
|
||||||
for (let row of filtered) {
|
for (let row of filtered) {
|
||||||
response.push(
|
response.push(
|
||||||
this.buildRowObject(headerValues, row._rawData, row._rowNumber)
|
this.buildRowObject(headerValues, row.toObject(), row._rowNumber)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -598,10 +602,10 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
const updateValues =
|
const updateValues =
|
||||||
typeof query.row === "string" ? JSON.parse(query.row) : query.row
|
typeof query.row === "string" ? JSON.parse(query.row) : query.row
|
||||||
for (let key in updateValues) {
|
for (let key in updateValues) {
|
||||||
row[key] = updateValues[key]
|
row.set(key, updateValues[key])
|
||||||
|
|
||||||
if (row[key] === null) {
|
if (row.get(key) === null) {
|
||||||
row[key] = ""
|
row.set(key, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
const { type, subtype, constraints } = query.table.schema[key]
|
const { type, subtype, constraints } = query.table.schema[key]
|
||||||
|
@ -615,7 +619,11 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
}
|
}
|
||||||
await row.save()
|
await row.save()
|
||||||
return [
|
return [
|
||||||
this.buildRowObject(sheet.headerValues, row._rawData, row._rowNumber),
|
this.buildRowObject(
|
||||||
|
sheet.headerValues,
|
||||||
|
row.toObject(),
|
||||||
|
row.rowNumber
|
||||||
|
),
|
||||||
]
|
]
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Row does not exist.")
|
throw new Error("Row does not exist.")
|
||||||
|
|
Loading…
Reference in New Issue