3.2.6.2 Copy Paste Cell Formatting


The following code shows how to get and set tables/blocks of cells of same dimension using Origin C code. You can use OriginObject::GetFormat to get the format tree of one Origin object(e.x. DataRange), save it to XML file or output it the learn more about it. Then you can use OriginObject::SetFormat to set the format to the Origin objects.

Version Info

Minimum Origin Version Required: Origin 8 SR0

Copy Format

// Define the theme file path and name.
#define	  STR_THEME_FILE_PATH        GetOriginPath(ORIGIN_PATH_USER) + "wksTheme.xml"

void copy_cell_format()
{
	Worksheet wks = Project.ActiveLayer();
	if( !wks )
	{
		printf("Please keep worksheet active before running.\n");
		return;
	}
	
	int r1, c1, r2, c2;
	wks.GetSelectedRange( r1, c1, r2, c2 ); // to get the selected range
	
	DataRange dr;	
	dr.Add( "Range1", wks, r1, c1, r2, c2 ); // construct a data range object
	
	Tree tr;
	tr = dr.GetFormat( FPB_ALL, FOB_RANGE_STYLE, true, true );	
	tr.Save(STR_THEME_FILE_PATH);
}

Apply Format

void paste_cell_format()
{
	Worksheet wks = Project.ActiveLayer();
	if( !wks )
	{
		printf("Please keep worksheet active before running.\n");
		return;
	}
	
	vector<int> vR1, vC1, vR2, vC2;
	wks.GetSelectedRange( vR1, vC1, vR2, vC2 );
    Tree tr;	
	if( tr.Load(STR_THEME_FILE_PATH) )
	{			
		for(int iRange = 0; iRange < vR1.GetSize(); iRange++)
		{			
			DataRange dr;
			dr.Add( "Range1", wks, vR1[iRange], vC1[iRange], vR2[iRange], vC2[iRange] ); 
			
			// apply the format from drSource to drDest
			bool bRet = dr.ApplyFormat( tr, true, true );			
		}
	}
}