The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 replicating rows in worksheet
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

peter.cook

UK
356 Posts

Posted - 10/06/2008 :  02:21:05 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi,

I want to eg duplicate all rows in a worksheet. Performance is an issue as there is a lot of (text) data. I can of course use GetCell and SetCell for all cells but I feel sure there is a slicker way. I was thinking along the lines of duplicating the worksheet, appending it to the original and then sorting by an index column which I would create. Any good ideas wuld be appreicated, thanks!

Example

a b c d e
f g h i j
k l m n o

to

a b c d e
a b c d e
f g h i j
f g h i j
k l m n o
k l m n o

Cheers,

Pete


Deanna

China
Posts

Posted - 10/07/2008 :  02:18:34 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Peter,

In Origin, it is usually easier to manipulate columns than to manipulate rows. You may consider transpose your worksheet first, duplicate the columns and then transpose the worksheet again.

I have an example below. Please run "test7347" when the source worksheet is active.


//Copy the texts in one column to another column
bool copy_text_col(Worksheet wksSrc, uint iColSrc, Worksheet wksDst, uint iColDst)
{
	bool bRet = false;	
	
	Dataset dsSrc(wksSrc, iColSrc);
	Dataset dsDst(wksDst, iColDst);
	
	if ( (!dsSrc.IsValid()) || (!dsDst.IsValid() )) return bRet;
	
	StringArray sa;
	bRet = dsSrc.GetStringArray(sa);
	
	if ( !bRet ) return bRet;
	
	bRet = dsDst.PutStringArray(sa);
	
	return bRet;
}

//Duplicate all columns in the worksheet
bool duplicate_columns(Worksheet wks)
{
	bool bRet = false;	
	
	int nCols = wks.GetNumCols() * 2;
	
	for (int ii = 0; ii < nCols; ii += 2)
	{
		string strName; 
		bRet = wks.InsertCol(ii + 1, "copy", strName);
		if ( !bRet ) break;
		
		bRet = copy_text_col(wks, ii, wks, ii + 1);
		if ( !bRet ) break;		
	}	
	
	return bRet;
}

//Main function
bool test7347()
{
	BOOL  bRet = false;
	Worksheet wks = Project.ActiveLayer();
	if ( !wks.IsValid() ) return bRet;
	
	int nCols = wks.GetNumCols();
	int nRows = wks.GetNumRows();
	
	//Create a copy of the worksheet and perform the duplication of rows there
    Worksheet    wksCopy;
    bRet = wksCopy.CreateCopy(wks, CREATE_VISIBLE_SAME);
	if ( !bRet ) return bRet;		
    	
	bRet = (wksCopy.Transpose() == 0);
	if ( !bRet ) return bRet;		

	//Remove some empty columns created by trasposing
	int iR1, iR2;		
	bRet = wks.GetRange(iR1, iR2, 0, 0, GDR_NO_ADJUST, GDR_NO_ADJUST);
	if ( !bRet ) return bRet;		
	
	bRet = wksCopy.SetSize(nCols, iR2 - iR1 + 1);
	if ( !bRet ) return bRet;		
	
	//Duplicate all columns in this worksheet		
	bRet = duplicate_columns(wksCopy);
	if ( !bRet ) return bRet;		
	
	bRet = (wksCopy.Transpose() == 0);
	if ( !bRet ) return bRet;		
	
	//Copy the result back to the original worksheet
	for (int ii = 0; ii < nCols; ii ++)
	{
		bRet = copy_text_col(wksCopy, ii, wks, ii);
		if ( !bRet ) break;
	}
	if ( !bRet ) return bRet;		
	
	bRet = wksCopy.Destroy();
	
	return bRet;	
}


Deanna
OriginLab Technical Services
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000