From 0aa23d557d7c12a9e500e09876340cc122aa906c Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Thu, 1 Feb 2024 10:46:34 +0000 Subject: [PATCH] fix(Microsoft Excel 365 Node): Upsert append new rows at the end of used range, option to append at the end of selected range (#8461) --- .../Microsoft/Excel/MicrosoftExcel.node.ts | 3 +- .../Excel/v2/actions/versionDescription.ts | 2 +- .../v2/actions/worksheet/upsert.operation.ts | 52 ++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/Microsoft/Excel/MicrosoftExcel.node.ts b/packages/nodes-base/nodes/Microsoft/Excel/MicrosoftExcel.node.ts index 9c9d0318756de..1963970ed7088 100644 --- a/packages/nodes-base/nodes/Microsoft/Excel/MicrosoftExcel.node.ts +++ b/packages/nodes-base/nodes/Microsoft/Excel/MicrosoftExcel.node.ts @@ -13,12 +13,13 @@ export class MicrosoftExcel extends VersionedNodeType { group: ['input'], subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Consume Microsoft Excel API', - defaultVersion: 2, + defaultVersion: 2.1, }; const nodeVersions: IVersionedNodeType['nodeVersions'] = { 1: new MicrosoftExcelV1(baseDescription), 2: new MicrosoftExcelV2(baseDescription), + 2.1: new MicrosoftExcelV2(baseDescription), }; super(nodeVersions, baseDescription); diff --git a/packages/nodes-base/nodes/Microsoft/Excel/v2/actions/versionDescription.ts b/packages/nodes-base/nodes/Microsoft/Excel/v2/actions/versionDescription.ts index 76deccbad3b84..e0f83ca5bb9ae 100644 --- a/packages/nodes-base/nodes/Microsoft/Excel/v2/actions/versionDescription.ts +++ b/packages/nodes-base/nodes/Microsoft/Excel/v2/actions/versionDescription.ts @@ -10,7 +10,7 @@ export const versionDescription: INodeTypeDescription = { name: 'microsoftExcel', icon: 'file:excel.svg', group: ['input'], - version: 2, + version: [2, 2.1], subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Consume Microsoft Excel API', defaults: { diff --git a/packages/nodes-base/nodes/Microsoft/Excel/v2/actions/worksheet/upsert.operation.ts b/packages/nodes-base/nodes/Microsoft/Excel/v2/actions/worksheet/upsert.operation.ts index f7a85cea00eb9..fc615c349434f 100644 --- a/packages/nodes-base/nodes/Microsoft/Excel/v2/actions/worksheet/upsert.operation.ts +++ b/packages/nodes-base/nodes/Microsoft/Excel/v2/actions/worksheet/upsert.operation.ts @@ -138,6 +138,19 @@ const properties: INodeProperties[] = [ placeholder: 'Add Option', default: {}, options: [ + { + displayName: 'Append After Selected Range', + name: 'appendAfterSelectedRange', + type: 'boolean', + default: false, + description: 'Whether to append data after the selected range or used range', + displayOptions: { + show: { + '/dataMode': ['autoMap', 'define'], + '/useRange': [true], + }, + }, + }, { displayName: 'RAW Data', name: 'rawData', @@ -185,6 +198,7 @@ export async function execute( items: INodeExecutionData[], ): Promise { const returnData: INodeExecutionData[] = []; + const nodeVersion = this.getNode().typeVersion; try { const workbookId = this.getNodeParameter('workbook', 0, undefined, { @@ -287,6 +301,27 @@ export async function execute( ); } + const appendAfterSelectedRange = this.getNodeParameter( + 'options.appendAfterSelectedRange', + 0, + false, + ) as boolean; + + //remove empty rows from the end + if (nodeVersion > 2 && !appendAfterSelectedRange && updateSummary.updatedData.length) { + for (let i = updateSummary.updatedData.length - 1; i >= 0; i--) { + if ( + updateSummary.updatedData[i].every( + (item) => item === '' || item === undefined || item === null, + ) + ) { + updateSummary.updatedData.pop(); + } else { + break; + } + } + } + if (updateSummary.appendData.length) { const appendValues: string[][] = []; const columnsRow = (worksheetData.values as string[][])[0]; @@ -304,9 +339,24 @@ export async function execute( updateSummary.updatedData = updateSummary.updatedData.concat(appendValues); const [rangeFrom, rangeTo] = range.split(':'); + const cellDataTo = rangeTo.match(/([a-zA-Z]{1,10})([0-9]{0,10})/) || []; + let lastRow = cellDataTo[2]; + + if (nodeVersion > 2 && !appendAfterSelectedRange) { + const { address } = await microsoftApiRequest.call( + this, + 'GET', + `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/usedRange`, + undefined, + { select: 'address' }, + ); + + const addressTo = (address as string).split('!')[1].split(':')[1]; + lastRow = addressTo.match(/([a-zA-Z]{1,10})([0-9]{0,10})/)![2]; + } - range = `${rangeFrom}:${cellDataTo[1]}${Number(cellDataTo[2]) + appendValues.length}`; + range = `${rangeFrom}:${cellDataTo[1]}${Number(lastRow) + appendValues.length}`; } responseData = await microsoftApiRequest.call(