Example
The following example shows the code required to assign a custom function along with the code for the function itself. This example creates a Max function that accepts a variable number of parameters.
C++
// Assign MyMax function during initialization
FuncMaxProcInst = MakeProcInstance((FARPROC)SpreadFuncMaxProc, hInst);
SSAddCustomFunction(hWnd, "MYMAX", SS_CALC_VARPARAMS, FuncMaxProcInst);
// Callback function to perform calculation
BOOL CALLBACK SpreadFuncMaxProc(LPSS_VALUE Result, LPSS_VALUE Values, short ParamCnt);
{
double Max;
double MaxTemp;
short i;
Result->Status = SS_VALUE_STATUS_EMPTY;
Result->Type = SS_VALUE_TYPE_LONG;
for (i = 0; i < ParamCnt; i++)
if (Values[i].Status == SS_VALUE_STATUS_OK)
{
if (Values[i].Type == SS_VALUE_TYPE_DOUBLE)
{
Result->Type = SS_VALUE_TYPE_DOUBLE;
MaxTemp = Values[i].Val.ValDouble;
}
else if (Values[i].Type==SS_VALUE_TYPE_LONG)
MaxTemp = (double)Values[i].Val.ValLong;
if (Result->Status == SS_VALUE_STATUS_EMPTY)
Max = MaxTemp;
else
Max = max(Max, MaxTemp);
Result->Status = SS_VALUE_STATUS_OK;
}
// Check to see if the status is OK. Then determine if the
// result is a long or double and place it into the
// appropriate structure member.
if (Result->Status == SS_VALUE_STATUS_OK)
if (Result->Type == SS_VALUE_TYPE_DOUBLE)
Result->Val.ValDouble = Max;
else
Result->Val.ValLong = (long)Max;
return (TRUE);
}