2022-11-15 05:57:07 -08:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import { IDataObject, INodeExecutionData } from 'n8n-workflow';
|
|
|
|
import * as sheet from './sheet/Sheet.resource';
|
|
|
|
import * as spreadsheet from './spreadsheet/SpreadSheet.resource';
|
|
|
|
import { GoogleSheet } from '../helpers/GoogleSheet';
|
|
|
|
import { getSpreadsheetId } from '../helpers/GoogleSheets.utils';
|
|
|
|
import { GoogleSheets, ResourceLocator } from '../helpers/GoogleSheets.types';
|
|
|
|
|
|
|
|
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const operationResult: INodeExecutionData[] = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
|
|
|
|
|
|
|
const googleSheets = {
|
|
|
|
resource,
|
|
|
|
operation,
|
|
|
|
} as GoogleSheets;
|
|
|
|
|
|
|
|
if (googleSheets.resource === 'sheet') {
|
|
|
|
const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject;
|
|
|
|
const spreadsheetId = getSpreadsheetId(mode as ResourceLocator, value as string);
|
|
|
|
|
|
|
|
const googleSheet = new GoogleSheet(spreadsheetId, this);
|
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
let sheetId = '';
|
2022-11-15 05:57:07 -08:00
|
|
|
if (operation !== 'create') {
|
2022-12-01 00:39:03 -08:00
|
|
|
sheetId = this.getNodeParameter('sheetName', 0, undefined, {
|
2022-11-15 05:57:07 -08:00
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
|
|
|
}
|
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
if (sheetId === 'gid=0') {
|
|
|
|
sheetId = '0';
|
2022-11-15 05:57:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let sheetName = '';
|
|
|
|
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;
|
|
|
|
default:
|
2022-12-01 00:39:03 -08:00
|
|
|
sheetName = await googleSheet.spreadsheetGetSheetNameById(sheetId);
|
2022-11-15 05:57:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
operationResult.push(
|
2022-12-01 00:39:03 -08:00
|
|
|
...(await sheet[googleSheets.operation].execute.call(
|
|
|
|
this,
|
|
|
|
googleSheet,
|
|
|
|
sheetName,
|
|
|
|
sheetId,
|
|
|
|
)),
|
2022-11-15 05:57:07 -08:00
|
|
|
);
|
|
|
|
} else if (googleSheets.resource === 'spreadsheet') {
|
|
|
|
operationResult.push(...(await spreadsheet[googleSheets.operation].execute.call(this)));
|
|
|
|
}
|
|
|
|
} 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];
|
|
|
|
}
|