Skip to content

Commit

Permalink
fix(Microsoft Excel 365 Node): Upsert append new rows at the end of u…
Browse files Browse the repository at this point in the history
…sed range, option to append at the end of selected range (#8461)
  • Loading branch information
michael-radency authored and netroy committed Feb 2, 2024
1 parent 0fd3470 commit 0aa23d5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -185,6 +198,7 @@ export async function execute(
items: INodeExecutionData[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
const nodeVersion = this.getNode().typeVersion;

try {
const workbookId = this.getNodeParameter('workbook', 0, undefined, {
Expand Down Expand Up @@ -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];
Expand All @@ -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(
Expand Down

0 comments on commit 0aa23d5

Please sign in to comment.