2.2.4.6.13 DataObject::SetFormula

Description

Set the LabTalk formula and optional Before Formula Script for Column and MatrixObject.

Syntax

BOOL SetFormula( LPCSTR lpcszFormula, int nAutoUpdate = AU_NONE, int nBegin = -1, int nEnd = -1, BOOL bUndo = FALSE, UINT nRefColUID = 0 )

Parameters

lpcszFormula
[input] The string containing the formula expression.
nAutoUpdate
[input] AutoUpdate or not.
nBegin
[input] The begining Row number.
nEnd
[input] The endding Row number.
bUndo
[input] If this parameter is TRUE (by default) method can be undone.
nRefColUID
[input] internal use

Return

Returns TRUE on success and FALSE on failure.

Examples

EX1

// Create a worksheet with column formulas in place and to run it
void    DataObject_SetFormula_Ex1()
{
    Worksheet wks;
    wks.Create("origin", CREATE_VISIBLE);
    wks.AddCol();
 
    Column col;
    col.Attach(wks, 0);
    col.SetFormula("5*(i-1)");
    col.ExecuteFormula();
 
    // next two column we will set Recalculate = Auto
    col.Attach(wks, 1);
    col.SetFormula("sin(4*col(A)*pi/180)", AU_AUTO);
    col.ExecuteFormula();// this step is needed to initiate Recaulation
 
    // using declared variables in Before Formula Script
    col.Attach(wks, 2);
    string strExpression = "cos(Amp*x*pi/180)";
    string strBeforeScript = "double Amp=4.5;" + "\r\n" + "range x=col(A);";
    string strFormula = strExpression + STR_COL_FORMULAR_SEPARATOR + strBeforeScript;
    col.SetFormula(strFormula, AU_AUTO);
    col.ExecuteFormula();
}

EX2

void DataObject_SetFormula_Ex2()
{
	MatrixPage mp;
	mp.Create("origin");
    MatrixLayer ml = Project.ActiveLayer();
    if ( !ml )
    {
        printf("Can not access active matrixsheet");
        return;
    }
    MatrixObject mo = ml.MatrixObjects(0); //get first matrixobject
    mo.SetFormula("sin(i) + cos(j)");
    mo.ExecuteFormula();
}

Remark

You can also use this method to set the Before Formula Script. This additional script is appended to the formula string by a separator string:

#define STR_COL_FORMULAR_SEPARATOR ";\r\n#"

You can use the STR_COL_FORMULAR_SEPARATOR in your code if you don't want to hard code this separator. This marco has been defined in oc_sys.h file in \OriginC\system folder, so can use it directly.

For the formula, see Set Column Value dialog for more sample.

See Also

DataObject::GetFormula, DataObject::ExecuteFormula

Header to Include

origin.h