2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject, ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
|
|
|
|
import { NodeOperationError } 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 { ResourceLocator } from '../helpers/GoogleSheets.types';
|
2022-11-15 05:57:07 -08:00
|
|
|
|
|
|
|
export async function getSheets(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2022-12-01 00:39:03 -08:00
|
|
|
const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject;
|
|
|
|
const spreadsheetId = getSpreadsheetId(mode as ResourceLocator, value as string);
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
const sheet = new GoogleSheet(spreadsheetId, this);
|
|
|
|
const responseData = await sheet.spreadsheetGetSheets();
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
if (responseData === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'No data got returned');
|
|
|
|
}
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-12-02 12:54:28 -08:00
|
|
|
for (const entry of responseData.sheets!) {
|
|
|
|
if (entry.properties!.sheetType !== 'GRID') {
|
2022-12-01 00:39:03 -08:00
|
|
|
continue;
|
2022-11-15 05:57:07 -08:00
|
|
|
}
|
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
returnData.push({
|
2022-12-02 12:54:28 -08:00
|
|
|
name: entry.properties!.title as string,
|
|
|
|
value: entry.properties!.sheetId as unknown as string,
|
2022-12-01 00:39:03 -08:00
|
|
|
});
|
2022-11-15 05:57:07 -08:00
|
|
|
}
|
2022-12-01 00:39:03 -08:00
|
|
|
|
|
|
|
return returnData;
|
2022-11-15 05:57:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSheetHeaderRow(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2022-12-01 00:39:03 -08:00
|
|
|
const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject;
|
|
|
|
const spreadsheetId = getSpreadsheetId(mode as ResourceLocator, value as string);
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
const sheet = new GoogleSheet(spreadsheetId, this);
|
|
|
|
let sheetWithinDocument = this.getNodeParameter('sheetName', undefined, {
|
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
if (sheetWithinDocument === 'gid=0') {
|
|
|
|
sheetWithinDocument = '0';
|
|
|
|
}
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
const sheetName = await sheet.spreadsheetGetSheetNameById(sheetWithinDocument);
|
|
|
|
const sheetData = await sheet.getData(`${sheetName}!1:1`, 'FORMATTED_VALUE');
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
if (sheetData === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'No data got returned');
|
|
|
|
}
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
const columns = sheet.testFilter(sheetData, 0, 0);
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-11-15 05:57:07 -08:00
|
|
|
|
2022-12-01 00:39:03 -08:00
|
|
|
for (const column of columns) {
|
|
|
|
returnData.push({
|
|
|
|
name: column as unknown as string,
|
|
|
|
value: column as unknown as string,
|
|
|
|
});
|
2022-11-15 05:57:07 -08:00
|
|
|
}
|
2022-12-01 00:39:03 -08:00
|
|
|
|
|
|
|
return returnData;
|
2022-11-15 05:57:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSheetHeaderRowAndAddColumn(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData = await getSheetHeaderRow.call(this);
|
|
|
|
returnData.push({
|
|
|
|
name: 'New column ...',
|
|
|
|
value: 'newColumn',
|
|
|
|
});
|
|
|
|
const columnToMatchOn = this.getNodeParameter('columnToMatchOn', 0) as string;
|
|
|
|
return returnData.filter((column) => column.value !== columnToMatchOn);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSheetHeaderRowWithGeneratedColumnNames(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData = await getSheetHeaderRow.call(this);
|
|
|
|
return returnData.map((column, i) => {
|
|
|
|
if (column.value !== '') return column;
|
|
|
|
const indexBasedValue = `col_${i + 1}`;
|
|
|
|
return {
|
|
|
|
name: indexBasedValue,
|
|
|
|
value: indexBasedValue,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSheetHeaderRowAndSkipEmpty(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData = await getSheetHeaderRow.call(this);
|
|
|
|
return returnData.filter((column) => column.value);
|
|
|
|
}
|