2023-03-09 09:13:15 -08:00
|
|
|
import type { IExecuteFunctions, IDataObject, INodeExecutionData } from 'n8n-workflow';
|
2022-11-15 05:57:07 -08:00
|
|
|
import { GoogleSheet } from '../helpers/GoogleSheet';
|
|
|
|
import { getSpreadsheetId } from '../helpers/GoogleSheets.utils';
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { GoogleSheets, ResourceLocator } from '../helpers/GoogleSheets.types';
|
2022-11-15 05:57:07 -08:00
|
|
|
import * as spreadsheet from './spreadsheet/SpreadSheet.resource';
|
|
|
|
import * as sheet from './sheet/Sheet.resource';
|
|
|
|
|
|
|
|
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
2023-10-09 07:58:16 -07:00
|
|
|
let operationResult: INodeExecutionData[] = [];
|
2022-11-15 05:57:07 -08:00
|
|
|
|
|
|
|
try {
|
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
|
|
|
|
|
|
|
const googleSheets = {
|
|
|
|
resource,
|
|
|
|
operation,
|
|
|
|
} as GoogleSheets;
|
|
|
|
|
2023-10-09 07:58:16 -07:00
|
|
|
let results: INodeExecutionData[] | undefined;
|
2022-11-15 05:57:07 -08:00
|
|
|
if (googleSheets.resource === 'sheet') {
|
|
|
|
const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject;
|
2023-10-26 02:33:11 -07:00
|
|
|
const spreadsheetId = getSpreadsheetId(
|
|
|
|
this.getNode(),
|
|
|
|
mode as ResourceLocator,
|
|
|
|
value as string,
|
|
|
|
);
|
2022-11-15 05:57:07 -08:00
|
|
|
|
|
|
|
const googleSheet = new GoogleSheet(spreadsheetId, this);
|
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
let sheetId = '';
|
2024-01-10 05:30:09 -08:00
|
|
|
let sheetName = '';
|
|
|
|
|
2022-11-15 05:57:07 -08:00
|
|
|
if (operation !== 'create') {
|
2024-01-10 05:30:09 -08:00
|
|
|
const sheetWithinDocument = this.getNodeParameter('sheetName', 0, undefined, {
|
2022-11-15 05:57:07 -08:00
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
2024-01-10 05:30:09 -08:00
|
|
|
const { mode: sheetMode } = this.getNodeParameter('sheetName', 0) as {
|
|
|
|
mode: ResourceLocator;
|
|
|
|
};
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2024-01-10 05:30:09 -08:00
|
|
|
const result = await googleSheet.spreadsheetGetSheet(
|
|
|
|
this.getNode(),
|
|
|
|
sheetMode,
|
|
|
|
sheetWithinDocument,
|
|
|
|
);
|
|
|
|
sheetId = result.sheetId.toString();
|
|
|
|
sheetName = result.title;
|
2022-11-15 05:57:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case 'create':
|
|
|
|
sheetName = spreadsheetId;
|
|
|
|
break;
|
|
|
|
case 'delete':
|
2022-12-01 00:39:03 -08:00
|
|
|
sheetName = sheetId;
|
2022-11-15 05:57:07 -08:00
|
|
|
break;
|
|
|
|
case 'remove':
|
2022-12-01 00:39:03 -08:00
|
|
|
sheetName = `${spreadsheetId}||${sheetId}`;
|
2022-11-15 05:57:07 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-09 07:58:16 -07:00
|
|
|
results = await sheet[googleSheets.operation].execute.call(
|
|
|
|
this,
|
|
|
|
googleSheet,
|
|
|
|
sheetName,
|
|
|
|
sheetId,
|
2022-11-15 05:57:07 -08:00
|
|
|
);
|
|
|
|
} else if (googleSheets.resource === 'spreadsheet') {
|
2023-10-09 07:58:16 -07:00
|
|
|
results = await spreadsheet[googleSheets.operation].execute.call(this);
|
|
|
|
}
|
|
|
|
if (results?.length) {
|
|
|
|
operationResult = operationResult.concat(results);
|
2022-11-15 05:57:07 -08:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
operationResult.push({ json: this.getInputData(0)[0].json, error: err });
|
|
|
|
} else {
|
2022-12-01 00:39:03 -08:00
|
|
|
if (
|
|
|
|
err.message &&
|
|
|
|
(err.message.toLowerCase().includes('bad request') ||
|
|
|
|
err.message.toLowerCase().includes('uknown error')) &&
|
|
|
|
err.description
|
|
|
|
) {
|
|
|
|
err.message = err.description;
|
|
|
|
err.description = undefined;
|
|
|
|
}
|
2022-11-15 05:57:07 -08:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [operationResult];
|
|
|
|
}
|