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
 Copy Columns to a new workbook
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Kaemme1909

8 Posts

Posted - 06/11/2013 :  04:51:51 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 9
Operating System: Win7

Hey together,

I am new in Origin C so I hope this easy question don't bother you.
I have a Workbook which contains a x column and 17 y columns. What I want to do is to create a new workbook, for each y column, which contains the x column and one y column. And if possible name it after the comment of the y column. I have no idea how to do this, because I even can't find the function to copy a column.

Thanks for your help
Florian

Kaemme1909

8 Posts

Posted - 06/11/2013 :  05:23:06 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
First try, but is not working:
foreach(Column col5 in wks.Columns){
if(OKDATAOBJ_DESIGNATION_Y == col5.GetType())
{
WorksheetPage wksPg;
wksPg.Create("Book2");
Dataset ds4(col5);
Dataset ds5(wksPg,1);
Data_copy(&ds5,&ds4);
}
}
Go to Top of Page

rlewis

Canada
253 Posts

Posted - 06/11/2013 :  4:51:33 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Try this function ....

void DisassembleToNewWks()
{
	Worksheet Wks=Project.ActiveLayer();
	if(Wks.IsValid()==false)
	{
		out_str("Error	-	Active layer is NOT a valid worksheet");
		return;
	}
	Column  xCol;
	int NumCols=Wks.GetNumCols();
	for(int i=0;i<NumCols;i++)
	{
		Column Col(Wks,i);
		if(Col.IsValid()==false)
		{
			out_str("Worksheet column validation error");
			return;
		}
		if(Col.GetType()==OKDATAOBJ_DESIGNATION_X)
		{
			xCol=Col;
			bool IsNotXcol=true;
			do
			{
				i++;
				if(i>=NumCols)
				{
					return;
				}
				Column yCol(Wks,i);
				if(yCol.IsValid()==false)
				{
					out_str("Worksheet column validation error");
					return;					
				}
				int ColType=yCol.GetType();
				if(ColType==OKDATAOBJ_DESIGNATION_Y)
				{
					WorksheetPage wP;
					if(wP.Create("Origin",CREATE_HIDDEN)==false)
					{
						out_str("Origin Workbook creation error");
						return;
					}
					Worksheet Wk1(wP.Layers(0));
					if(Wk1.IsValid()==false)
					{
						out_str("Error	-	New Worksheet Validation Error");
						return;
					}
					Dataset dsXsource(xCol);
					Dataset dsYsource(yCol);
					if(dsXsource.IsValid()==false || dsYsource.IsValid()==false)
					{
						out_str("Dataset validation Error");
						return;
					}
					Dataset dsXTarget;
					Dataset dsYTarget;
					if(dsXTarget.Attach(Wk1,0)==false || dsYTarget.Attach(Wk1,1)==false)
					{
						out_str("Worksheet Column adressing error");
						return;
					}
					dsXTarget=dsXsource;
					dsYTarget=dsYsource;
					string strComment=yCol.GetComments();
					strComment.TrimLeft();
					strComment.TrimRight();
					if(strComment.GetLength()>0)
					{
						wP.SetLongName(strComment);
					}
				}
				else
				{
					if(ColType==OKDATAOBJ_DESIGNATION_X)
					{
						IsNotXcol=false;
						i--;
					}
				}
			}
			while (IsNotXcol==true);
		}
	}
}
Go to Top of Page

rlewis

Canada
253 Posts

Posted - 06/11/2013 :  7:19:39 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The function posted previously should work in most simple cases (i.e. with most numeric data types) but may run into problems with other data types. The version posted below should work regardless of the type of data stored in the worksheet columns ...

void DisassembleToNewWks()
{
	Worksheet Wks=Project.ActiveLayer();
	if(Wks.IsValid()==false)
	{
		out_str("Error	-	Active Layer is NOT a valid worksheet");
		return;
	}
	Column  xCol;
	int NumCols=Wks.GetNumCols();
	for(int i=0;i<NumCols;i++)
	{
		Column Col(Wks,i);
		if(Col.IsValid()==false)
		{
			out_str("Worksheet column validation error");
			return;
		}
		if(Col.GetType()==OKDATAOBJ_DESIGNATION_X)
		{
			xCol=Col;
			bool IsNotXcol=true;
			do
			{
				i++;
				if(i>=NumCols)
				{
					return;
				}
				Column yCol(Wks,i);
				if(yCol.IsValid()==false)
				{
					out_str("Worksheet column validation error");
					return;					
				}
				int ColType=yCol.GetType();
				if(ColType==OKDATAOBJ_DESIGNATION_Y)
				{
					WorksheetPage wP;
					if(wP.Create("Origin",CREATE_HIDDEN)==false)
					{
						out_str("Origin Workbook creation error");
						return;
					}
					Worksheet Wk1(wP.Layers(0));
					if(Wk1.IsValid()==false)
					{
						out_str("Error	-	New Worksheet Validation Error");
						return;
					}
					Column xTarget(Wk1,0);
					if(CopyColumnData(xCol, xTarget)==false)
					{
						out_str("x-column copy fail");
						return;
					}
					Column yTarget(Wk1,1);
					if(CopyColumnData(yCol, yTarget)==false)
					{
						out_str("y-column copy fail");
						return;
					}
					string strComment=yCol.GetComments();
					strComment.TrimLeft();
					strComment.TrimRight();
					if(strComment.GetLength()>0)
					{
						wP.SetLongName(strComment);
					}
				}
				else
				{
					if(ColType==OKDATAOBJ_DESIGNATION_X)
					{
						IsNotXcol=false;
						i--;
					}
				}
			}
			while (IsNotXcol==true);
		}
	}
}

bool CopyColumnData(Column SourceCol, Column &TargetCol)
{
	if(SourceCol.IsValid()==false || TargetCol.IsValid()==false)
	{
		out_str("Worksheet Column Validation Error");
		return (false);
	}
	int iType=SourceCol.GetInternalDataType();
	TargetCol.SetInternalDataType(iType);
	switch (iType)
	{
		case FSI_DOUBLE:
			Dataset<double> dsSource(SourceCol);
			Dataset<double> dsTarget(TargetCol);
			if(dsTarget.IsValid()==false || dsSource.IsValid()==false)
			{
				out_str("Dataset validation eror");
				return (false);
			}
			dsTarget=dsSource;
		break;
		case FSI_REAL:
			Dataset<float> dsSource(SourceCol);
			Dataset<float> dsTarget(TargetCol);
			if(dsTarget.IsValid()==false || dsSource.IsValid()==false)
			{
				out_str("Dataset validation eror");
				return (false);
			}
			dsTarget=dsSource;
		break;		
		case FSI_SHORT:
			Dataset<short> dsSource(SourceCol);
			Dataset<short> dsTarget(TargetCol);
			if(dsTarget.IsValid()==false || dsSource.IsValid()==false)
			{
				out_str("Dataset validation eror");
				return (false);
			}
			dsTarget=dsSource;
		break;
		case FSI_USHORT:
			Dataset<ushort> dsSource(SourceCol);
			Dataset<ushort> dsTarget(TargetCol);
			if(dsTarget.IsValid()==false || dsSource.IsValid()==false)
			{
				out_str("Dataset validation eror");
				return (false);
			}
			dsTarget=dsSource;
		break;
		case FSI_LONG:
			Dataset<int> dsSource(SourceCol);
			Dataset<int> dsTarget(TargetCol);
			if(dsTarget.IsValid()==false || dsSource.IsValid()==false)
			{
				out_str("Dataset validation eror");
				return (false);
			}
			dsTarget=dsSource;
		break;
		case FSI_ULONG:
			Dataset<uint> dsSource(SourceCol);
			Dataset<uint> dsTarget(TargetCol);
			if(dsTarget.IsValid()==false || dsSource.IsValid()==false)
			{
				out_str("Dataset validation eror");
				return (false);
			}
			dsTarget=dsSource;
		break;
		case FSI_BYTE:
			Dataset<byte> dsSource(SourceCol);
			Dataset<byte> dsTarget(TargetCol);
			if(dsTarget.IsValid()==false || dsSource.IsValid()==false)
			{
				out_str("Dataset validation eror");
				return (false);
			}
			dsTarget=dsSource;
		break;
		case FSI_CHAR:
			Dataset<char> dsSource(SourceCol);
			Dataset<char> dsTarget(TargetCol);
			if(dsTarget.IsValid()==false || dsSource.IsValid()==false)
			{
				out_str("Dataset validation eror");
				return (false);
			}
			dsTarget=dsSource;
		break;
		case FSI_COMPLEX:
			Dataset<complex> dsSource(SourceCol);
			Dataset<complex> dsTarget(TargetCol);
			if(dsTarget.IsValid()==false || dsSource.IsValid()==false)
			{
				out_str("Dataset validation eror");
				return (false);
			}
			dsTarget=dsSource;
		break;
		case FSI_MIXED:
			vector<string>vctString;
			if(SourceCol.GetStringArray(vctString,0,-1)==false)
			{
				out_str("Column Contents access error");
				return (false);
			}
			
			if(TargetCol.PutStringArray(vctString,0,true)==false)
			{
				out_str("Column Contents update error");
				return (false);						
			}			
		break;
		case FSI_TEXT:
			vector<string>vctString;
			if(SourceCol.GetStringArray(vctString,0,-1)==false)
			{
				out_str("Column Contents access error");
				return (false);
			}
			
			if(TargetCol.PutStringArray(vctString,0,false)==false)
			{
				out_str("Column Contents update error");
				return (false);						
			}
		break;
		default:
			out_str("Unknown data Type");
		return (false);
	break
	}
	return (true);
}


Go to Top of Page

Kaemme1909

8 Posts

Posted - 06/12/2013 :  05:34:23 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Works fine. Thanks for your help.
Go to Top of Page

magicalmarshmallow

10 Posts

Posted - 01/09/2014 :  03:51:51 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Origin 8 SR6
Windows 7

I have a similar request to this. I have a lot of worksheets (broken up in to different books and folders).

What I want to do is copy column 'C' of all the worksheets into a new workbook, labelling the column (long name) with the name of the worksheet that it came from.
Is this possible to do automatically as I haven't breeze how to write a script for it.

Cheers
Go to Top of Page

Penn

China
644 Posts

Posted - 01/16/2014 :  05:00:28 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi magicalmarshmallow,

You can try this example.

void GetWorksheetsAndColumns(Folder fld, vector<string>& vsWorksheets, vector<int>& viColIndices)
{
	Folder fldSub;  // Subfolder
	foreach(fldSub in fld.Subfolders)  // Loop all subfolders
	{
		// Call function recursively to loop all folders
		GetWorksheetsAndColumns(fldSub, vsWorksheets, viColIndices);
	}
	foreach(PageBase page in fld.Pages)  // Loop all pages
	{
		if(page.GetType() == EXIST_WKS)  // If the page is workbook
		{
			WorksheetPage wp(page);
			int numWorksheets = wp.Layers.Count();  // How many worksheets in workbook
			for(int ii = 0; ii < numWorksheets; ii++)  // Loop all worksheets in workbook
			{
				Worksheet wks = wp.Layers(ii);  // Get worksheet
				Column colC = wks.Columns("C");  // Get column C in worksheet
				if(colC.IsValid())  // If column is valid
				{
					string strName;
					strName.Format("[%s]%s", wp.GetName(), wks.GetName());  // Construct the full worksheet name
					vsWorksheets.Add(strName);  // Put the worksheet name to string vector
					viColIndices.Add(colC.GetIndex());  // Store the column index
				}
			}
		}
	}
}

void Test_GetWorksheetsAndColumns()
{
	Folder fld = Project.ActiveFolder();  // Get the active folder
	vector<string> vs;
	vector<int> vi;
	GetWorksheetsAndColumns(fld, vs, vi);  // Get all column C in the active folder recursively
	int numCols = vs.GetSize();  // How many column C
	Worksheet wks;
	wks.Create("Origin");  // Create a new worksheet
	wks.SetSize(-1, numCols);
	for(int ii = 0; ii < numCols; ii++)  // Loop to copy data
	{
		Column colC(vs[ii], vi[ii]);  // Source column C
		if(colC.IsValid())
		{
			vector v = colC.GetDataObject();  // Get source column C data
			Column colDest(wks, ii);  // Destination column
			vector& vDest = colDest.GetDataObject();  // Get reference vector of the destination column
			vDest = v;  // Put data to destination column
			colDest.SetLongName(vs[ii]);  // Set long name
		}
	}
}


Penn
Go to Top of Page

javanapoleon

Indonesia
1 Posts

Posted - 02/01/2014 :  04:33:24 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Great, thanks

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