feat(Microsoft Excel 365 Node): Overhaul

This commit is contained in:
Michael Kret
2023-05-02 12:44:25 +03:00
committed by GitHub
parent 25fe14be56
commit 5364a2dff3
75 changed files with 8049 additions and 675 deletions

View File

@@ -0,0 +1,2 @@
export * as loadOptions from './loadOptions';
export * as listSearch from './listSearch';

View File

@@ -0,0 +1,134 @@
import type {
IDataObject,
ILoadOptionsFunctions,
INodeListSearchItems,
INodeListSearchResult,
} from 'n8n-workflow';
import { microsoftApiRequest } from '../transport';
export async function searchWorkbooks(
this: ILoadOptionsFunctions,
filter?: string,
paginationToken?: string,
): Promise<INodeListSearchResult> {
const q = filter ? encodeURI(`.xlsx AND ${filter}`) : '.xlsx';
let response: IDataObject = {};
if (paginationToken) {
response = await microsoftApiRequest.call(
this,
'GET',
'',
undefined,
undefined,
paginationToken, // paginationToken contains the full URL
);
} else {
response = await microsoftApiRequest.call(
this,
'GET',
`/drive/root/search(q='${q}')`,
undefined,
{
select: 'id,name,webUrl',
$top: 100,
},
);
}
return {
results: (response.value as IDataObject[]).map((workbook: IDataObject) => {
return {
name: (workbook.name as string).replace('.xlsx', ''),
value: workbook.id as string,
url: workbook.webUrl as string,
};
}),
paginationToken: response['@odata.nextLink'],
};
}
export async function getWorksheetsList(
this: ILoadOptionsFunctions,
): Promise<INodeListSearchResult> {
const workbookRLC = this.getNodeParameter('workbook') as IDataObject;
const workbookId = workbookRLC.value as string;
let workbookURL = workbookRLC.cachedResultUrl as string;
if (workbookURL.includes('1drv.ms')) {
workbookURL = `https://onedrive.live.com/edit.aspx?resid=${workbookId}`;
}
let response: IDataObject = {};
response = await microsoftApiRequest.call(
this,
'GET',
`/drive/items/${workbookId}/workbook/worksheets`,
undefined,
{
select: 'id,name',
},
);
return {
results: (response.value as IDataObject[]).map((worksheet: IDataObject) => ({
name: worksheet.name as string,
value: worksheet.id as string,
url: `${workbookURL}&activeCell=${encodeURIComponent(worksheet.name as string)}!A1`,
})),
};
}
export async function getWorksheetTables(
this: ILoadOptionsFunctions,
): Promise<INodeListSearchResult> {
const workbookRLC = this.getNodeParameter('workbook') as IDataObject;
const workbookId = workbookRLC.value as string;
let workbookURL = workbookRLC.cachedResultUrl as string;
if (workbookURL.includes('1drv.ms')) {
workbookURL = `https://onedrive.live.com/edit.aspx?resid=${workbookId}`;
}
const worksheetId = this.getNodeParameter('worksheet', undefined, {
extractValue: true,
}) as string;
let response: IDataObject = {};
response = await microsoftApiRequest.call(
this,
'GET',
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables`,
undefined,
);
const results: INodeListSearchItems[] = [];
for (const table of response.value as IDataObject[]) {
const name = table.name as string;
const value = table.id as string;
const { address } = await microsoftApiRequest.call(
this,
'GET',
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${value}/range`,
undefined,
{
select: 'address',
},
);
const [sheetName, sheetRange] = address.split('!' as string);
const url = `${workbookURL}&activeCell=${encodeURIComponent(sheetName as string)}${
sheetRange ? '!' + (sheetRange as string) : ''
}`;
results.push({ name, value, url });
}
return { results };
}

View File

@@ -0,0 +1,89 @@
import type { IDataObject, ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
import { microsoftApiRequest } from '../transport';
export async function getWorksheetColumnRow(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const workbookId = this.getNodeParameter('workbook', undefined, {
extractValue: true,
}) as string;
const worksheetId = this.getNodeParameter('worksheet', undefined, {
extractValue: true,
}) as string;
let range = this.getNodeParameter('range', '') as string;
let columns: string[] = [];
if (range === '') {
const worksheetData = await microsoftApiRequest.call(
this,
'GET',
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/usedRange`,
undefined,
{ select: 'values' },
);
columns = worksheetData.values[0] as string[];
} else {
const [rangeFrom, rangeTo] = range.split(':');
const cellDataFrom = rangeFrom.match(/([a-zA-Z]{1,10})([0-9]{0,10})/) || [];
const cellDataTo = rangeTo.match(/([a-zA-Z]{1,10})([0-9]{0,10})/) || [];
range = `${rangeFrom}:${cellDataTo[1]}${cellDataFrom[2]}`;
const worksheetData = await microsoftApiRequest.call(
this,
'PATCH',
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/range(address='${range}')`,
{ select: 'values' },
);
columns = worksheetData.values[0] as string[];
}
const returnData: INodePropertyOptions[] = [];
for (const column of columns) {
returnData.push({
name: column,
value: column,
});
}
return returnData;
}
export async function getWorksheetColumnRowSkipColumnToMatchOn(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const returnData = await getWorksheetColumnRow.call(this);
const columnToMatchOn = this.getNodeParameter('columnToMatchOn', 0) as string;
return returnData.filter((column) => column.value !== columnToMatchOn);
}
export async function getTableColumns(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const workbookId = this.getNodeParameter('workbook', undefined, {
extractValue: true,
}) as string;
const worksheetId = this.getNodeParameter('worksheet', undefined, {
extractValue: true,
}) as string;
const tableId = this.getNodeParameter('table', undefined, {
extractValue: true,
}) as string;
const response = await microsoftApiRequest.call(
this,
'GET',
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`,
{},
);
return (response.value as IDataObject[]).map((column) => ({
name: column.name as string,
value: column.name as string,
}));
}