n8n-nodes-base
Version:
Base nodes of n8n
117 lines • 4.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.description = void 0;
exports.execute = execute;
const n8n_workflow_1 = require("n8n-workflow");
const utilities_1 = require("../../../../../../utils/utilities");
const transport_1 = require("../../transport");
const common_descriptions_1 = require("../common.descriptions");
const properties = [
common_descriptions_1.workbookRLC,
common_descriptions_1.worksheetRLC,
common_descriptions_1.tableRLC,
{
displayName: 'Lookup Column',
name: 'lookupColumn',
type: 'string',
default: '',
placeholder: 'Email',
required: true,
description: 'The name of the column in which to look for value',
},
{
displayName: 'Lookup Value',
name: 'lookupValue',
type: 'string',
default: '',
placeholder: 'frank@example.com',
required: true,
description: 'The value to look for in column',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add option',
default: {},
options: [
{
displayName: 'Return All Matches',
name: 'returnAllMatches',
type: 'boolean',
default: false,
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
description: 'By default only the first result gets returned. If options gets set all found matches get returned.',
},
],
},
];
const displayOptions = {
show: {
resource: ['table'],
operation: ['lookup'],
},
};
exports.description = (0, utilities_1.updateDisplayOptions)(displayOptions, properties);
async function execute(items) {
const returnData = [];
for (let i = 0; i < items.length; i++) {
const qs = {};
try {
const workbookId = this.getNodeParameter('workbook', i, undefined, {
extractValue: true,
});
const worksheetId = this.getNodeParameter('worksheet', i, undefined, {
extractValue: true,
});
const tableId = this.getNodeParameter('table', i, undefined, {
extractValue: true,
});
const lookupColumn = this.getNodeParameter('lookupColumn', i);
const lookupValue = this.getNodeParameter('lookupValue', i);
const options = this.getNodeParameter('options', i);
let responseData = await transport_1.microsoftApiRequestAllItemsSkip.call(this, 'value', 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/rows`, {}, {});
qs.$select = 'name';
// TODO: That should probably be cached in the future
let columns = await transport_1.microsoftApiRequestAllItemsSkip.call(this, 'value', 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`, {}, qs);
columns = columns.map((column) => column.name);
if (!columns.includes(lookupColumn)) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), responseData, {
message: `Column ${lookupColumn} does not exist on the table selected`,
});
}
const result = [];
for (let index = 0; index < responseData.length; index++) {
const object = {};
for (let y = 0; y < columns.length; y++) {
object[columns[y]] = responseData[index].values[0][y];
}
result.push({ ...object });
}
if (options.returnAllMatches) {
responseData = result.filter((data) => {
return data[lookupColumn]?.toString() === lookupValue;
});
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } });
returnData.push(...executionData);
}
else {
responseData = result.find((data) => {
return data[lookupColumn]?.toString() === lookupValue;
});
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } });
returnData.push(...executionData);
}
}
catch (error) {
if (this.continueOnFail()) {
const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } });
returnData.push(...executionErrorData);
continue;
}
throw error;
}
}
return returnData;
}
//# sourceMappingURL=lookup.operation.js.map