feat(Google Sheets Node): Add "By Name" option to selector for Sheets (#8241)

Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
Elias Meire
2024-01-10 14:30:09 +01:00
committed by GitHub
parent e796e7f06d
commit dce28f9cb9
8 changed files with 87 additions and 46 deletions

View File

@@ -1,24 +1,26 @@
import get from 'lodash/get';
import type {
IDataObject,
IExecuteFunctions,
ILoadOptionsFunctions,
IDataObject,
IPollFunctions,
INode,
IPollFunctions,
} from 'n8n-workflow';
import { ApplicationError, NodeOperationError } from 'n8n-workflow';
import { utils as xlsxUtils } from 'xlsx';
import get from 'lodash/get';
import { apiRequest } from '../transport';
import type {
ILookupValues,
ISheetUpdateData,
ResourceLocator,
SheetCellDecoded,
SheetRangeData,
SheetRangeDecoded,
SpreadSheetResponse,
ValueInputOption,
ValueRenderOption,
} from './GoogleSheets.types';
import { removeEmptyColumns } from './GoogleSheets.utils';
import { getSheetId, removeEmptyColumns } from './GoogleSheets.utils';
export class GoogleSheet {
id: string;
@@ -116,32 +118,35 @@ export class GoogleSheet {
}
/**
* Returns the name of a sheet from a sheet id
* Returns the sheet within a spreadsheet based on name or ID
*/
async spreadsheetGetSheetNameById(node: INode, sheetId: string) {
async spreadsheetGetSheet(node: INode, mode: ResourceLocator, value: string) {
const query = {
fields: 'sheets.properties',
};
const response = await apiRequest.call(
const response = (await apiRequest.call(
this.executeFunctions,
'GET',
`/v4/spreadsheets/${this.id}`,
{},
query,
);
)) as SpreadSheetResponse;
const foundItem = response.sheets.find(
(item: { properties: { sheetId: number } }) => item.properties.sheetId === +sheetId,
);
const foundItem = response.sheets.find((item) => {
if (mode === 'name') return item.properties.title === value;
return item.properties.sheetId === getSheetId(value);
});
if (!foundItem?.properties?.title) {
throw new NodeOperationError(node, `Sheet with ID ${sheetId} not found`, {
level: 'warning',
});
throw new NodeOperationError(
node,
`Sheet with ${mode === 'name' ? 'name' : 'ID'} ${value} not found`,
{ level: 'warning' },
);
}
return foundItem.properties.title;
return foundItem.properties;
}
/**