mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Make it possible to return all matches in with GoogleSheet Lookup
This commit is contained in:
parent
78af036231
commit
09b339f019
|
@ -316,10 +316,11 @@ export class GoogleSheet {
|
||||||
* @param {number} keyRowIndex Index of the row which contains the keys
|
* @param {number} keyRowIndex Index of the row which contains the keys
|
||||||
* @param {number} dataStartRowIndex Index of the first row which contains data
|
* @param {number} dataStartRowIndex Index of the first row which contains data
|
||||||
* @param {ILookupValues[]} lookupValues The lookup values which decide what data to return
|
* @param {ILookupValues[]} lookupValues The lookup values which decide what data to return
|
||||||
|
* @param {boolean} [returnAllMatches] Returns all the found matches instead of only the first one
|
||||||
* @returns {Promise<IDataObject[]>}
|
* @returns {Promise<IDataObject[]>}
|
||||||
* @memberof GoogleSheet
|
* @memberof GoogleSheet
|
||||||
*/
|
*/
|
||||||
async lookupValues(inputData: string[][], keyRowIndex: number, dataStartRowIndex: number, lookupValues: ILookupValues[]): Promise<IDataObject[]> {
|
async lookupValues(inputData: string[][], keyRowIndex: number, dataStartRowIndex: number, lookupValues: ILookupValues[], returnAllMatches?: boolean): Promise<IDataObject[]> {
|
||||||
const keys: string[] = [];
|
const keys: string[] = [];
|
||||||
|
|
||||||
if (keyRowIndex < 0 || dataStartRowIndex < keyRowIndex || keyRowIndex >= inputData.length) {
|
if (keyRowIndex < 0 || dataStartRowIndex < keyRowIndex || keyRowIndex >= inputData.length) {
|
||||||
|
@ -351,13 +352,18 @@ export class GoogleSheet {
|
||||||
for (rowIndex = dataStartRowIndex; rowIndex < inputData.length; rowIndex++) {
|
for (rowIndex = dataStartRowIndex; rowIndex < inputData.length; rowIndex++) {
|
||||||
if (inputData[rowIndex][returnColumnIndex].toString() === lookupValue.lookupValue.toString()) {
|
if (inputData[rowIndex][returnColumnIndex].toString() === lookupValue.lookupValue.toString()) {
|
||||||
returnData.push(inputData[rowIndex]);
|
returnData.push(inputData[rowIndex]);
|
||||||
continue lookupLoop;
|
|
||||||
|
if (returnAllMatches !== true) {
|
||||||
|
continue lookupLoop;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If value could not be found add an empty one that the order of
|
// If value could not be found add an empty one that the order of
|
||||||
// the returned items stays the same
|
// the returned items stays the same
|
||||||
returnData.push([]);
|
if (returnAllMatches !== true) {
|
||||||
|
returnData.push([]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.structureData(returnData, 1, keys, true);
|
return this.structureData(returnData, 1, keys, true);
|
||||||
|
|
|
@ -267,6 +267,20 @@ export class GoogleSheets implements INodeType {
|
||||||
placeholder: 'Add Option',
|
placeholder: 'Add Option',
|
||||||
default: {},
|
default: {},
|
||||||
options: [
|
options: [
|
||||||
|
{
|
||||||
|
displayName: 'Return All Matches',
|
||||||
|
name: 'returnAllMatches',
|
||||||
|
type: 'boolean',
|
||||||
|
default: false,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
'/operation': [
|
||||||
|
'lookup',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
description: 'By default only the first result gets returned. If options gets set all found matches get returned.',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Value Input Mode',
|
displayName: 'Value Input Mode',
|
||||||
name: 'valueInputMode',
|
name: 'valueInputMode',
|
||||||
|
@ -436,7 +450,7 @@ export class GoogleSheets implements INodeType {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const returnData = await sheet.lookupValues(sheetData, keyRow, dataStartRow, lookupValues);
|
const returnData = await sheet.lookupValues(sheetData, keyRow, dataStartRow, lookupValues, options.returnAllMatches as boolean | undefined);
|
||||||
|
|
||||||
return [this.helpers.returnJsonArray(returnData)];
|
return [this.helpers.returnJsonArray(returnData)];
|
||||||
} else if (operation === 'read') {
|
} else if (operation === 'read') {
|
||||||
|
|
Loading…
Reference in a new issue