Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions x-pack/plugins/canvas/canvas_plugin_src/functions/common/getCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ import { getFunctionHelp, getFunctionErrors } from '../../../i18n';
interface Arguments {
column: string;
row: number;
onError: 'null' | 'zero' | 'false' | 'throw' | 'emptyString';
}

const fallbackValue = {
null: null,
zero: 0,
false: false,
emptyString: '',
} as const;

export function getCell(): ExpressionFunctionDefinition<'getCell', Datatable, Arguments, any> {
const { help, args: argHelp } = getFunctionHelp().getCell;
const errors = getFunctionErrors().getCell;
Expand All @@ -34,21 +42,35 @@ export function getCell(): ExpressionFunctionDefinition<'getCell', Datatable, Ar
help: argHelp.row,
default: 0,
},
onError: {
types: ['string'],
options: ['throw', 'false', 'zero', 'null', 'emptyString'],
help: argHelp.onError,
default: 'throw',
},
},
fn: (input, args) => {
const row = input.rows[args.row];
if (!row) {
throw errors.rowNotFound(args.row);
}
try {
const row = input.rows[args.row];
if (!row) {
throw errors.rowNotFound(args.row);
}

const { column = input.columns[0].name } = args;
const value = row[column];
const { column = input.columns[0].name } = args;
const value = row[column];

if (typeof value === 'undefined') {
throw errors.columnNotFound(column);
}
if (typeof value === 'undefined') {
throw errors.columnNotFound(column);
}

return value;
} catch (e) {
if (args.onError !== 'throw' && args.onError in fallbackValue) {
return fallbackValue[args.onError];
}

return value;
throw e;
}
},
};
}
4 changes: 4 additions & 0 deletions x-pack/plugins/canvas/i18n/functions/dict/get_cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export const help: FunctionHelp<FunctionFactory<typeof getCell>> = {
row: i18n.translate('xpack.canvas.functions.getCell.args.rowHelpText', {
defaultMessage: 'The row number, starting at 0.',
}),
onError: i18n.translate('xpack.canvas.functions.getCell.args.onErrorHelpText', {
defaultMessage:
"In case of an error retrieving the cell, the return value is specified by onError. When `'throw'`, it will throw an exception, terminating expression execution (default).",
}),
},
};

Expand Down