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
 All Forums
 Origin Forum for Programming
 Forum for Origin C
 replicating rows in worksheet

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
peter.cook Posted - 10/06/2008 : 02:21:05 AM
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


1   L A T E S T    R E P L I E S    (Newest First)
Deanna Posted - 10/07/2008 : 02:18:34 AM
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

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000