Author |
Topic  |
|
aakanik
India
15 Posts |
Posted - 02/10/2016 : 01:29:01 AM
|
Origin Ver. and Service Release (Select Help-->About Origin): OriginPro 8.5 Operating System: Windows 10
Hello,
I am beginner in coding with Origin and don't have much idea about language. I am trying to develop code using Origin C and have succeeded up to bit extent. I have imported two ASCII files into worksheets (i.e, wks2 and wks3) and have tried copying but m stuck up with copying specifying column in particular earlier created worksheet.
The steps I want to do are: 1) Copy second column of source worksheet (wks3) to third column of destination worksheet (wks2). Please note that both worksheets are already created. 2) subtract third column from second column of destination worksheet and put result in fourth column of same worksheet.
Kindly help me with these. Many thanks in advance..!!
newuser |
|
Castiel
343 Posts |
Posted - 02/10/2016 : 6:42:18 PM
|
quote: Originally posted by aakanik
Origin Ver. and Service Release (Select Help-->About Origin): OriginPro 8.5 Operating System: Windows 10
Hello,
I am beginner in coding with Origin and don't have much idea about language. I am trying to develop code using Origin C and have succeeded up to bit extent. I have imported two ASCII files into worksheets (i.e, wks2 and wks3) and have tried copying but m stuck up with copying specifying column in particular earlier created worksheet.
The steps I want to do are: 1) Copy second column of source worksheet (wks3) to third column of destination worksheet (wks2). Please note that both worksheets are already created. 2) subtract third column from second column of destination worksheet and put result in fourth column of same worksheet.
Kindly help me with these. Many thanks in advance..!!
newuser
something similar to
vectorbase& vbTo = wks2.Columns(2).GetDataObject(); vectorbase& vbFrom = wks3.Columns(1).GetDataObject(); vbTo = vbFrom;
妾+ 午旦 妹罕妾 妾伊 用仇 妾/ 岫ㄞ
妾京用 仍巨 件 侈 件戶' 甘岫平 /欠 白岫妹
併 艮 岫 奈 白 岫
岫 |
 |
|
aakanik
India
15 Posts |
Posted - 02/11/2016 : 03:16:04 AM
|
Hello Castiel,
Thanks for your quick reply..!!  I tried with these commands but it isn't working..
newuser |
 |
|
Castiel
343 Posts |
Posted - 02/11/2016 : 5:47:09 PM
|
quote: Originally posted by aakanik
Hello Castiel,
Thanks for your quick reply..!!  I tried with these commands but it isn't working..
newuser
Please show your code.
妾+ 午旦 妹罕妾 妾伊 用仇 妾/ 岫ㄞ
妾京用 仍巨 件 侈 件戶' 甘岫平 /欠 白岫妹
併 艮 岫 奈 白 岫
岫 |
 |
|
aakanik
India
15 Posts |
Posted - 02/12/2016 : 06:14:49 AM
|
Hello Castiel,
I am pasting my code below, please refer it:
#include <Origin.h> ////////////////////////////////////////////////////////////////////////////////////
//#pragma labtalk(0) // to disable OC functions for LT calling.
//////////////////////////////////////////////////////////////////////////////////// // Include your own header files here.
void hello() { Note note; note.Create(); // Create the target Note window LT_set_str("type.notes$", note.GetName()); LT_set_var("type.redirection", 2); // 2 for Note window out_str("* File is selected \n"); WorksheetPage page(); page.Create(NULL, CREATE_EMPTY); page.SetName("FIRST FILE"); printf("* Page Name is %s\n\n", page.GetName()); if(0 == page.Layers.Count()) { // FIRST WORKSHEET LAYER page.AddLayer("Data File"); int index = 0; foreach(Layer lay in page.Layers) { // Prompt user with a File Open dialog to choose a file to import. string strFile = GetOpenBox("*.txt"); if( strFile.IsEmpty() ) out_str("* Please select another data file"); ASCIMP ai; if( 0 == AscImpReadFileStruct(strFile, &ai) ) { ai.iAutoSubHeaderLines = 1; // Able auto detect sub header ai.iSubHeaderLines = 4; ai.nLongNames = ai.iHeaderLines; ai.iMaxLabels = 0; // Not set any header to Comments label ai.iRenameCols =5; // use 5th line of header block to rename col ai.iLabels = 2; // header block has 2 lines ai.iMaxLabels = 2; // comments has two lines ai.nUnits=1; // use 1st line of header block to set Units ai.iDelimited = 1; // use delimiter ai.iDelimiter = ASCIMP_DELIM_DEMICOLON; // semicolon as delimiter Worksheet wks = Project.ActiveLayer(); // Get active worksheet from active work book. if( 0 == wks.ImportASCII(strFile, ai) ) // Return 0 for no error { // The names of the user parameter labels vector<string> vsUserLabels = {"Expanded Description", "Type Indication"}; // Set user parameter labels to specified names Grid grid; grid.Attach(wks); grid.SetUserDefinedLabelNames(vsUserLabels); wks.AutoSize(); // Resize column widths to best fit their contents. wks.Columns(0).SetLongName("sample1"); wks.Columns(1).SetLongName("Sample2"); wks.Columns(2).SetLongName("sample3"); wks.Columns(3).SetLongName("sample4"); wks.Columns(0).SetUnits("mm"); wks.Columns(1).SetUnits("mm"); wks.Columns(2).SetUnits("mm"); wks.Columns(3).SetUnits("mm"); wks.SetName("SELECTED_FILE") } Worksheet wks1 = Project.ActiveLayer(); if(wks1) { Grid gg1; gg1.Attach(wks1); if(gg1) { printf("* Number of Rows and Columns in datafile are :%d and %d respectively\n", gg1.Rows, gg1.Cols); } } } printf("layer %d named %s\n\n", index, lay.GetName()); index++; } // SECOND WORKSHEET LAYER page.AddLayer("Band File"); int index1 = 1; foreach(Layer lay1 in page.Layers) { printf("layer %d named %s\n\n", index, lay1.GetName()); index1++; } } else printf("Error, fail to create empty workbook\n"); // COPY WORKSHEET INTO NEW WORKSHEET Worksheet wksSource = Project.ActiveLayer(); if( wksSource ) { Worksheet wks2; // Create a new worksheet to be the target of our copy wks2.Create("origin"); int nC1 = 0, nC2 = 1; // Copy from all columns, Use -1 to indicate last column int nR1 = 0, nR2 = -1; // Copy from all columns, Use -1 to indicate last column int nDestC1 = 0; // Copy to the target worksheet starting at the 1st column DWORD dwCtrl = CPYT_COPY_COLUMN_FORMAT | CPYT_COPY_COLUMN_DESIGNATIONS; // Also copy column formats and designations int nRet = wksSource.CopyTo(wks2, nC1, nC2, nR1, nR2, nDestC1, -1, dwCtrl);
wks2.SetName("INPUT_DATA"); wks2.Columns(0).SetLongName("sample1"); wks2.Columns(1).SetLongName("sample2"); wks2.Columns(1).SetComments("A"); wks2.Columns(0).SetUnits("mm"); wks2.Columns(1).SetUnits("mm"); int nColIndex = wks2.AddCol(); wks2.Columns(2).SetLongName("sample3"); wks2.Columns(2).SetUnits("mm"); wks2.Columns(2).SetComments("B"); int nColIndex1 = wks2.AddCol(); wks2.Columns(3).SetComments("C"); WorksheetPage wksPg("Book1"); if( wksPg ) // if there is a workbook named "Book1" wksPg.SetName("INPUT"); // rename the workbook } // THIRD WORKSHEET (BG DATA FILE) WorksheetPage background; background.Create("origin"); background.SetName("bg"); // Prompt user with a File Open dialog to choose a file to import. string strFile = GetOpenBox("*.txt"); if( strFile.IsEmpty() ) out_str("* Please select bg data file\n"); // User canceled or error ASCIMP ai; if( 0 == AscImpReadFileStruct(strFile, &ai) ) { ai.iAutoSubHeaderLines = 1; // Able auto detect sub header ai.iSubHeaderLines = 4; ai.nLongNames = ai.iHeaderLines; ai.nUnits = ai.iHeaderLines + 1; ai.iMaxLabels = 2; // comments has two lines ai.iRenameCols = 5; // use 5th line of header block to rename col ai.iLabels = 2; // header block has 2 lines ai.iDelimited = 1; // use delimiter ai.iDelimiter = ASCIMP_DELIM_DEMICOLON; // semicolon as delimiter Worksheet wks3 = Project.ActiveLayer(); //get active worksheet from active work book. if( 0 == wks3.ImportASCII(strFile, ai) ) // Return 0 for no error { vector<string> vsUserLabels = {"Expanded Description", "Type Indication"}; Grid gg3; gg3.Attach(wks3); gg3.SetUserDefinedLabelNames(vsUserLabels); wks3.AutoSize(); // Resize column widths to best fit their contents. wks3.Columns(0).SetLongName("sample1"); wks3.Columns(1).SetLongName("Sample2"); wks3.Columns(2).SetLongName("sample3"); wks3.Columns(3).SetLongName("sample4"); wks3.Columns(0).SetUnits("mm"); wks3.Columns(1).SetUnits("mm"); wks3.Columns(2).SetUnits("mm"); wks3.Columns(3).SetUnits("mm"); wks3.SetName("BACKGROUND_DATA"); } page.SetShow(); } }
Here, I have total three worksheets among which two are of imported ASCII file i.e, wks and wks3 whereas one worksheet wks2 is created for data processing. Now, I want to copy second column of wks3 to third column of wks2. Also, after copying I want to subtract newly copied third column with second column of same worksheet (wks2) and put result in it's fourth column. I am trying since many days but yet haven't got any result..!!
Regards, aakanik
newuser |
 |
|
Castiel
343 Posts |
Posted - 02/14/2016 : 02:08:56 AM
|
quote: Originally posted by aakanik
int nC1 = 0, nC2 = 1; // Copy from all columns, Use -1 to indicate last column int nR1 = 0, nR2 = -1; // Copy from all columns, Use -1 to indicate last column int nDestC1 = 0; // Copy to the target worksheet starting at the 1st column DWORD dwCtrl = CPYT_COPY_COLUMN_FORMAT | CPYT_COPY_COLUMN_DESIGNATIONS; // Also copy column formats and designations int nRet = wksSource.CopyTo(wks2, nC1, nC2, nR1, nR2, nDestC1, -1, dwCtrl);
It means, copy the 1st column and the 2nd column from wksSource to the 1st column of wks2.
To copy the 2nd column of wksSource to the 3rd column of wks2, it should have been nC1 = 1, nC2 = 1, nDstC1 = 2.
妾+ 午旦 妹罕妾 妾伊 用仇 妾/ 岫ㄞ
妾京用 仍巨 件 侈 件戶' 甘岫平 /欠 白岫妹
併 艮 岫 奈 白 岫
岫 |
 |
|
aakanik
India
15 Posts |
Posted - 02/15/2016 : 03:45:14 AM
|
Hello Castiel,
I guess, you have misunderstood my question.. But thank you for your suggestion..!! :)
Copying of first and second column was also needed to be done from imported worksheet wks1 to first and second column of worskheet wks2. That I have done successfully as seen in code.
Now, I wanted to copy second column of different imported worksheet wks3 to third column of worksheet wks2. And then I wanted to get result of subtraction (3 column - 2 column) in fourth column of same wks2. That was my problem..!! I am unable to copy desired column in already created worksheet from already existing worksheet. I referred to help blog of Origin and even googled it out, I could find codes for copying and pasting in new worksheet but I am not able to find way out how to copy paste in already existing worksheets from already created ones.. As my both worksheets are created long before doing this mathematical operation.
This is bit confusing and I am sorry if m not able to express my question effectively.. But, by now thing would have been clear to you I guess..!!
Thanks..
newuser |
 |
|
aakanik
India
15 Posts |
Posted - 02/26/2016 : 06:17:46 AM
|
Please help..!!
newuser |
 |
|
cpyang
USA
1406 Posts |
Posted - 02/26/2016 : 8:32:23 PM
|
Using CopyTo should work, so can you put here a small example of code that does not work for you?
Below I have put down the simplest code to illustrate the usage of CopyTo,
//copy data and headings (not short name, not plot designation)
//from book1 col(2) to book2 col(1)
void dd()
{
Worksheet wk1("[Book1]!1");
Worksheet wk2("[Book2]!1");
if(wk1 && wk2)
{
DWORD dwCntrl = CPYT_COPY_COLUMN_LABELS;
wk1.CopyTo(wk2, 1,1,0,-1,0,-1, dwCntrl);
}
}
CP
|
 |
|
aakanik
India
15 Posts |
Posted - 03/01/2016 : 12:58:46 AM
|
First of all thank you so much for taking pain to read my problem, understand and your kind reply..!!
I tried your suggestion but it is also not working..
I am very new to this software and is learning to write codes with Origin. Now am stuck up with very foolish but basic problem of copying desired column from already existing worksheet to already existing one.. I am trying very hard since almost two months to fix my problem but I am not able to find way out thus lastly I posted here. As per your request I have cut sorted out many of lines and have pasted very few lines, indicating only basic idea, below.. In case if these are long then kindly forgive me, I am sorry..
As seen in my code first I have created a worksheet (SELECTED_FILE) for importing data ASCII file, then I have created another worksheet (INPUT) for pasting only two column from it and finally I have created third worksheet (BACKGROUND) for importing bg ASCII file. These works very well, but now after this I want to activate BACKGROUND worksheet again and copy only second column of it to third column of worksheet INPUT. Also after that I want to subtract col2 - col3 and finally put result in col4 of same INPUT worksheet.
I am really frustrated with these after trying for almost two months. I can't express in words but that will be great help if these gets fixed as I am really struggling to find way out.
#include <Origin.h>
void hello() { WorksheetPage page(); page.Create(NULL, CREATE_EMPTY); if(0 == page.Layers.Count()) { // FIRST WORKSHEET LAYER page.AddLayer("Data File"); int index = 0; foreach(Layer lay in page.Layers) { string strFile = GetOpenBox("*.txt"); // Prompt user with a File Open dialog to choose a file to import. ASCIMP ai; if( 0 == AscImpReadFileStruct(strFile, &ai) ) { ai.iDelimited = 1; // use delimiter ai.iDelimiter = ASCIMP_DELIM_DEMICOLON; // semicolon as delimiter Worksheet wks = Project.ActiveLayer(); // Get active worksheet from active work book. if( 0 == wks.ImportASCII(strFile, ai) ) // Return 0 for no error { vector<string> vsUserLabels = {"Expanded Description", "Type Indication"}; // The names of the user parameter labels Grid grid; // Set user parameter labels to specified names grid.Attach(wks); grid.SetUserDefinedLabelNames(vsUserLabels); wks.AutoSize(); // Resize column widths to best fit their contents. wks.SetName("SELECTED_FILE") } } } } else printf("Error, fail to create empty workbook and select data file\n"); // COPY WORKSHEET INTO NEW WORKSHEET Worksheet wksSource = Project.ActiveLayer(); if( wksSource ) { Worksheet wks2; // Create a new worksheet to be the target of our copy wks2.Create("origin"); int nC1 = 0, nC2 = 1; // Copy only first and second column from SELECTED_FILE int nR1 = 0, nR2 = -1; // Copy all rows from only both column int nDestC1 = 0; // Copy to the target worksheet starting at the 1st column int nRet = wksSource.CopyTo(wks2, nC1, nC2, nR1, nR2, nDestC1, -1); wks2.SetName("INPUT_DATA"); // rename the worksheet WorksheetPage wksPg("Book1"); if( wksPg ) // if there is a workbook named "Book1" wksPg.SetName("INPUT"); // rename the workbook int nColIndex = wks2.AddCol(); // Add third column to copy column from background worksheet int nColIndex1 = wks2.AddCol(); // Add fourth column for putting answers of substraction, col2 - col3 } // THIRD WORKSHEET (BACKGROUND DATA FILE) WorksheetPage bg; bg.Create("origin"); bg.SetName("BACKGROUND"); string strFile = GetOpenBox("*.txt"); // Prompt user with a File Open dialog to choose a file to import. ASCIMP a2; if( 0 == AscImpReadFileStruct(strFile, &a2) ) { a2.iDelimited = 1; // use delimiter a2.iDelimiter = ASCIMP_DELIM_DEMICOLON; // semicolon as delimiter Worksheet wks3 = Project.ActiveLayer(); //get active worksheet from active work book. if( 0 == wks3.ImportASCII(strFile, a2) ) // Return 0 for no error { vector<string> vsUserLabels = {"Expanded Description", "Type Indication"}; Grid gg3; gg3.Attach(wks3); gg3.SetUserDefinedLabelNames(vsUserLabels); wks3.AutoSize(); // Resize column widths to best fit their contents. wks3.SetName("BACKGROUND_DATA"); } page.SetShow(); } }
newuser |
 |
|
cpyang
USA
1406 Posts |
Posted - 03/01/2016 : 10:52:21 AM
|
I verified the following works using your code and I made book1 and book2 in Origin. I fill book1 with two columns of data
void dd()
{
Worksheet wksSource("[Book1]!1");
Worksheet wks2("[Book2]!1");
if(wksSource && wks2)
{
int nC1 = 0, nC2 = 1; // Copy only first and second column from Book1
int nR1 = 0, nR2 = -1; // Copy all rows from only both column
int nDestC1 = 0; // Copy to the target worksheet starting at the 1st column
int nRet = wksSource.CopyTo(wks2, nC1, nC2, nR1, nR2, nDestC1, -1);
}
}
I can try to run through with your code, but without your data file, I don't see how to do it. Also, I noticed you are using a very old version, and that might be another reason for your trouble, do you have access to a more recent version like at least Origin 2015?
CP
|
 |
|
aakanik
India
15 Posts |
Posted - 01/19/2017 : 02:14:22 AM
|
Thank you for taking pains and helping me cpyang..!! 
I am bounded to work with same version of origin and have to troubleshoot the problem with the available resources. After trying hard for almost a year finally I solved the problem.
for anyone, who is struggling to address same problem I am pasting codes below:
Worksheet wks4 = "INPUT"; Column colC; colC.Attach(wks4, 3); string strExpression = "y-x"; string strBeforeScript = "range y=col(B);" + "\r\n" + "range x=col(C);"; string strFormula = strExpression + STR_COL_FORMULAR_SEPARATOR + strBeforeScript; colC.SetFormula(strFormula, AU_AUTO); colC.ExecuteFormula();
Once again my warm regards and hearty thanks to origin team who took pains and tried every possible ways to help me..!!  
newuser |
 |
|
Aaron03
Australia
2 Posts |
Posted - 02/17/2017 : 12:50:21 AM
|
That's good info Thanks for this. |
 |
|
|
Topic  |
|
|
|