Skip to content

Commit

Permalink
fix(Google Sheets Node): Read operation execute for each item (#7800)
Browse files Browse the repository at this point in the history
Github issue / Community forum post (link here to close automatically):
  • Loading branch information
michael-radency authored and netroy committed Nov 29, 2023
1 parent 1b9da04 commit 2563433
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 51 deletions.
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Google/Sheet/GoogleSheets.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -111,70 +111,93 @@ export async function execute(
sheet: GoogleSheet,
sheetName: string,
): Promise<INodeExecutionData[]> {
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 2563433

Please sign in to comment.