From 1b3417390b0e84eed7c52a0ae4d577b6e69ecf60 Mon Sep 17 00:00:00 2001 From: ricardo Date: Thu, 26 Mar 2020 15:44:48 -0400 Subject: [PATCH] :zap: added lookup and fixed issue with add columns operation --- .../Microsoft/Excel/MicrosoftExcel.node.ts | 72 ++++++++- .../nodes/Microsoft/Excel/TableDescription.ts | 138 ++++++++++++++++++ 2 files changed, 206 insertions(+), 4 deletions(-) diff --git a/packages/nodes-base/nodes/Microsoft/Excel/MicrosoftExcel.node.ts b/packages/nodes-base/nodes/Microsoft/Excel/MicrosoftExcel.node.ts index d7c372c2db..4edab92d35 100644 --- a/packages/nodes-base/nodes/Microsoft/Excel/MicrosoftExcel.node.ts +++ b/packages/nodes-base/nodes/Microsoft/Excel/MicrosoftExcel.node.ts @@ -173,10 +173,35 @@ export class MicrosoftExcel implements INodeType { if (additionalFields.index) { body.index = additionalFields.index as number; } - const values: any[][] = []; + + // Get table columns to eliminate any columns not needed on the input + responseData = await microsoftApiRequest.call(this, 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`, {}, qs); + const columns = responseData.value.map((column: IDataObject) => (column.name)); + + const cleanedItems: IDataObject[] = []; + + // Delete columns the excel table does not have for (const item of items) { - values.push(Object.values(item.json)); + for (const key of Object.keys(item.json)) { + if (!columns.includes(key)) { + const property = { ...item.json }; + delete property[key]; + cleanedItems.push(property); + } + } } + + // Map the keys to the column index + const values: any[][] = []; + let value = []; + for (const item of cleanedItems) { + for (const column of columns) { + value.push(item[column]); + } + values.push(value); + value = []; + } + body.values = values; const { id } = await microsoftApiRequest.call(this, 'POST', `/drive/items/${workbookId}/workbook/createSession`, { persistChanges: true }); responseData = await microsoftApiRequest.call(this, 'POST', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/rows/add`, body, {}, '', { 'workbook-session-id': id }); @@ -204,8 +229,7 @@ export class MicrosoftExcel implements INodeType { responseData = responseData.value; } if (!rawData) { - //@ts-ignore - responseData = responseData.map(column => ({ name: column.name })); + responseData = responseData.map((column: IDataObject) => ({ name: column.name })); } else { const dataProperty = this.getNodeParameter('dataProperty', i) as string; responseData = { [dataProperty] : responseData }; @@ -251,6 +275,46 @@ export class MicrosoftExcel implements INodeType { } } } + if (operation === 'lookup') { + for (let i = 0; i < length; i++) { + const workbookId = this.getNodeParameter('workbook', 0) as string; + const worksheetId = this.getNodeParameter('worksheet', 0) as string; + const tableId = this.getNodeParameter('table', 0) as string; + const lookupColumn = this.getNodeParameter('lookupColumn', 0) as string; + const lookupValue = this.getNodeParameter('lookupValue', 0) as string; + const options = this.getNodeParameter('options', 0) as IDataObject; + + responseData = await microsoftApiRequestAllItemsSkip.call(this, 'value', 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/rows`, {}, qs); + + qs['$select'] = 'name'; + let columns = await microsoftApiRequestAllItemsSkip.call(this, 'value', 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`, {}, qs); + columns = columns.map((column: IDataObject) => column.name); + for (let i = 0; i < responseData.length; i++) { + for (let y = 0; y < columns.length; y++) { + object[columns[y]] = responseData[i].values[0][y]; + } + result.push({ ...object }); + } + responseData = result; + + if (!columns.includes(lookupColumn)) { + throw new Error(`Column ${lookupColumn} does not exist on the table selected`); + } + + if (options.returnAllMatches) { + + responseData = responseData.filter((data: IDataObject) => { + return (data[lookupColumn]?.toString() === lookupValue ); + }); + + } else { + + responseData = responseData.find((data: IDataObject) => { + return (data[lookupColumn]?.toString() === lookupValue ); + }); + } + } + } } if (resource === 'workbook') { for (let i = 0; i < length; i++) { diff --git a/packages/nodes-base/nodes/Microsoft/Excel/TableDescription.ts b/packages/nodes-base/nodes/Microsoft/Excel/TableDescription.ts index 8c491a16e1..ea8b78b9d6 100644 --- a/packages/nodes-base/nodes/Microsoft/Excel/TableDescription.ts +++ b/packages/nodes-base/nodes/Microsoft/Excel/TableDescription.ts @@ -28,6 +28,11 @@ export const tableOperations = [ value: 'getRows', description: 'Retrieve a list of tablerows', }, + { + name: 'Lookup', + value: 'lookup', + description: 'Looks for a specific column value and then returns the matching row' + }, ], default: 'addRow', description: 'The operation to perform.', @@ -484,4 +489,137 @@ export const tableFields = [ }, ] }, +/* -------------------------------------------------------------------------- */ +/* table:lookup */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'Workbook', + name: 'workbook', + type: 'options', + required: true, + typeOptions: { + loadOptionsMethod: 'getWorkbooks', + }, + displayOptions: { + show: { + operation: [ + 'lookup', + ], + resource: [ + 'table', + ], + }, + }, + default: '', + }, + { + displayName: 'Worksheet', + name: 'worksheet', + type: 'options', + required: true, + typeOptions: { + loadOptionsMethod: 'getworksheets', + loadOptionsDependsOn: [ + 'workbook', + ], + }, + displayOptions: { + show: { + operation: [ + 'lookup', + ], + resource: [ + 'table', + ], + }, + }, + default: '', + }, + { + displayName: 'Table', + name: 'table', + type: 'options', + required: true, + typeOptions: { + loadOptionsMethod: 'getTables', + loadOptionsDependsOn: [ + 'worksheet', + ], + }, + displayOptions: { + show: { + operation: [ + 'lookup', + ], + resource: [ + 'table', + ], + }, + }, + default: '', + }, + { + displayName: 'Lookup Column', + name: 'lookupColumn', + type: 'string', + default: '', + placeholder: 'Email', + required: true, + displayOptions: { + show: { + resource: [ + 'table', + ], + operation: [ + 'lookup' + ], + }, + }, + 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, + displayOptions: { + show: { + resource: [ + 'table', + ], + operation: [ + 'lookup' + ], + }, + }, + description: 'The value to look for in column.', + }, + { + displayName: 'Options', + name: 'options', + type: 'collection', + placeholder: 'Add Option', + default: {}, + displayOptions: { + show: { + resource: [ + 'table', + ], + operation: [ + 'lookup', + ], + }, + }, + options: [ + { + displayName: 'Return All Matches', + name: 'returnAllMatches', + type: 'boolean', + default: false, + description: 'By default only the first result gets returned. If options gets set all found matches get returned.', + }, + ], + } ] as INodeProperties[];