From 29ee4fab61c2f364b249b91c7561b176e78f37ac Mon Sep 17 00:00:00 2001 From: Elias Meire Date: Tue, 23 Apr 2024 10:29:39 +0200 Subject: [PATCH] fix(Google Sheets Node): Fix "Append or Update" on an empty sheet (#9175) Co-authored-by: Michael Kret --- .../ResourceMapper/ResourceMapper.vue | 16 ++++++++++++++-- .../actions/sheet/appendOrUpdate.operation.ts | 17 +++++++++-------- .../Google/Sheet/v2/helpers/GoogleSheet.ts | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/editor-ui/src/components/ResourceMapper/ResourceMapper.vue b/packages/editor-ui/src/components/ResourceMapper/ResourceMapper.vue index 6dcc4247e4..6316af72aa 100644 --- a/packages/editor-ui/src/components/ResourceMapper/ResourceMapper.vue +++ b/packages/editor-ui/src/components/ResourceMapper/ResourceMapper.vue @@ -19,6 +19,7 @@ import { fieldCannotBeDeleted, parseResourceMapperFieldName } from '@/utils/node import { isResourceMapperValue } from '@/utils/typeGuards'; import { i18n as locale } from '@/plugins/i18n'; import { useNDVStore } from '@/stores/ndv.store'; +import { useWorkflowsStore } from '@/stores/workflows.store'; type Props = { parameter: INodeProperties; @@ -32,6 +33,7 @@ type Props = { const nodeTypesStore = useNodeTypesStore(); const ndvStore = useNDVStore(); +const workflowsStore = useWorkflowsStore(); const props = withDefaults(defineProps(), { teleported: true, @@ -71,6 +73,16 @@ watch( }, ); +// Reload fields to map when node is executed +watch( + () => workflowsStore.getWorkflowExecution, + async (data) => { + if (data?.status === 'success' && state.paramValue.mappingMode === 'autoMapInputData') { + await initFetching(true); + } + }, +); + onMounted(async () => { if (props.node) { state.parameterValues = { @@ -199,9 +211,9 @@ const pluralFieldWord = computed(() => { ); }); -async function initFetching(inlineLading = false): Promise { +async function initFetching(inlineLoading = false): Promise { state.loadingError = false; - if (inlineLading) { + if (inlineLoading) { state.refreshInProgress = true; } else { state.loading = true; diff --git a/packages/nodes-base/nodes/Google/Sheet/v2/actions/sheet/appendOrUpdate.operation.ts b/packages/nodes-base/nodes/Google/Sheet/v2/actions/sheet/appendOrUpdate.operation.ts index 46521fb245..ea68b5b954 100644 --- a/packages/nodes-base/nodes/Google/Sheet/v2/actions/sheet/appendOrUpdate.operation.ts +++ b/packages/nodes-base/nodes/Google/Sheet/v2/actions/sheet/appendOrUpdate.operation.ts @@ -249,18 +249,23 @@ export async function execute( } } + const dataMode = + nodeVersion < 4 + ? (this.getNodeParameter('dataMode', 0) as string) + : (this.getNodeParameter('columns.mappingMode', 0) as string); + let columnNames: string[] = []; - const sheetData = await sheet.getData(sheetName, 'FORMATTED_VALUE'); + const sheetData = (await sheet.getData(sheetName, 'FORMATTED_VALUE')) ?? []; - if (sheetData?.[headerRow] === undefined) { + if (!sheetData[headerRow] && dataMode !== 'autoMapInputData') { throw new NodeOperationError( this.getNode(), `Could not retrieve the column names from row ${headerRow + 1}`, ); } - columnNames = sheetData[headerRow]; + columnNames = sheetData[headerRow] ?? []; const newColumns = new Set(); @@ -269,11 +274,6 @@ export async function execute( ? [this.getNodeParameter('columnToMatchOn', 0) as string] : (this.getNodeParameter('columns.matchingColumns', 0) as string[]); - const dataMode = - nodeVersion < 4 - ? (this.getNodeParameter('dataMode', 0) as string) - : (this.getNodeParameter('columns.mappingMode', 0) as string); - // TODO: Add support for multiple columns to match on in the next overhaul const keyIndex = columnNames.indexOf(columnsToMatchOn[0]); @@ -379,6 +379,7 @@ export async function execute( headerRow + 1, ); columnNames = newColumnNames; + sheetData[headerRow] = newColumnNames; newColumns.clear(); } diff --git a/packages/nodes-base/nodes/Google/Sheet/v2/helpers/GoogleSheet.ts b/packages/nodes-base/nodes/Google/Sheet/v2/helpers/GoogleSheet.ts index 6305577a94..b7d917cfc2 100644 --- a/packages/nodes-base/nodes/Google/Sheet/v2/helpers/GoogleSheet.ts +++ b/packages/nodes-base/nodes/Google/Sheet/v2/helpers/GoogleSheet.ts @@ -476,7 +476,7 @@ export class GoogleSheet { const keyIndex = columnNames.indexOf(indexKey); - if (keyIndex === -1) { + if (keyIndex === -1 && !upsert) { throw new NodeOperationError( this.executeFunctions.getNode(), `Could not find column for key "${indexKey}"`,