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
 ASCIMP_MODE_NEW_SHEETS

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
Thomas83 Posted - 07/19/2013 : 08:36:56 AM
Origin 8 SR 6
WIN 7

Hi.
I want to import a lot of files. The filenames are as follows:
1values600 2values600 3values600 ... 30values600
1values601 2values601 3values601 ... 30values601
1values602 2values602 3values602 ... 30values602
1values603 2values603 3values603 ... 30values603
...
1values699 2values699 3values699 ... 30values699

So, I have 30 groups of files (group 1 contains all file with "1values..." , group 2 all files with "2values..." , etc.). Each group should be imported to a new worksheet. I thought this can be done with the command "iMode = ASCIMP_MODE_NEW_SHEETS", but it doesn´t work in my code. Can someone show me the fault? I mean ASCIMP_MODE_APPEND_ROWS, ASCIMP_MODE_APPEND_COLS and ASCIMP_MODE_REPLACE_DATA work. Why not ASCIMP_MODE_NEW_SHEETS?
As a second possibility:
Each time in the first "for" loop (i.e. "for (int i=1; i<=3; i++)" in the code) I create a new worksheet and set the iMode = ASCIMP_MODE_APPEND_COLS. The problem is that this new worksheet is not active and I don´t know how to activate.

#include <Origin.h>

void Import_Picture()
{
    Worksheet wks;
    wks.Create();
    string strName = "Picture 1";
    wks.SetName(strName);
    //WorksheetPage wp = Project.Pages(); 
    for (int i=1; i<=30; i++) //corresponding to 3 groups
    {
    //int nn = wp.AddLayer("Picture "+(i+1)); //used for the second option
        
    	
			string strFile = ("C:\\Test\\Data\\1values624.txt");
			ASCIMP  ai;
			if(0 == AscImpReadFileStruct(strFile, &ai) )
        		ai.iMode = ASCIMP_MODE_NEW_SHEETS;
        		ai.iRenameCols = 0; 
        		ai.iPartial = 1;  
        		ai.iPartialC1 = 0; 
        		ai.iPartialC2 = 0; 
        		ai.iPartialR1 = 0; 
        		ai.iPartialR2 = -1; 
    			ai.iDelimited = 1;
        		ai.iDelimiter = ASCIMP_DELIM_OTHER;
        		ai.nNumSep = NF_IS_EUROPEAN;
 
    		if(0 == wks.ImportASCII(strFile, ai))
        		out_str("Import data successful.");
           
	
    	for (int k=0 ; k<=9 ; k++) //corresponding to "i"values601.txt to "i"values609.txt
    		{	
				string strFile = (""C:\\Test\\Data\\"+i+"values60"+k+".txt");
				ASCIMP  ai;
				if(0 == AscImpReadFileStruct(strFile, &ai) )
        			ai.iMode = ASCIMP_MODE_APPEND_COLS; 
        			ai.iRenameCols = 0; 
        			ai.iPartial = 1;  
        			ai.iPartialC1 = 1; 
        			ai.iPartialC2 = 1; 
        			ai.iPartialR1 = 0; 
        			ai.iPartialR2 = -1; 
    				ai.iDelimited = 1;
        			ai.iDelimiter = ASCIMP_DELIM_OTHER;
        			ai.nNumSep = NF_IS_EUROPEAN;

    			if(0 == wks.ImportASCII(strFile, ai))
        		out_str("Import data successful.");
    		}
    		
    	for ( k=10 ; k<=99 ; k++)
   			 {	
				string strFile = (""C:\\Test\\Data\\"+i+"values6"+k+".txt");
				ASCIMP  ai;
				if(0 == AscImpReadFileStruct(strFile, &ai) )
        			ai.iMode = ASCIMP_MODE_APPEND_COLS; 
        			ai.iRenameCols = 0; 
        			ai.iPartial = 1;  
        			ai.iPartialC1 = 1; 
        			ai.iPartialC2 = 1; 
        			ai.iPartialR1 = 0; 
        			ai.iPartialR2 = -1; 
    				ai.iDelimited = 1;
        			ai.iDelimiter = ASCIMP_DELIM_OTHER;
        			ai.nNumSep = NF_IS_EUROPEAN;

    			if(0 == wks.ImportASCII(strFile, ai))
        		out_str("Import data successful.");
    			}
    	}
}


Thanks in advance.
Regards
2   L A T E S T    R E P L I E S    (Newest First)
rlewis Posted - 07/19/2013 : 8:27:44 PM
The challenge in this case is that of determining the group IDs of the files imported and maintaining the connection between the group IDs and the worksheet object into which each group is to be imported. The code posted below (three functions) parses the file name to determine the group ID of the file, creates worksheets for storing data from the various file groups found, and maintains the connection between the file groupID and the target Worksheet by means of a string vector and an array of worksheet pointers. The code posted is of more general design with no preset limits as to the number of files or the number of filegroups that can be imported (aside from Origin's internal limits of the number of workbooks and worksheet columns that can be created).



#include <Origin.h>
#include <Array.h>

void ImportFileGroups()
{
	int iNumSelFiles;
	StringArray saFileTypes;
	StringArray saFilePaths;
	saFileTypes.SetSize(1);
	saFileTypes[0]="[FileType (*.txt)] *.txt";
	iNumSelFiles = GetMultiOpenBox(saFilePaths, saFileTypes);
	if (iNumSelFiles<=0)	
	{
		out_str("No Files Selected for Import ...");
		return;
	}
	
	// Declare a string vector to store the Group Numbers of the imported files ...
	vector<string> strGroupIds;
	strGroupIds.SetSize(0);
	
	// Declare a Worksheet array to store pointers to the worksheets containng data from files of the same Group Number
	Array<Worksheet&> WksArray;
	WksArray.SetAsOwner(false);
	WksArray.SetSize(0);
	
	Worksheet Wks;
	ASCIMP  ai;
	int FilesImported=0;
	for(int i=0;i<iNumSelFiles;i++)
	{
		string strPathToFile=saFilePaths[i];
		
		// Parse the filename to obtain the Group Number of the files to be imported ...
		string strFileName=GetFileName(strPathToFile,true);
		int ValStart=FindSubString(strFileName,"values");
		if(ValStart==0)
		{
			out_str("Incompatible File Name Specification ... "+strFileName);
			out_str("File Ignored ...");
			continue;			
		}
		string strGroupNumber=strFileName.Mid(0,ValStart);
		if(IsStringAllNumeric(strGroupNumber)==false)
		{
			out_str("Incompatible File Name Specification ... "+strFileName);
			out_str("File Ignored ...");
			continue;
		}

		// Determine whether a file of the  Group number "GroupNumber" has already been imported ...
		int iLocation=-1;
		for(int j=0;j<strGroupIds.GetSize();j++)
		{
			if(strGroupNumber.CompareNoCase(strGroupIds[j])==0)
			{
				iLocation=j;
				break;
			}
		}
		
		if(iLocation>=0)
		{
			/*
				File(s) of the  Group number "strGroupNumber" have already been imported ...
				The Group number of the file is stored in strGroupIDs[iLocation]
				A pointer to the worksheet storing data from files of this Group Number is stored in WksArray[iLocation]
			*/
			
			// Access the Worksheet object referenced by WksArray[iLocation]
			Wks=WksArray.GetAt(iLocation);
			if(Wks.IsValid()==false)
			{
				out_str("Worsheet validation fail ...");
				goto ErrorExit;
			}
			
			//  Import data as per the specificatons of the second,  third,  fourth ..... files of the group...
			if(AscImpReadFileStruct(strPathToFile, &ai)!=0)
			{
				out_str("AscImpReadFileStruct fail ...");
				goto ErrorExit;
			}
			ai.iMode = ASCIMP_MODE_APPEND_COLS;
			//ai.iRenameCols = 1; 
			//ai.iPartial = 1;  
			//ai.iPartialC1 = 1; 
			//ai.iPartialC2 = 1; 
			//ai.iPartialR1 = 0; 
			//ai.iPartialR2 = -1; 
			//ai.iDelimited = 1;
			//ai.iDelimiter = ASCIMP_DELIM_OTHER;
			//ai.nNumSep = NF_IS_EUROPEAN;
			if(Wks.ImportASCII(strPathToFile, ai)!=0)
			{
				out_str("Data Import Fail ...");
				goto ErrorExit;
			}
			FilesImported++;
		}
		else
		{
			/*
				File(s) of the  Group number "strGroupNumber" have not been imported ...
				Create a New Workbook
				Append a pointer to  Worksheet 0 of the newly created workbook to the worksheet array WbkArray
				Append the Group Number to the integer array  strGroupIDs
			*/
			WorksheetPage wP;
			if(wP.Create("origin",CREATE_VISIBLE)==false)
			{
				out_str("Origin Workbook Create Fail ...");
				goto ErrorExit;
			}
			Wks=wP.Layers(0);
			if(Wks.IsValid()==false)
			{
				out_str("Worsheet validation fail ...");
				goto ErrorExit;
			}
			wP.SetLongName("Group_"+strGroupNumber);
			
			// Create a pointer to the newly created worksheet and append it to WksArray.
			Worksheet *pWks = new Worksheet;
			pWks=&Wks;
			WksArray.Add(*pWks);
			
			// Append strGroupNumber to the string Array strGroupIds
			strGroupIds.Add(strGroupNumber);
			
			//  Import data as per the specificatons of the first file of the group..
			if(AscImpReadFileStruct(strPathToFile, &ai)!=0)
			{
				out_str("AscImpReadFileStruct fail ...");
				goto ErrorExit;
			}
			ai.iMode = ASCIMP_MODE_APPEND_COLS;
			//ai.iRenameCols = 1; 
			//ai.iPartial = 1;  
			//ai.iPartialC1 = 0; 
			//ai.iPartialC2 = 0; 
			//ai.iPartialR1 = 0; 
			//ai.iPartialR2 = -1; 
			//ai.iDelimited = 1;
			//ai.iDelimiter = ASCIMP_DELIM_OTHER;
			//ai.nNumSep = NF_IS_EUROPEAN;
			if(Wks.ImportASCII(strPathToFile, ai)!=0)
			{
				out_str("Data Import Fail ...");
				goto ErrorExit;
			}
			FilesImported++;
		}
	}
	out_str("File Import Completed ... "+FilesImported+" Files ... "+strGroupIds.GetSize()+" File Groups");
	return;
	
ErrorExit:
	// On error Clean up the mess before exiting ...
	for(i=0;i<WksArray.GetSize();i++)
	{
		Wks=WksArray.GetAt(i);
		if(Wks.IsValid()==true)
		{
			Wks.GetPage().Destroy();
		}
	}
}

int FindSubString(string strHostString, string strSubString)
{
	int HostStringLength=strHostString.GetLength();
	int subStringLength=strSubString.GetLength();
	if(HostStringLength==0 || subStringLength==0 || subStringLength>=HostStringLength)
	{
		return (-1);
	}
	for(int i=0; i<(HostStringLength-subStringLength); i++)
	{
		string strTemp=strHostString.Mid(i,subStringLength);
		{
			if(strTemp.CompareNoCase(strSubString)==0)
			{
				return (i);
			}
		}
	}
	return (-1);
}

bool IsStringAllNumeric(string strSearchString)
{
	for(int i=0;i<strSearchString.GetLength();i++)
	{
		char chNum=strSearchString.GetAt(i);
		if(chNum<48 || chNum>57)
		{
			return (false);
		}
	}
	return (true);
}
Castiel Posted - 07/19/2013 : 12:14:02 PM
  • Why activating the worksheet created?
    int nn = wp.AddLayer();
    Worksheet wks = wp.Layers(nn);
    wks.ImportASCII(strFile, ai);
  • To activate a layer,
    int nn = wp.AddLayer();
    set_active_layer( wp.Layers(nn) );


©c¡Ï   ¤È¥¹  ©f¨u©c  ©c¥ì    ¥Î¤³  ©c/     ©¦£¯
 ©c¨Ê¥Î ¤´¥¨  ¥ó ¨×   ¥ó¤á' ¥Ì©¦¥­   /¤í  ¥Õ©¦©f
   ¨Ö             ¦á          ©¦£þ  ©` ¥Õ   ©¦
                              ©¦

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