mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 22:54:05 -08:00
336 lines
13 KiB
TypeScript
336 lines
13 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
INodeExecutionData,
|
|
INodeTypeDescription,
|
|
INodeType,
|
|
ILoadOptionsFunctions,
|
|
INodePropertyOptions,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
microsoftApiRequest,
|
|
microsoftApiRequestAllItems,
|
|
microsoftApiRequestAllItemsSkip,
|
|
} from './GenericFunctions';
|
|
|
|
import {
|
|
workbookOperations,
|
|
workbookFields,
|
|
} from './WorkbookDescription';
|
|
|
|
import {
|
|
worksheetOperations,
|
|
worksheetFields,
|
|
} from './WorksheetDescription';
|
|
|
|
import {
|
|
tableOperations,
|
|
tableFields,
|
|
} from './TableDescription';
|
|
|
|
export class MicrosoftExcel implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Microsoft Excel',
|
|
name: 'microsoftExcel',
|
|
icon: 'file:excel.png',
|
|
group: ['input'],
|
|
version: 1,
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
description: 'Consume Microsoft Excel API.',
|
|
defaults: {
|
|
name: 'Microsoft Excel',
|
|
color: '#1c6d40',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'microsoftOAuth2Api',
|
|
required: true,
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
options: [
|
|
{
|
|
name: 'Table',
|
|
value: 'table',
|
|
description: 'Represents an Excel table.',
|
|
},
|
|
{
|
|
name: 'Workbook',
|
|
value: 'workbook',
|
|
description: 'Workbook is the top level object which contains related workbook objects such as worksheets, tables, ranges, etc.',
|
|
},
|
|
{
|
|
name: 'Worksheet',
|
|
value: 'worksheet',
|
|
description: 'An Excel worksheet is a grid of cells. It can contain data, tables, charts, etc.',
|
|
},
|
|
],
|
|
default: 'workbook',
|
|
description: 'The resource to operate on.',
|
|
},
|
|
...workbookOperations,
|
|
...workbookFields,
|
|
...worksheetOperations,
|
|
...worksheetFields,
|
|
...tableOperations,
|
|
...tableFields,
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
loadOptions: {
|
|
// Get all the workbooks to display them to user so that he can
|
|
// select them easily
|
|
async getWorkbooks(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const qs: IDataObject = {
|
|
select: 'id,name',
|
|
};
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const workbooks = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/root/search(q='.xlsx')`, {}, qs);
|
|
for (const workbook of workbooks) {
|
|
const workbookName = workbook.name;
|
|
const workbookId = workbook.id;
|
|
returnData.push({
|
|
name: workbookName,
|
|
value: workbookId,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
// Get all the worksheets to display them to user so that he can
|
|
// select them easily
|
|
async getworksheets(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const workbookId = this.getCurrentNodeParameter('workbook');
|
|
const qs: IDataObject = {
|
|
select: 'id,name',
|
|
};
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const worksheets = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/items/${workbookId}/workbook/worksheets`, {}, qs);
|
|
for (const worksheet of worksheets) {
|
|
const worksheetName = worksheet.name;
|
|
const worksheetId = worksheet.id;
|
|
returnData.push({
|
|
name: worksheetName,
|
|
value: worksheetId,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
// Get all the tables to display them to user so that he can
|
|
// select them easily
|
|
async getTables(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const workbookId = this.getCurrentNodeParameter('workbook');
|
|
const worksheetId = this.getCurrentNodeParameter('worksheet');
|
|
const qs: IDataObject = {
|
|
select: 'id,name',
|
|
};
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const tables = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables`, {}, qs);
|
|
for (const table of tables) {
|
|
const tableName = table.name;
|
|
const tableId = table.id;
|
|
returnData.push({
|
|
name: tableName,
|
|
value: tableId,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
}
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
const returnData: IDataObject[] = [];
|
|
const length = items.length as unknown as number;
|
|
const qs: IDataObject = {};
|
|
const result: IDataObject[] = [];
|
|
const object: IDataObject = {};
|
|
let responseData;
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
if (resource === 'table') {
|
|
//https://docs.microsoft.com/en-us/graph/api/table-post-rows?view=graph-rest-1.0&tabs=http
|
|
if (operation === 'addRow') {
|
|
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 additionalFields = this.getNodeParameter('additionalFields', 0) as IDataObject;
|
|
const body: IDataObject = {};
|
|
if (Object.keys(items[0].json).length === 0) {
|
|
throw new Error('Input cannot be empty');
|
|
}
|
|
if (additionalFields.index) {
|
|
body.index = additionalFields.index as number;
|
|
}
|
|
const values: any[][] = [];
|
|
for (const item of items) {
|
|
values.push(Object.values(item.json));
|
|
}
|
|
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 });
|
|
await microsoftApiRequest.call(this, 'POST', `/drive/items/${workbookId}/workbook/closeSession`, {}, {}, '', { 'workbook-session-id': id });
|
|
}
|
|
//https://docs.microsoft.com/en-us/graph/api/table-list-columns?view=graph-rest-1.0&tabs=http
|
|
if (operation === 'getColumns') {
|
|
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 returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
const rawData = this.getNodeParameter('rawData', i) as boolean;
|
|
if (rawData) {
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
|
if (filters.fields) {
|
|
qs['$select'] = filters.fields;
|
|
}
|
|
}
|
|
if (returnAll === true) {
|
|
responseData = await microsoftApiRequestAllItemsSkip.call(this, 'value', 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`, {}, qs);
|
|
} else {
|
|
qs['$top'] = this.getNodeParameter('limit', i) as number;
|
|
responseData = await microsoftApiRequest.call(this, 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`, {}, qs);
|
|
responseData = responseData.value;
|
|
}
|
|
if (!rawData) {
|
|
//@ts-ignore
|
|
responseData = responseData.map(column => ({ name: column.name }));
|
|
}
|
|
}
|
|
}
|
|
//https://docs.microsoft.com/en-us/graph/api/table-list-rows?view=graph-rest-1.0&tabs=http
|
|
if (operation === 'getRows') {
|
|
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 returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
const rawData = this.getNodeParameter('rawData', i) as boolean;
|
|
if (rawData) {
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
|
if (filters.fields) {
|
|
qs['$select'] = filters.fields;
|
|
}
|
|
}
|
|
if (returnAll === true) {
|
|
responseData = await microsoftApiRequestAllItemsSkip.call(this, 'value', 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/rows`, {}, qs);
|
|
} else {
|
|
qs['$top'] = this.getNodeParameter('limit', i) as number;
|
|
responseData = await microsoftApiRequest.call(this, 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/rows`, {}, qs);
|
|
responseData = responseData.value;
|
|
}
|
|
if (!rawData) {
|
|
qs['$select'] = 'name';
|
|
let columns = await microsoftApiRequestAllItemsSkip.call(this, 'value', 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`, {}, qs);
|
|
//@ts-ignore
|
|
columns = columns.map(column => 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 (resource === 'workbook') {
|
|
for (let i = 0; i < length; i++) {
|
|
//https://docs.microsoft.com/en-us/graph/api/worksheetcollection-add?view=graph-rest-1.0&tabs=http
|
|
if (operation === 'addWorksheet') {
|
|
const workbookId = this.getNodeParameter('workbook', i) as string;
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
const body: IDataObject = {};
|
|
if (additionalFields.name) {
|
|
body.name = additionalFields.name;
|
|
}
|
|
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/add`, body, {}, '', { 'workbook-session-id': id });
|
|
await microsoftApiRequest.call(this, 'POST', `/drive/items/${workbookId}/workbook/closeSession`, {}, {}, '', { 'workbook-session-id': id });
|
|
}
|
|
if (operation === 'getAll') {
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
|
if (filters.fields) {
|
|
qs['$select'] = filters.fields;
|
|
}
|
|
if (returnAll === true) {
|
|
responseData = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/root/search(q='.xlsx')`, {}, qs);
|
|
} else {
|
|
qs['$top'] = this.getNodeParameter('limit', i) as number;
|
|
responseData = await microsoftApiRequest.call(this, 'GET', `/drive/root/search(q='.xlsx')`, {}, qs);
|
|
responseData = responseData.value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (resource === 'worksheet') {
|
|
for (let i = 0; i < length; i++) {
|
|
//https://docs.microsoft.com/en-us/graph/api/workbook-list-worksheets?view=graph-rest-1.0&tabs=http
|
|
if (operation === 'getAll') {
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
const workbookId = this.getNodeParameter('workbook', i) as string;
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
|
if (filters.fields) {
|
|
qs['$select'] = filters.fields;
|
|
}
|
|
if (returnAll === true) {
|
|
responseData = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/items/${workbookId}/workbook/worksheets`, {}, qs);
|
|
} else {
|
|
qs['$top'] = this.getNodeParameter('limit', i) as number;
|
|
responseData = await microsoftApiRequest.call(this, 'GET', `/drive/items/${workbookId}/workbook/worksheets`, {}, qs);
|
|
responseData = responseData.value;
|
|
}
|
|
}
|
|
//https://docs.microsoft.com/en-us/graph/api/worksheet-range?view=graph-rest-1.0&tabs=http
|
|
if (operation === 'getContent') {
|
|
const workbookId = this.getNodeParameter('workbook', i) as string;
|
|
const worksheetId = this.getNodeParameter('worksheet', i) as string;
|
|
const range = this.getNodeParameter('range', i) as string;
|
|
const rawData = this.getNodeParameter('rawData', i) as boolean;
|
|
if (rawData) {
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
|
if (filters.fields) {
|
|
qs['$select'] = filters.fields;
|
|
}
|
|
}
|
|
responseData = await microsoftApiRequest.call(this, 'GET', `/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/range(address='${range}')`, {}, qs);
|
|
if (!rawData) {
|
|
const keyRow = this.getNodeParameter('keyRow', i) as number;
|
|
const dataStartRow = this.getNodeParameter('dataStartRow', i) as number;
|
|
if (responseData.values === null) {
|
|
throw new Error('Range did not return data');
|
|
}
|
|
const keyValues = responseData.values[keyRow];
|
|
for (let i = dataStartRow; i < responseData.values.length; i++) {
|
|
for (let y = 0; y < keyValues.length; y++) {
|
|
object[keyValues[y]] = responseData.values[i][y];
|
|
}
|
|
result.push({ ...object });
|
|
}
|
|
responseData = result;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (Array.isArray(responseData)) {
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
} else if (responseData !== undefined) {
|
|
returnData.push(responseData as IDataObject);
|
|
}
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|