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 
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

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;
Grid gg;
// if Parameters lable not show, show it.
bool bShow = gg.IsLabelsShown(RCLT_PARAM);
if( !bShow )
wks.Columns(0).SetLongName("X Data");
wks.Columns(1).SetLongName("Y Data");
wks.Columns(0).SetComments("This is a test");
// 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();
    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();
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

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() )

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() )