🐛 (sheets) Make sure update cells do not overwrite existing…
This commit is contained in:
@ -0,0 +1,22 @@
|
||||
import { Variable, Cell } from '@typebot.io/schemas'
|
||||
import { parseVariables } from '@typebot.io/variables/parseVariables'
|
||||
|
||||
export const parseNewCellValuesObject =
|
||||
(variables: Variable[]) =>
|
||||
(
|
||||
cells: Cell[],
|
||||
headerValues?: string[]
|
||||
): { [key: string]: { value: string; columnIndex: number } } =>
|
||||
cells.reduce((row, cell) => {
|
||||
return !cell.column || !cell.value
|
||||
? row
|
||||
: {
|
||||
...row,
|
||||
[cell.column]: {
|
||||
value: parseVariables(variables)(cell.value),
|
||||
columnIndex: headerValues?.findIndex(
|
||||
(headerValue) => headerValue === cell.column
|
||||
),
|
||||
},
|
||||
}
|
||||
}, {})
|
@ -1,7 +1,7 @@
|
||||
import { Variable, Cell } from '@typebot.io/schemas'
|
||||
import { parseVariables } from '@typebot.io/variables/parseVariables'
|
||||
|
||||
export const parseCellValues =
|
||||
export const parseNewRowObject =
|
||||
(variables: Variable[]) =>
|
||||
(cells: Cell[]): { [key: string]: string } =>
|
||||
cells.reduce((row, cell) => {
|
@ -3,7 +3,7 @@ import {
|
||||
GoogleSheetsInsertRowOptions,
|
||||
ChatLog,
|
||||
} from '@typebot.io/schemas'
|
||||
import { parseCellValues } from './helpers/parseCellValues'
|
||||
import { parseNewRowObject } from './helpers/parseNewRowObject'
|
||||
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
|
||||
import { ExecuteIntegrationResponse } from '../../../types'
|
||||
|
||||
@ -24,7 +24,7 @@ export const insertRow = async (
|
||||
spreadsheetId: options.spreadsheetId,
|
||||
})
|
||||
|
||||
const parsedValues = parseCellValues(variables)(options.cellsToInsert)
|
||||
const parsedValues = parseNewRowObject(variables)(options.cellsToInsert)
|
||||
|
||||
try {
|
||||
await doc.loadInfo()
|
||||
|
@ -3,7 +3,7 @@ import {
|
||||
GoogleSheetsUpdateRowOptions,
|
||||
ChatLog,
|
||||
} from '@typebot.io/schemas'
|
||||
import { parseCellValues } from './helpers/parseCellValues'
|
||||
import { parseNewCellValuesObject } from './helpers/parseNewCellValuesObject'
|
||||
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
|
||||
import { ExecuteIntegrationResponse } from '../../../types'
|
||||
import { matchFilter } from './helpers/matchFilter'
|
||||
@ -36,8 +36,6 @@ export const updateRow = async (
|
||||
spreadsheetId: options.spreadsheetId,
|
||||
})
|
||||
|
||||
const parsedValues = parseCellValues(variables)(options.cellsToUpsert)
|
||||
|
||||
await doc.loadInfo()
|
||||
const sheet = doc.sheetsById[Number(sheetId)]
|
||||
const rows = await sheet.getRows()
|
||||
@ -55,13 +53,24 @@ export const updateRow = async (
|
||||
return { outgoingEdgeId, logs }
|
||||
}
|
||||
|
||||
const parsedValues = parseNewCellValuesObject(variables)(
|
||||
options.cellsToUpsert,
|
||||
sheet.headerValues
|
||||
)
|
||||
|
||||
try {
|
||||
for (const filteredRow of filteredRows) {
|
||||
const rowIndex = filteredRow.rowNumber - 2 // -1 for 1-indexing, -1 for header row
|
||||
const cellsRange = filteredRow.a1Range.split('!').pop()
|
||||
await sheet.loadCells(cellsRange)
|
||||
const rowIndex = filteredRow.rowNumber - 1
|
||||
for (const key in parsedValues) {
|
||||
rows[rowIndex].set(key, parsedValues[key])
|
||||
const cellToUpdate = sheet.getCell(
|
||||
rowIndex,
|
||||
parsedValues[key].columnIndex
|
||||
)
|
||||
cellToUpdate.value = parsedValues[key].value
|
||||
}
|
||||
await rows[rowIndex].save()
|
||||
await sheet.saveUpdatedCells()
|
||||
}
|
||||
|
||||
logs.push({
|
||||
|
Reference in New Issue
Block a user