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);
}