Author |
Topic  |
|
kaiouu
Germany
Posts |
Posted - 10/13/2005 : 2:25:30 PM
|
Origin Version (Select Help-->About Origin): 7.5 Pro Operating System: Windows 2000
Hi,
I want to include header lines in exported data. The values of the header lines are stored in the system variables page.user.info.L1V1$...page.user.info.LNV1$, depending on the number of header lines. What i want, is that these strings are stored first in the export file, before the data are stored there. How could I do this?
Thanks, Kai |
|
Mike Buess
USA
3037 Posts |
Posted - 10/14/2005 : 12:01:58 PM
|
Hi Kai,
Use the Origin C function at the bottom like this...
ExportWksWithHeader 5 C:\Data\data.dat;
The first argument is the number of lines you want to save to header. The second argument is the file name. Omitting the second argument will bring up a file dialog.#Function starts here void ExportWksWithHeader(int nLines, string strFile = "") { Worksheet wks = Project.ActiveLayer(); if( !wks ) return; // wks must be active if( strFile.IsEmpty() ) { strFile = GetSaveAsBox("*.dat"); if( strFile.IsEmpty() ) return; // user cancelled file dialog } string str; WorksheetPage wpg = wks.GetPage(); stdioFile f1(strFile, file::modeCreate | file::modeWrite); for(int i=1; i<=nLines; i++) { if( !page_get_info_var_value(wpg, "L" + i + "V1", str, "user.variables") ) break; f1.WriteString("#" + str); } f1.WriteString("#Data"); // identify start of data ?? char szTemp[MAXFULLPATH]; GetTempPath(MAXFULLPATH, szTemp); string strTemp = szTemp + "temp.dat"; if( wks.ExportASCII(strTemp, WKS_EXPORT_ALL)<0 ) return; stdioFile f2(strTemp, file::modeRead); while( f2.ReadString(str) ) f1.WriteString(str); }
Mike Buess Origin WebRing Member
Edited by - Mike Buess on 10/16/2005 08:59:55 AM |
 |
|
kaiouu
Germany
Posts |
Posted - 10/17/2005 : 06:44:55 AM
|
Hi Mike,
thanks a lot, that was a good starting point. I extended the script, so that the column labels / titles are written before the header lines, and a dialogbox asks for the export options, so if anybody needs this...
Kai
#include <Origin.h> #include <GetNBox.h>
void ExportWksWithHeader(string defaultfilename) { Worksheet wks = Project.ActiveLayer(); DWORD selection; char tabchar='\t'; string strFile; if( !wks ) return; // wks must be active GETN_TREE(myTree) GETN_BEGIN_BRANCH(DataSelection, "Data Selection")
GETN_CHECK(expall, "Export all", true) GETN_CHECK(expsel, "Export selected", false) GETN_CHECK(expheading, "Export Column titles", false) GETN_CHECK(explabels, "Export Column labels", true) GETN_NUM(nR1, "First Row", 1) GETN_NUM(nR2, "Last Row", 2) GETN_NUM(nC1, "First Column",1 ) GETN_NUM(nC2, "Last Column", 2) GETN_END_BRANCH(DataSelection) GETN_LIST(separator, "Separator", 0, "tab|space|,") GETN_NUM(nStartLines, "Export Headerlines from", 1) GETN_NUM(nLines, "to", 100) GETN_BUTTON(filename, "File Path", defaultfilename)
if( GetNBox(myTree, "Export", NULL, NULL,myDialogOnClickButtonEvent)) { selection=0; if(myTree.DataSelection.expall.dVal ==true)selection=WKS_EXPORT_ALL; if(myTree.DataSelection.expsel.dVal ==true)selection=selection+WKS_EXPORT_SELECTED; if(myTree.DataSelection.expheading.dVal ==true)selection=selection+WKS_EXPORT_HEADING; if(myTree.DataSelection.explabels.dVal ==true)selection=selection+WKS_EXPORT_LABELS; strFile=myTree.filename.strVal; } int iRet; if (strFile.IsFile()) { iRet = MessageBox(GetWindow(), "File "+strFile+ " exists, Overwrite?", "Verification", MB_OKCANCEL | MB_ICONQUESTION); if( IDOK != iRet ) return; }
int nLines=myTree.nlines.dVal; if(myTree.separator.strVal=="0")tabchar='\t'; if(myTree.separator.strVal=="1")tabchar=' '; if(myTree.separator.strVal=="2")tabchar=','; if( strFile.IsEmpty() ) return; // user cancelled file dialog } string str; WorksheetPage wpg = wks.GetPage(); stdioFile f1(strFile, file::modeCreate | file::modeWrite); //f1.WriteString("#Data"); // identify start of data ?? char szTemp[MAXFULLPATH]; GetTempPath(MAXFULLPATH, szTemp); string strTemp = szTemp + "\\temp.dat"; int R1=myTree.DataSelection.nR1.dVal-1; int R2=myTree.DataSelection.nR2.dVal; int C1=myTree.DataSelection.nC1.dVal-1; int C2=myTree.DataSelection.nC2.dVal; if( wks.ExportASCII(strTemp, selection,tabchar,R1,C1,R2,C2)<0 ) return; stdioFile f2(strTemp, file::modeRead); if (myTree.DataSelection.expheading.dVal ==true) { f2.ReadString(str); f1.WriteString("#"+str); } if (myTree.DataSelection.explabels.dVal ==true) { f2.ReadString(str); f1.WriteString("#"+str); } int j=myTree.nStartLines.dVal; for(int i=1; i<=nLines; i++) { if( !page_get_info_var_value(wpg, "L" + i + "V1", str, "user.variables") ) break; j--; if (j<=0) f1.WriteString(str); } while( f2.ReadString(str) ) f1.WriteString(str); }
bool myDialogOnClickButtonEvent(TreeNode& myTree, int nRow, int nType, Dialog& Dlg) { if(TRGP_STR_BUTTON == nType) // If Browse button clicked then open browse dialog { string strPath = GetSaveAsBox("*.dat"); if( !strPath.IsEmpty() ) myTree.filename.strVal = strPath; } return true; // Return true to indicate update of display is needed }
|
 |
|
|
Topic  |
|
|
|