From 56600b86893fcebf3c6f9990c43f833c604c5163 Mon Sep 17 00:00:00 2001 From: Michael Kret Date: Fri, 24 Nov 2023 06:24:27 +0200 Subject: [PATCH] :zap: looping support for google sheets read operation --- .../nodes/Google/Sheet/GoogleSheets.node.ts | 3 +- .../Sheet/v2/actions/sheet/read.operation.ts | 121 +++++++++++------- .../Sheet/v2/actions/versionDescription.ts | 2 +- 3 files changed, 75 insertions(+), 51 deletions(-) diff --git a/packages/nodes-base/nodes/Google/Sheet/GoogleSheets.node.ts b/packages/nodes-base/nodes/Google/Sheet/GoogleSheets.node.ts index 3c5fd6ad49800..47784b22a5fa5 100644 --- a/packages/nodes-base/nodes/Google/Sheet/GoogleSheets.node.ts +++ b/packages/nodes-base/nodes/Google/Sheet/GoogleSheets.node.ts @@ -11,7 +11,7 @@ export class GoogleSheets extends VersionedNodeType { name: 'googleSheets', icon: 'file:googleSheets.svg', group: ['input', 'output'], - defaultVersion: 4.1, + defaultVersion: 4.2, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Read, update and write data to Google Sheets', }; @@ -22,6 +22,7 @@ export class GoogleSheets extends VersionedNodeType { 3: new GoogleSheetsV2(baseDescription), 4: new GoogleSheetsV2(baseDescription), 4.1: new GoogleSheetsV2(baseDescription), + 4.2: new GoogleSheetsV2(baseDescription), }; super(nodeVersions, baseDescription); diff --git a/packages/nodes-base/nodes/Google/Sheet/v2/actions/sheet/read.operation.ts b/packages/nodes-base/nodes/Google/Sheet/v2/actions/sheet/read.operation.ts index d9779625b8c42..f9c777e6e3ff9 100644 --- a/packages/nodes-base/nodes/Google/Sheet/v2/actions/sheet/read.operation.ts +++ b/packages/nodes-base/nodes/Google/Sheet/v2/actions/sheet/read.operation.ts @@ -12,7 +12,7 @@ import type { SheetRangeData, ValueRenderOption, } from '../../helpers/GoogleSheets.types'; -import { generatePairedItemData } from '../../../../../../utils/utilities'; + import { dataLocationOnSheet, outputFormatting } from './commonDescription'; export const description: SheetProperties = [ @@ -111,70 +111,93 @@ export async function execute( sheet: GoogleSheet, sheetName: string, ): Promise { - const options = this.getNodeParameter('options', 0, {}); - const outputFormattingOption = - ((options.outputFormatting as IDataObject)?.values as IDataObject) || {}; - - const dataLocationOnSheetOptions = - ((options.dataLocationOnSheet as IDataObject)?.values as RangeDetectionOptions) || {}; + const items = this.getInputData(); + const nodeVersion = this.getNode().typeVersion; + let length = 1; - if (dataLocationOnSheetOptions.rangeDefinition === undefined) { - dataLocationOnSheetOptions.rangeDefinition = 'detectAutomatically'; + if (nodeVersion > 4.1) { + length = items.length; } - const range = getRangeString(sheetName, dataLocationOnSheetOptions); + const returnData: INodeExecutionData[] = []; - const valueRenderMode = (outputFormattingOption.general || - 'UNFORMATTED_VALUE') as ValueRenderOption; - const dateTimeRenderOption = (outputFormattingOption.date || 'FORMATTED_STRING') as string; + for (let itemIndex = 0; itemIndex < length; itemIndex++) { + const options = this.getNodeParameter('options', itemIndex, {}); + const outputFormattingOption = + ((options.outputFormatting as IDataObject)?.values as IDataObject) || {}; - const sheetData = (await sheet.getData( - range, - valueRenderMode, - dateTimeRenderOption, - )) as SheetRangeData; + const dataLocationOnSheetOptions = + ((options.dataLocationOnSheet as IDataObject)?.values as RangeDetectionOptions) || {}; - if (sheetData === undefined || sheetData.length === 0) { - return []; - } + if (dataLocationOnSheetOptions.rangeDefinition === undefined) { + dataLocationOnSheetOptions.rangeDefinition = 'detectAutomatically'; + } - const { data, headerRow, firstDataRow } = prepareSheetData(sheetData, dataLocationOnSheetOptions); + const range = getRangeString(sheetName, dataLocationOnSheetOptions); - let responseData = []; + const valueRenderMode = (outputFormattingOption.general || + 'UNFORMATTED_VALUE') as ValueRenderOption; + const dateTimeRenderOption = (outputFormattingOption.date || 'FORMATTED_STRING') as string; - const lookupValues = this.getNodeParameter('filtersUI.values', 0, []) as ILookupValues[]; + const sheetData = (await sheet.getData( + range, + valueRenderMode, + dateTimeRenderOption, + )) as SheetRangeData; - if (lookupValues.length) { - const returnAllMatches = options.returnAllMatches === 'returnAllMatches' ? true : false; + if (sheetData === undefined || sheetData.length === 0) { + return []; + } - const items = this.getInputData(); - for (let i = 1; i < items.length; i++) { - const itemLookupValues = this.getNodeParameter('filtersUI.values', i, []) as ILookupValues[]; - if (itemLookupValues.length) { - lookupValues.push(...itemLookupValues); + const { data, headerRow, firstDataRow } = prepareSheetData( + sheetData, + dataLocationOnSheetOptions, + ); + + let responseData = []; + + const lookupValues = this.getNodeParameter( + 'filtersUI.values', + itemIndex, + [], + ) as ILookupValues[]; + + if (lookupValues.length) { + const returnAllMatches = options.returnAllMatches === 'returnAllMatches' ? true : false; + + if (nodeVersion <= 4.1) { + for (let i = 1; i < items.length; i++) { + const itemLookupValues = this.getNodeParameter( + 'filtersUI.values', + i, + [], + ) as ILookupValues[]; + if (itemLookupValues.length) { + lookupValues.push(...itemLookupValues); + } + } } + + responseData = await sheet.lookupValues( + data as string[][], + headerRow, + firstDataRow, + lookupValues, + returnAllMatches, + ); + } else { + responseData = sheet.structureArrayDataByColumn(data as string[][], headerRow, firstDataRow); } - responseData = await sheet.lookupValues( - data as string[][], - headerRow, - firstDataRow, - lookupValues, - returnAllMatches, + returnData.push( + ...responseData.map((item, index) => { + return { + json: item, + pairedItem: { item: itemIndex }, + }; + }), ); - } else { - responseData = sheet.structureArrayDataByColumn(data as string[][], headerRow, firstDataRow); } - const items = this.getInputData(); - const pairedItem = generatePairedItemData(items.length); - - const returnData: INodeExecutionData[] = responseData.map((item, index) => { - return { - json: item, - pairedItem, - }; - }); - return returnData; } diff --git a/packages/nodes-base/nodes/Google/Sheet/v2/actions/versionDescription.ts b/packages/nodes-base/nodes/Google/Sheet/v2/actions/versionDescription.ts index 170ecb50f53d9..89360623831d8 100644 --- a/packages/nodes-base/nodes/Google/Sheet/v2/actions/versionDescription.ts +++ b/packages/nodes-base/nodes/Google/Sheet/v2/actions/versionDescription.ts @@ -9,7 +9,7 @@ export const versionDescription: INodeTypeDescription = { name: 'googleSheets', icon: 'file:googleSheets.svg', group: ['input', 'output'], - version: [3, 4, 4.1], + version: [3, 4, 4.1, 4.2], subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Read, update and write data to Google Sheets', defaults: {