Gnumeric - Bespoke function writing

Gnumeric - Bespoke function writing

Nigel Wilbraham September 09, 2020
Gnumeric - Bespoke function writing

Overview:

For GNU/Linux users there are many spreadsheet applications to choose from. Gnumeric offers the easiest for the addition of bespoke user defined functions into the compiled application.

Here I give a flavour of what can be done to extend this package.

Gnumeric is a Gtk and C coded application which has a long history. As of today it is maintained but with limited development. Whilst this may at first appear off putting it is a fully developed application and there is very little it cannot do compared to other commercial applications. It is however, not maintained for windows based machines due to the difficult compilation.

Here I will show how a very simple function can be written and included in the compiled and installed application on any GNU/Linux machine. The code will be written in C with some simple modifications to xml files. The resulting function will be seamlessly integrated into Gnumeric with its own function grouping. this is shown in the picture below

vessels

example of the function

The function definition file is contained within the plugins directory and is compiled along with the rest of the code base when the standard ./config make make install commands are used:

 1/* this section is used to provide the user with some information about the function */
 2
 3static GnmFuncHelp const help_VcombRef[] = {
 4        { GNM_FUNC_HELP_NAME, F_("VcombRef:Returns the combustor reference velocity @{Airflow}, @{Fuelflow}, @{GasDensity}, @{Diameter}")},
 5        { GNM_FUNC_HELP_ARG, F_("Airflow :Front end airflow into the combustor (kg/s)")},
 6        { GNM_FUNC_HELP_ARG, F_("Fuelflow :Fuelflow into the combustor (kg/s)")},
 7        { GNM_FUNC_HELP_ARG, F_("GasDensity :Density of gas in combustor (kg/m^3)")},
 8        { GNM_FUNC_HELP_ARG, F_("Diameter :Diameter of the combustor (m)")},
 9        { GNM_FUNC_HELP_DESCRIPTION, F_("Returns the combustor reference velocity based on cold flow")},
10        { GNM_FUNC_HELP_NOTE, F_("UNITS = m/s")},
11        { GNM_FUNC_HELP_NOTE, F_("This function is part of the thermochemical library")},
12        { GNM_FUNC_HELP_EXCEL, F_("This function is @{not} Excel compatible.") },
13        { GNM_FUNC_HELP_DESCRIPTION, F_("Licensed software. Copyright BURNTASTIC Ltd 2017")},
14        { GNM_FUNC_HELP_SEEALSO, "Nothing yet"},
15        { GNM_FUNC_HELP_END}
16};
17
18static GnmValue *
19gnumeric_VcombRef(GnmFuncEvalInfo *ei, GnmValue const * const *argv)
20{
21        gnm_float Airflow = value_get_as_float (argv[0]);
22        gnm_float Fuelflow = value_get_as_float (argv[1]);
23        gnm_float GasDensity = value_get_as_float (argv[2]);
24        gnm_float Diameter = value_get_as_float (argv[3]);
25        
26        const double pi = 4.0 * atan(1.0);  // calculate pi
27        
28        return value_new_float ((Airflow + Fuelflow) / (GasDensity * 0.25 * pi* powf(Diameter, 2.0)));
29}

The GNM_FUNC_HELP tags provide variables that are read during compilation and for the prompts in the function guru GUI.

The information displayed in the function guru is controlled by an associated xml file.

A detailed guide for writing a function will be put in a follow up post.