1.6.2.1 Worksheet Column Operation

To perform operation on worksheet column, you can use Column class or Worksheet class.

Add or Insert Column

To add a column to the end of the worksheet, the AddCol method in Worksheet class is available, and also the InsertCol for inserting a column before a specified position.

// Add column with default name
int nColIndex = wks.AddCol();

// Add column with namestring strName;
int nColIndex = wks.AddCol("AA", strName); // Returns the index of column 
// If the column named AA already exist, name enumeration automatically 
out_str(strName); 
Column col(wks, nColIndex); // Construct column object by column index


// Insert a new column as the first column
int nPos = 0; // The position to insert
string strNewCreated; // the real name of the new column 
// The name will be auto enumerated if name MyCol already existed
if( wks.InsertCol(nPos, "MyCol", strNewCreated) )
{
        printf("Insert column successfully, name is %s\n", strNewCreated);
}

Delete Column

The Worksheet::DeleteCol method is capable of removing a column from worksheet.

// Delete the column by index
wks.DeleteCol(0);

Rename and Label Column

To rename (short name) a column, Origin provides the SetName method.

Column col = wks.Columns(0);  // Get the 1st column in worksheet
BOOL bRet = col.SetName("MyNewName");  // Rename the column

Worksheet column labels support Long Name, Units, Comments, Parameters and User-Defined labels. We can use Origin C code to show/hide labels or to add text to the specified column label.

Worksheet wks;
wks.Create();    
 
Grid gg;
gg.Attach(wks);
 
// if Parameters lable not show, show it.
bool bShow = gg.IsLabelsShown(RCLT_PARAM);
if( !bShow )
        gg.ShowLabels(RCLT_PARAM);
 
wks.Columns(0).SetLongName("X Data");
wks.Columns(1).SetLongName("Y Data");
 
wks.Columns(0).SetComments("This is a test");
 
wks.Columns(0).SetUnits("AA");
wks.Columns(1).SetUnits("BB");
 
// put text to Parameters label for two columns.
wks.Columns(0).SetExtendedLabel("Param A", RCLT_PARAM);
wks.Columns(1).SetExtendedLabel("Param B", RCLT_PARAM);

RCLT_PARAM is the type of Parameters column label, other types see OriginC\system\oc_const.h file ROWCOLLABELTYPE enum.

Hide/Unhide Column

To hide/unhide column(s), you can use the Workhseet::ShowCol method.

wks.ShowCol(1, 1, false); // to hide column 1.

Move and Swap Columns

Move Column To move columns or swap columns, the super class of Worksheet class, Datasheet class, provides the method MoveColumns and [[OriginC:Datasheet-SwapColumns|SwapColumns] respectively for such purposes.

// Move three columns - starting with column 5 - to the first column
// Example requires first worksheet in project with at least 7 columns
Worksheet wks = Project.ActiveLayer();
if(wks)
    wks.MoveColumns(4, 3, MOVE_COL_TO_FIRST);

// Reverse the column order in the active worksheet
for(int ii = 1; ii <= wks.GetNumCols() / 2 ; ii++)
    wks.SwapColumns(ii - 1, wks.GetNumCols() - ii);

Add Sparkline to Column

To add sparkline to column(s), Origin C provides the wks_set_show_labels with the RCLT_SPARKLINE label type.

// Configure active sheet to show Sampling Inverval and SparkLine in order
// append to the curernt Labels
Worksheet wks = Project.ActiveLayer();
vector<int> vn = {RCLT_SAMPLE_RATE, RCLT_SPARKLINE}; 
wks_add_show_labels(wks, vn, false);

Data Type, Format, SubFormat

Get & Set Data Type

Worksheet wks = Project.ActiveLayer();	
Column col(wks, 0);

// Get column type, can be:
// 0: Y
// 1: None
// 2: Y Error
// 3: X
// 4: L
// 5: Z
// 6: X Error
int nType = col.GetType();
out_int("Type: ", nType);
// Set column type. See more define OKDATAOBJ_DESIGNATION_* in oc_const.h
col.SetType(OKDATAOBJ_DESIGNATION_Z);

Get & Set Data Format

// Get and set data format
// The default format of column is OKCOLTYPE_TEXT_NUMERIC.
// Set the format of column to Date
if( OKCOLTYPE_DATE != col.GetFormat() )
{
	col.SetFormat(OKCOLTYPE_DATE);
}

Get & Set Data Subformat

// Get and set data subformat
// The options of the sub format will be different according to the above format,
// numeric, date, time and so on. 
if( LDF_YYMMDD != col.GetSubFormat() )
{
	col.SetSubFormat(LDF_YYMMDD);
}