3.2.1 Get and Set Number of Columns and Rows


Version Info

Minimum Origin Version Required: Origin8 SR0

Get Number of Columns and Rows

This example shows how to get the number of columns and rows in a worksheet

void wks_get_cols_and_rows_num_ex()
{
	WorksheetPage wp;
	wp.Create("Origin");
	
	//Get the first worksheet of the workbook
	Worksheet wks(wp.Layers(0));
	
	//Get the number of rows and columns
	int nRows = wks.GetNumRows();
	int nCols = wks.GetNumCols();
	
	printf("The number of the rows in %s is: %d\n",  wks.GetName(),nRows);
	printf("The number of the columns in %s is: %d\n", wks.GetName(), nCols);
}

Set Number of Columns and Rows

This example shows how to set the number of columns and rows in a worksheet

void wks_set_cols_and_rows_num_ex()
{
	WorksheetPage wp;
	wp.Create("Origin");
	
	//Get the first worksheet of the workbook
	Worksheet wks(wp.Layers(0));
	
	//Set the number of rows and columns
	int nRows = 50;
	int nCols = 5;
	if(!wks.SetSize(nRows, nCols))
	{
		printf("Failed to set size of %s!\n", wks.GetName());
	}
}

Add Column

This example shows how to add column to the specified worksheet

void wks_add_col_ex()
{
	WorksheetPage wp;
	wp.Create("Origin");
	
	//Get the first worksheet of the workbook
	Worksheet wks(wp.Layers(0));
	
	string strColName;
	//Add a new column named "NewCol", if there already has a column named NewCol, will auto enumerate name with number and put name to strColName.
	int iCol = wks.AddCol("NewCol", strColName);
	
	if( iCol>=0 )
	{
		printf("New column added at index %d, name %s\n", iCol, strColName);
	}
}

Delete Column

This example shows how to delete a column in the worksheet

void wks_delete_col_ex()
{
	WorksheetPage wp;
	wp.Create("Origin");
	
	//Get the first worksheet of the workbook
	Worksheet wks(wp.Layers(0));
	
	//Delete the first column of the worksheet
	if( wks.DeleteCol(0) )
		printf("Successful delete column\n");
	else
		printf("Failed to delete column\n");
	
}

Insert Column

This example shows how to insert a column in the worksheet

void wks_insert_col_ex()
{
    WorksheetPage wp;
    wp.Create("Origin");
    
    Worksheet wks(wp.GetName());    
    
    int nPos = 0; //Set the insert position as the first column    
    string strColName = "NewInsert"; //Set the insert column name to "NewInsert"
    string strColCreated;  
    
    if(!wks.InsertCol(nPos, strColName, strColCreated))
        printf("Failed to insert column\n");
    else
        printf("New column insert named %s\n", strColCreated);    
}

Set Large Size

In order to keep Origin C function more efficient on setting the large number of columns or rows in worksheet, for example 5000 columns, need to do like the steps below.

  • Use Worksheet::SetSize, not use Worksheet::AddCol to set size.
  • Do SetSize on an empty worksheet, means no column, since need to check the short name of the existed columns to avoid duplicate name when add new column. This check may cost many times. Can use while( wks.DeleteCol(0) ); to remove all columns.
  • Keep Code Builder close when running function to set size.
void wks_set_large_size_ex(int rows = 100, int cols = 5000)
{
    // prepare worksheet size
    Worksheet wks;
    wks.Create("Origin");
    
    while( wks.DeleteCol(0) );  // del all columns
    
    wks.SetSize(rows, cols);  
}