2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2020-03-15 16:51:31 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeExecutionData,
|
2020-03-15 16:51:31 -07:00
|
|
|
INodePropertyOptions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2023-02-27 19:39:43 -08:00
|
|
|
JsonObject,
|
2020-03-15 16:51:31 -07:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-03-15 16:51:31 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
microsoftApiRequest,
|
|
|
|
microsoftApiRequestAllItems,
|
|
|
|
microsoftApiRequestAllItemsSkip,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { workbookFields, workbookOperations } from './WorkbookDescription';
|
2020-03-15 16:51:31 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { worksheetFields, worksheetOperations } from './WorksheetDescription';
|
2020-03-15 16:51:31 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { tableFields, tableOperations } from './TableDescription';
|
2020-03-15 16:51:31 -07:00
|
|
|
|
|
|
|
export class MicrosoftExcel implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Microsoft Excel',
|
|
|
|
name: 'microsoftExcel',
|
2021-03-25 09:10:02 -07:00
|
|
|
icon: 'file:excel.svg',
|
2020-03-15 16:51:31 -07:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Microsoft Excel API',
|
2020-03-15 16:51:31 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'Microsoft Excel',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
2020-03-22 10:03:56 -07:00
|
|
|
name: 'microsoftExcelOAuth2Api',
|
2020-03-15 16:51:31 -07:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-03-15 16:51:31 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Table',
|
|
|
|
value: 'table',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Represents an Excel table',
|
2020-03-15 16:51:31 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Workbook',
|
|
|
|
value: 'workbook',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Workbook is the top level object which contains related workbook objects such as worksheets, tables, ranges, etc',
|
2020-03-15 16:51:31 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Worksheet',
|
|
|
|
value: 'worksheet',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'An Excel worksheet is a grid of cells. It can contain data, tables, charts, etc.',
|
2020-03-15 16:51:31 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'workbook',
|
|
|
|
},
|
|
|
|
...workbookOperations,
|
|
|
|
...workbookFields,
|
|
|
|
...worksheetOperations,
|
|
|
|
...worksheetFields,
|
|
|
|
...tableOperations,
|
|
|
|
...tableFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the workbooks to display them to user so that they can
|
2020-03-15 16:51:31 -07:00
|
|
|
// select them easily
|
|
|
|
async getWorkbooks(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const qs: IDataObject = {
|
|
|
|
select: 'id,name',
|
|
|
|
};
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const workbooks = await microsoftApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
"/drive/root/search(q='.xlsx')",
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-03-15 16:51:31 -07:00
|
|
|
for (const workbook of workbooks) {
|
|
|
|
const workbookName = workbook.name;
|
|
|
|
const workbookId = workbook.id;
|
|
|
|
returnData.push({
|
|
|
|
name: workbookName,
|
|
|
|
value: workbookId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the worksheets to display them to user so that they can
|
2020-03-15 16:51:31 -07:00
|
|
|
// select them easily
|
|
|
|
async getworksheets(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const workbookId = this.getCurrentNodeParameter('workbook');
|
|
|
|
const qs: IDataObject = {
|
|
|
|
select: 'id,name',
|
|
|
|
};
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const worksheets = await microsoftApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-03-15 16:51:31 -07:00
|
|
|
for (const worksheet of worksheets) {
|
|
|
|
const worksheetName = worksheet.name;
|
|
|
|
const worksheetId = worksheet.id;
|
|
|
|
returnData.push({
|
|
|
|
name: worksheetName,
|
|
|
|
value: worksheetId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the tables to display them to user so that they can
|
2020-03-15 16:51:31 -07:00
|
|
|
// 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[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const tables = await microsoftApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-03-15 16:51:31 -07:00
|
|
|
for (const table of tables) {
|
|
|
|
const tableName = table.name;
|
|
|
|
const tableId = table.id;
|
|
|
|
returnData.push({
|
|
|
|
name: tableName,
|
|
|
|
value: tableId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-03-15 16:51:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-03-28 11:08:39 -07:00
|
|
|
let qs: IDataObject = {};
|
2020-03-15 16:51:31 -07:00
|
|
|
const result: IDataObject[] = [];
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-03-28 11:08:39 -07:00
|
|
|
|
2020-03-15 16:51:31 -07:00
|
|
|
if (resource === 'table') {
|
|
|
|
//https://docs.microsoft.com/en-us/graph/api/table-post-rows?view=graph-rest-1.0&tabs=http
|
|
|
|
if (operation === 'addRow') {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
// TODO: At some point it should be possible to use item dependent parameters.
|
|
|
|
// Is however important to then not make one separate request each.
|
|
|
|
const workbookId = this.getNodeParameter('workbook', 0) as string;
|
|
|
|
const worksheetId = this.getNodeParameter('worksheet', 0) as string;
|
|
|
|
const tableId = this.getNodeParameter('table', 0) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', 0);
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
2020-03-28 11:08:39 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.index) {
|
|
|
|
body.index = additionalFields.index as number;
|
|
|
|
}
|
2020-03-26 12:44:48 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Get table columns to eliminate any columns not needed on the input
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
const columns = responseData.value.map((column: IDataObject) => column.name);
|
2020-03-26 12:44:48 -07:00
|
|
|
|
2022-12-02 06:25:21 -08:00
|
|
|
const rows: any[][] = [];
|
2020-03-26 12:44:48 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Bring the items into the correct format
|
|
|
|
for (const item of items) {
|
|
|
|
const row = [];
|
|
|
|
for (const column of columns) {
|
|
|
|
row.push(item.json[column]);
|
|
|
|
}
|
|
|
|
rows.push(row);
|
2020-03-26 12:44:48 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.values = rows;
|
2022-08-17 08:50:24 -07:00
|
|
|
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 },
|
|
|
|
);
|
2020-03-28 11:08:39 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: 0 } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: 0 } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
2020-03-28 11:08:39 -07:00
|
|
|
}
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
|
|
|
//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++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
qs = {};
|
|
|
|
const workbookId = this.getNodeParameter('workbook', i) as string;
|
|
|
|
const worksheetId = this.getNodeParameter('worksheet', i) as string;
|
|
|
|
const tableId = this.getNodeParameter('table', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
|
|
|
const rawData = this.getNodeParameter('rawData', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (rawData) {
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (filters.fields) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = filters.fields;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequestAllItemsSkip.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$top = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.value;
|
|
|
|
}
|
|
|
|
if (!rawData) {
|
|
|
|
responseData = responseData.map((column: IDataObject) => ({ name: column.name }));
|
|
|
|
} else {
|
|
|
|
const dataProperty = this.getNodeParameter('dataProperty', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = { [dataProperty]: responseData };
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
2020-03-28 11:08:39 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-03-28 11:08:39 -07:00
|
|
|
}
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//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++) {
|
2020-03-28 11:08:39 -07:00
|
|
|
qs = {};
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const workbookId = this.getNodeParameter('workbook', i) as string;
|
|
|
|
const worksheetId = this.getNodeParameter('worksheet', i) as string;
|
|
|
|
const tableId = this.getNodeParameter('table', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
|
|
|
const rawData = this.getNodeParameter('rawData', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (rawData) {
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (filters.fields) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = filters.fields;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequestAllItemsSkip.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/rows`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const rowsQs = { ...qs };
|
2022-12-02 12:54:28 -08:00
|
|
|
rowsQs.$top = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/rows`,
|
|
|
|
{},
|
|
|
|
rowsQs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.value;
|
|
|
|
}
|
|
|
|
if (!rawData) {
|
|
|
|
const columnsQs = { ...qs };
|
2022-12-02 12:54:28 -08:00
|
|
|
columnsQs.$select = 'name';
|
2021-07-19 23:58:54 -07:00
|
|
|
// TODO: That should probably be cached in the future
|
2022-08-17 08:50:24 -07:00
|
|
|
let columns = await microsoftApiRequestAllItemsSkip.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`,
|
|
|
|
{},
|
|
|
|
columnsQs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
//@ts-ignore
|
2022-08-17 08:50:24 -07:00
|
|
|
columns = columns.map((column) => column.name);
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < responseData.length; index++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
const object: IDataObject = {};
|
|
|
|
for (let y = 0; y < columns.length; y++) {
|
2022-12-02 12:54:28 -08:00
|
|
|
object[columns[y]] = responseData[index].values[0][y];
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ ...object }),
|
2022-12-02 12:54:28 -08:00
|
|
|
{ itemData: { item: index } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const dataProperty = this.getNodeParameter('dataProperty', i) as string;
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ [dataProperty]: responseData }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 12:44:48 -07:00
|
|
|
if (operation === 'lookup') {
|
|
|
|
for (let i = 0; i < length; i++) {
|
2020-03-28 11:08:39 -07:00
|
|
|
qs = {};
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const workbookId = this.getNodeParameter('workbook', i) as string;
|
|
|
|
const worksheetId = this.getNodeParameter('worksheet', i) as string;
|
|
|
|
const tableId = this.getNodeParameter('table', i) as string;
|
|
|
|
const lookupColumn = this.getNodeParameter('lookupColumn', i) as string;
|
|
|
|
const lookupValue = this.getNodeParameter('lookupValue', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2020-03-26 12:44:48 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequestAllItemsSkip.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/rows`,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
);
|
2020-03-26 12:44:48 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = 'name';
|
2021-07-19 23:58:54 -07:00
|
|
|
// TODO: That should probably be cached in the future
|
2022-08-17 08:50:24 -07:00
|
|
|
let columns = await microsoftApiRequestAllItemsSkip.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/tables/${tableId}/columns`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
columns = columns.map((column: IDataObject) => column.name);
|
2020-03-28 11:08:39 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!columns.includes(lookupColumn)) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), responseData as JsonObject, {
|
2022-08-17 08:50:24 -07:00
|
|
|
message: `Column ${lookupColumn} does not exist on the table selected`,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-03-28 11:08:39 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
result.length = 0;
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < responseData.length; index++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
const object: IDataObject = {};
|
|
|
|
for (let y = 0; y < columns.length; y++) {
|
2022-12-02 12:54:28 -08:00
|
|
|
object[columns[y]] = responseData[index].values[0][y];
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
result.push({ ...object });
|
2020-03-26 12:44:48 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.returnAllMatches) {
|
|
|
|
responseData = result.filter((data: IDataObject) => {
|
2022-08-17 08:50:24 -07:00
|
|
|
return data[lookupColumn]?.toString() === lookupValue;
|
2021-07-19 23:58:54 -07:00
|
|
|
});
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
responseData = result.find((data: IDataObject) => {
|
2022-08-17 08:50:24 -07:00
|
|
|
return data[lookupColumn]?.toString() === lookupValue;
|
2021-07-19 23:58:54 -07:00
|
|
|
});
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-03-26 12:44:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
|
|
|
if (resource === 'workbook') {
|
|
|
|
for (let i = 0; i < length; i++) {
|
2020-03-28 11:08:39 -07:00
|
|
|
qs = {};
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
//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;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
|
|
|
if (additionalFields.name) {
|
|
|
|
body.name = additionalFields.name;
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
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 },
|
|
|
|
);
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (filters.fields) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = filters.fields;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
"/drive/root/search(q='.xlsx')",
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$top = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
"/drive/root/search(q='.xlsx')",
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.value;
|
|
|
|
}
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
2020-03-28 11:08:39 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (responseData !== undefined) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-03-28 11:08:39 -07:00
|
|
|
}
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resource === 'worksheet') {
|
|
|
|
for (let i = 0; i < length; i++) {
|
2020-03-28 11:08:39 -07:00
|
|
|
qs = {};
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
//https://docs.microsoft.com/en-us/graph/api/workbook-list-worksheets?view=graph-rest-1.0&tabs=http
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const workbookId = this.getNodeParameter('workbook', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2020-03-15 16:51:31 -07:00
|
|
|
if (filters.fields) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = filters.fields;
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$top = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.value;
|
|
|
|
}
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//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;
|
2022-11-18 05:31:38 -08:00
|
|
|
const rawData = this.getNodeParameter('rawData', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (rawData) {
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (filters.fields) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = filters.fields;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
}
|
2020-03-28 11:08:39 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/drive/items/${workbookId}/workbook/worksheets/${worksheetId}/range(address='${range}')`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-03-28 11:08:39 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!rawData) {
|
|
|
|
const keyRow = this.getNodeParameter('keyRow', i) as number;
|
|
|
|
const dataStartRow = this.getNodeParameter('dataStartRow', i) as number;
|
|
|
|
if (responseData.values === null) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), responseData as JsonObject, {
|
2022-08-17 08:50:24 -07:00
|
|
|
message: 'Range did not return data',
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
const keyValues = responseData.values[keyRow];
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = dataStartRow; index < responseData.values.length; index++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
const object: IDataObject = {};
|
|
|
|
for (let y = 0; y < keyValues.length; y++) {
|
2022-12-02 12:54:28 -08:00
|
|
|
object[keyValues[y]] = responseData.values[index][y];
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ ...object }),
|
2022-12-02 12:54:28 -08:00
|
|
|
{ itemData: { item: index } },
|
2022-08-30 08:55:33 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const dataProperty = this.getNodeParameter('dataProperty', i) as string;
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ [dataProperty]: responseData }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 11:08:39 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-03-15 16:51:31 -07:00
|
|
|
}
|
|
|
|
}
|