mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
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)
This commit is contained in:
parent
3128dca1fa
commit
1e02d73ad7
|
@ -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);
|
||||
|
|
|
@ -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: {
|
||||
|
|
|
@ -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<INodeExecutionData[]> {
|
||||
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})/) || [];
|
||||
|
||||
range = `${rangeFrom}:${cellDataTo[1]}${Number(cellDataTo[2]) + appendValues.length}`;
|
||||
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(lastRow) + appendValues.length}`;
|
||||
}
|
||||
|
||||
responseData = await microsoftApiRequest.call(
|
||||
|
|
Loading…
Reference in a new issue