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
 Need Help, merging a lot of workbooks
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

bbb208

USA
6 Posts

Posted - 07/15/2013 :  12:07:49 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
I am using Origin 9. These days, I am dealing with a lot of data. There are tens of workbooks with the sample columns (not worksheets in a workbook) in a folder. I have to copy, pase, copy, paste...to merge them together
I just found that Origin C could do that. Anybody could give me a Origin c script here?
Thanks a lot.

rlewis

Canada
253 Posts

Posted - 07/15/2013 :  12:13:20 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
How do you want the merge to work ..
Please Specify ...
Go to Top of Page

bbb208

USA
6 Posts

Posted - 07/15/2013 :  12:20:02 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
To be clear, I just need to copy the data of workbook2, and paste it at the end of workbook1. then workbook3 at the end of the new workbook1, without labels (They have the same labels).

So, I just need to connect all the workbooks one by one.


quote:
Originally posted by rlewis

How do you want the merge to work ..
Please Specify ...

Go to Top of Page

rlewis

Canada
253 Posts

Posted - 07/15/2013 :  5:55:21 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The following function is designed to loop through all workbooks in the active folder and copy all columns of the first worksheet of each workbook into columns of a single worksheet of a newly created workbook.

On Success ... returns the name of the new workbook created.
On Failure ... returns an appropriate error message.

Hopefully this will work for you ...


void MergeWorkbookData()
{
	WorksheetPage wP;
	if(wP.Create("origin", CREATE_HIDDEN)==false)
	{
		out_str("Workbook Create Fail ...");
		goto ErrorExit;
	}
	string strWbkName=wP.GetName();
	Worksheet Wks(wP.Layers(0));
	if(Wks.IsValid()==false)
	{
		out_str("Worksheet Validation Fail ...");
		goto ErrorExit;
	}
	
	// Delete all columns in the Worksheet Wks ..
	for(int i=(Wks.GetNumCols()-1);i>=0;i--)
	{
		Wks.DeleteCol(i);
	}
	Folder Fld=Project.ActiveFolder();
	foreach(PageBase pB in Fld.Pages)
	{
		if(pB.GetType()!=EXIST_WKS)
		{
			continue;
		}
		if(strWbkName.CompareNoCase(pB.GetName())!=0)
		{
			Page pG(pB);
			if(pG.IsValid()==false)
			{
				out_str("Workbook Validation Fail ...");
				goto ErrorExit;
			}
			Worksheet Wk1=pG.Layers(0);
			if(Wk1.IsValid()==false)
			{
				out_str("Worksheet Validation Fail ...");
			}
			for(int j=0;j<Wk1.GetNumCols();j++)
			{
				int ColIndex=Wks.AddCol();
				if(ColIndex==-1)
				{
					out_str("Worksheet AddCol function fail ...");
					goto ErrorExit;
				}
				if(Wk1.CopyTo(Wks, j, j, 0, Wk1.GetNumRows(), ColIndex, -1)!=0)
				{
					out_str("Worksheet CopyTo function fail ...");
					goto ErrorExit;
				}
			}
		}
	}
	out_str("Job Done ... Data Collated into Workbook "+strWbkName);
	return;
ErrorExit:
	if(wP.IsValid()==true)
	{
		wP.Destroy();
	}
}
Go to Top of Page

bbb208

USA
6 Posts

Posted - 07/17/2013 :  12:41:08 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,rlewis,

Could you tell me how to use this step by step?

I tried a lot of times, but I still failed.

quote:
Originally posted by rlewis

The following function is designed to loop through all workbooks in the active folder and copy all columns of the first worksheet of each workbook into columns of a single worksheet of a newly created workbook.

On Success ... returns the name of the new workbook created.
On Failure ... returns an appropriate error message.

Hopefully this will work for you ...


void MergeWorkbookData()
{
	WorksheetPage wP;
	if(wP.Create("origin", CREATE_HIDDEN)==false)
	{
		out_str("Workbook Create Fail ...");
		goto ErrorExit;
	}
	string strWbkName=wP.GetName();
	Worksheet Wks(wP.Layers(0));
	if(Wks.IsValid()==false)
	{
		out_str("Worksheet Validation Fail ...");
		goto ErrorExit;
	}
	
	// Delete all columns in the Worksheet Wks ..
	for(int i=(Wks.GetNumCols()-1);i>=0;i--)
	{
		Wks.DeleteCol(i);
	}
	Folder Fld=Project.ActiveFolder();
	foreach(PageBase pB in Fld.Pages)
	{
		if(pB.GetType()!=EXIST_WKS)
		{
			continue;
		}
		if(strWbkName.CompareNoCase(pB.GetName())!=0)
		{
			Page pG(pB);
			if(pG.IsValid()==false)
			{
				out_str("Workbook Validation Fail ...");
				goto ErrorExit;
			}
			Worksheet Wk1=pG.Layers(0);
			if(Wk1.IsValid()==false)
			{
				out_str("Worksheet Validation Fail ...");
			}
			for(int j=0;j<Wk1.GetNumCols();j++)
			{
				int ColIndex=Wks.AddCol();
				if(ColIndex==-1)
				{
					out_str("Worksheet AddCol function fail ...");
					goto ErrorExit;
				}
				if(Wk1.CopyTo(Wks, j, j, 0, Wk1.GetNumRows(), ColIndex, -1)!=0)
				{
					out_str("Worksheet CopyTo function fail ...");
					goto ErrorExit;
				}
			}
		}
	}
	out_str("Job Done ... Data Collated into Workbook "+strWbkName);
	return;
ErrorExit:
	if(wP.IsValid()==true)
	{
		wP.Destroy();
	}
}


Go to Top of Page

origjuhu

3 Posts

Posted - 07/24/2013 :  12:06:13 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
create a new origin file in the codebuilder, then compile it and then you can just run the function from labtalk / the command window
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