Accessing Worksheet Format with Theme Tree

 

Version Info

Minimum Origin Version Required: Origin 8 SR0

View Format Tree

Following code show how to get/set worksheet format using Origin C code. To customize worksheet format, you may use OriginObject::GetFormat to get the format tree of one Origin object(e.x. WorksheetPage, Worksheet, Column), save it to XML file or output it the learn more about it.

View Worksheet Format Tree

void view_wks_format_tree_ex()
{
        Worksheet wks = Project.ActiveLayer();
        if(!wks)
                return;
        
        Tree trFormat;
        trFormat = wks.GetFormat(FPB_ALL, FOB_ALL, TRUE, TRUE);
        //out_tree(trFormat); // output format tree
        
        string strSaveTo = GetAppPath(false) + "WksFormatTree.xml";
        if(trFormat.Save(strSaveTo))
                out_str("Format Tree xml file saved to "+strSaveTo);
}

View Column Format Tree

void view_format_tree_ex()
{
        Worksheet wks = Project.ActiveLayer();
        if(!wks)
                return;
        
        Column col(wks, 1);
        if(!col)
                return;
        
        Tree trFormat;
        trFormat = col.GetFormat(FPB_ALL, FOB_ALL, TRUE, TRUE);
        //out_tree(trFormat); // output format tree
        
        string strSaveTo = GetAppPath(false) + "ColFormatTree.xml";
        if(trFormat.Save(strSaveTo))
                out_str("Format Tree xml file saved to "+strSaveTo);
}

Set Merge for Units/Comments/Long Name

This example used to control the dynamic merge of Column Labels in Worksheet when labels contain same contents. Keep worksheet is active and run SetLabelMerge "LongName".

// strLabelType can be LongName, Unit, Comment, SampleRate, Param, Data, UDL(for user defined label)
void set_label_merge_ex(string strLabelType = "Param")
{
        Worksheet wks = Project.ActiveLayer();
        if( !wks )
                return;
        
        Tree trFormat;
        trFormat = wks.GetFormat(FPB_ALL, FOB_ALL, TRUE, TRUE);
        
        string strName = "og" + strLabelType;  
        TreeNode trNameStyles = trFormat.Root.Grid.NameStyles;
        
        bool bRet = false;
        // to find the NameStyle node for the specified column label
        foreach(TreeNode trNameStyle in trNameStyles.Children)
        {
                if( 0 == trNameStyle.Name.strVal.Compare(strName) )
                {
                        bRet = true; // found
                        break;
                }
        }
        if(!bRet) // not found, please check input strLabelType if correct
                return;
        
        trNameStyle.Style.Merge.nVal = GMC_HORZ; // GMC_NONE(0), GMC_HORZ(1), GMC_VERT(2), GMC_BOTH(3)
        
        if( 0 != wks.UpdateThemeIDs(trFormat) ) //0 for no error
                return; 
        
        bool bUndo = true;
        bRet = wks.ApplyFormat(trFormat, true, true, bUndo);
        if(!bRet)
                out_str("Fial to apply format");       
}

Set Row Height

  • n1, n2 can be negative, 0 or positive. 0 means the first data row, 1 means the second data row, -1 means last column lable row.
For example, one worksheet has 3 Column Labels: Long Name, Units, Comments and want to set the height of Comments Label to 3, then run the following function like SetRowHeight(-1, -1, 3).
  • nNewHeight is the new height you want to set.
#define DEFAULT_WKS_ROW_HEIGHT 100
void set_row_height_ex(int n1, int n2, int nNewHeight)
{
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
        return;    
    
    Tree tr;
    tr = wks.GetFormat(FPB_DIMENSION, FOB_DIMENSION, TRUE, TRUE);    
    
    // the current size
        vector<int> vn;
        TreeNode trSize = tr.Root.Grid.Dimensions.Vertical.Size;
        if( trSize )
                vn = trSize.nVals;
        else
                return;
        
        // get the state of lables
        vector<int> vnShowTypes;
        Grid grid;   
        if(grid.Attach(wks))
                grid.GetShowLabels(vnShowTypes, true); // true for row
        
        int nLabelOffset = vnShowTypes.GetSize() + 1;//1: column header        
        int nRow = nLabelOffset + n2, nCount = vn.GetSize();
        if(nRow >= nCount)
        {
                vector<int> vnAppend;
                vnAppend.SetSize(nRow + 1 - nCount);
                vnAppend = DEFAULT_WKS_ROW_HEIGHT; // default is 100
                vn.Append(vnAppend);
        }
        
        nNewHeight = nNewHeight * DEFAULT_WKS_ROW_HEIGHT;
        for(int ii = n1; ii <= n2; ii++)
                vn[nLabelOffset + ii] = nNewHeight;
        
        Tree trTemp;
        trTemp.Root.Grid.Dimensions.Vertical.Size.nVals = vn;                
                
    if( 0 != wks.UpdateThemeIDs(trTemp.Root) ) //0 for no error
        return; 
    
    bool bUndo = true;
    bool bRet = wks.ApplyFormat(trTemp, true, true, bUndo);
    if(!bRet)
        out_str("Fial to apply format");    
}

Set Row header Width

This example asign the value nWidth (width in characters approximate) to the Row header Width in active worksheet.

void rhWidth(int nWidth)   
{
        Worksheet wks = Project.ActiveLayer();
        Tree tr;
        vector<int> vec;
        vec.Add(nWidth);
        tr.Root.Grid.Dimensions.Horizontal.Size.nVals=vec;   
        wks.UpdateThemeIDs(tr.Root);
        wks.ApplyFormat(tr, true, true);
}