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
 Origin Forum
 loop through Workbooks with imported data
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

DrKlein1

Germany
6 Posts

Posted - 12/15/2017 :  11:00:33 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): Origin2015G
Operating System:Windows 7 professional

Hi,
I want to import several ASCII-Files (*.dat) using

dlgfile gr:=*.dat mu:=1 init:="K:\Mat\Daten\QE2";
newbook;

which allows me to select multiple files at a time. Then, each .dat file is imported into a new workbook using

impASC
options.sparklines:=0
options.FileStruct.Delimiter:=2
options.FileStruct.NumericSeparator:=0
options.Cols.NumCols:=20
options.Cols.AutoColTypes:=0
options.Cols.MaxTestLns:=1000
options.Hdr.AutoSubHdr:=0;

I played with the "options" of "ImpASC" a little and with these options I get the data into a workbook all right.

Next step would be to manipulate each workbook with a script by looping through all the workbooks I just imported.
But I can't figure out a way that will work for all ASCII filenames.

Once the files are imported, Origin truncates or changes their short names if they are not unique, contain "_" or leading numbers or other special characters (which happens to be the case in my case). I can find out the file names by using

fdlog.get(a,ii,1);

in a loop where ii is the loop index and "a" is a string containing the original name of the ASCII-file (which is the long name of the workbook after import). But to activate a workbook, I need the short name.

I also would not want to loop over all workbooks in the project since there might be other workbooks in the folder which I do not want to manipulate.

I also could not get the command "options.scripts.ScriptAfterEachImport:=...."
to work. It is also not very handy since the script after each import is several lines long.

Bottom line:
I think I need a way to get the short names of the worksbooks with the imported data or
a way to execute a (longer) script after each import or
a way to loop only over the recently imported workbooks.

Thanks in advance,
Oliver

Shirley_GZ

China
Posts

Posted - 12/18/2017 :  04:38:13 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Oliver,

Please kindly note that the command "options.scripts.ScriptAfterEachImport:=...." is only available since Origin 2016.

In addition, once you imported the files, you can get the file name and activate the corresponding workbook in this way:

fname$=fdlog.get(a,ii,1);
range rr=[%(fname$)];
win -a %(rr);

Please consider to upgrade your Origin to our current version Origin 2018. And don't hesitate to let us know the problem you met.

Thanks,
Shirley
OriginLab



Originlab Technical Service Team
Go to Top of Page

FJoel

Germany
5 Posts

Posted - 01/29/2018 :  08:55:38 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,
I am very new to Origin at all and uses OriginPro 2017G Version.
Similar to Olivers issue I have some further questions.
I imported multiple .*txt files with impasc fname:=fns$ analogs to your example on the webpage (2.10.1) successfully into multiple books names book1 to bookn.

After importing I want to add columns with a function and finally use the X-Function wxt to manipulate the data in each imported workbook.

How can I use the "doc -ef W{} function for looping over all books, and how can I tell the wxt function the book name?

Bellow, you can find a not working script with some comments.

I really appreciate your help and thank you in advance,

Joel


// Import multiple files with the extension *.txt from the path ref:2.10.1 Importing Data
string fns, path$=C:\Users...; 
findfiles fname:=fns$ ext:="*.txt";
int n = fns.GetNumTokens(CRLF);
string bkName$;
newbook s:=0 result:=bkName$;
impasc fname:=fns$  
options.ImpMode:=3     		    /* start with new book */
options.Sparklines:=2 		    /* add sparklines if < 50 */
options.Names.AutoNames:=0 	    /* turn off auto rename */
options.Names.FNameToBk:=0          /* do not rename the workbook */
options.Names.FNameToSht:=0         /* rename sheet to file name */
options.Names.FNameToBkComm:=1      /* add file name to book comment */
options.Hdr.AutoSubHdr:=1;
Options.FileStruct.NumSep:=1	 
Options.Headers.LongName:= 1		// Longname in row 1
orng:=[bkName$]A1!A[1]:C[0];            // What does these line mean?
// copyed from the top
fname$=fdlog.get(a,ii,1);
range rr=[%(fname$)];
win -a %(rr);
// loop over every Workbook W in project folder -ef ref: 2.12.3
doc -ef W {
int nsheets = 0;
{
// Starting with the Script for every workbook: activating worksheet 1
wks.col = 1;
// Adding 3 columns with the name X Y and Z
wks.insert[fname$]Sheet1!(X Y Z);
// Set Long Name by using col
[fname$]"Sheet1"!col(1)[L]$ = C/x;
// Set Units by using range
[fname$]"Sheet1"!col(1)[U]$ = A ;
// Set Comments by using range
[fname$]"Sheet1"!col(1)[C]$ = cell1;
// insert function to columns:
[Book8]"Sheet1"!col(Z)[o]$=col(A)/10;
// Manipulating the data with the wxt function (4.1.1) and copying selected rows into new sheet
wxt -r 2 test:="col(F)=0 AND col(G)=1" iw:=[Book8]"Sheet 1"! c2:=10 ow:=<0>;
wxt -r 2 test:="col(F)=1" iw:=[Book8]"Sheet 1"! c2:=10 ow:=<1>; 
};
}



Go to Top of Page

greg

USA
1379 Posts

Posted - 01/29/2018 :  6:01:12 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You can use a StringArray to keep track of the resulting window name, then loop through each of those and do whatever...

string fns, path$=C:\Program Files\OriginLab\Origin2018\Samples\Import and Export;
findfiles fname:=fns$ ext:="*.dat";
StringArray sa;
loop(ii,1,fns.GetNumTokens(CRLF))
{
impasc fname:=fns$
options.ImpMode:=3
options.Sparklines:=2
options.Names.AutoNames:=0
options.Names.FNameToBk:=0
options.Names.FNameToSht:=0
options.Names.FNameToBkComm:=1
options.Hdr.AutoSubHdr:=1
Options.FileStruct.NumSep:=1
Options.Headers.LongName:= 1;1
sa.Add(%H); // Add window name to stringarray
}
loop(ii,1,sa.GetSize())
{
str$ = sa.GetAt(ii)$;
win -a %(str$);
// Process code here;
}


There is no hope!
(But I may be wrong.)
Go to Top of Page

FJoel

Germany
5 Posts

Posted - 01/30/2018 :  02:38:29 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Greg,

thank you for the quick reply.
Unfortunately, I can not fix the problem with your example. I tried to modify the parts but the code still only imports the data but do not process the data.

string fns, path$=C:\Users...; 
findfiles fname:=fns$ ext:="*.txt";
StringArray sa;
loop(ii,1,fns.GetNumTokens(CRLF))
{
impasc fname:=fns$  
options.ImpMode:=3     		    /* start with new book */
options.Sparklines:=2 		    /* add sparklines if < 50 */
options.Names.AutoNames:=1 	    /* turn off auto rename */
options.Names.FNameToBk:=0          /* do not rename the workbook */
options.Names.FNameToSht:=0         /* rename sheet to file name */
options.Names.FNameToBkComm:=1      /* add file name to book comment */
options.Hdr.AutoSubHdr:=1;
Options.FileStruct.NumSep:=1	 
sa.Add(%H); 		// Add window name to stringarray
}
loop(ii,1,sa.GetSize())
{
str$ = sa.GetAt(ii)$;
win -a %(str$);
// my process code
wks.col = 1;
wks.insert(X Y Z);
col(1)[L]$ = C/x;
col(2)[L]$ = n(Li);
col(3)[L]$ = Capacity;
col(1)[U]$ = ;
col(2)[U]$ = mol;
col(3)[U]$ = mAh.g;
col(1)[C]$ = cell1;
col(Z)[o]$=col(A)/0,0063;
wxt -r 2 test:="col(F)=0 AND col(G)=1" c2:=10 ow:=<0>;
wxt -r 2 test:="col(F)=1" c2:=10 ow:=<1>; 
}


Many thanks for any help,

Joel
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