#110779 - 2003-12-22 06:36 PM
syntax frustration
|
JJscorpio
Fresh Scripter
Registered: 2002-06-17
Posts: 34
Loc: Houston, Tx
|
I am trying to read an excel file into an array. Excel sheet is 6 columns wide by about 300 rows deep. The following code seems like it should work... but I keep getting an expected']' error I know I am doing something wrong in my syntax, but not sure what. Code:
For $Column=0 to 6 For $cell=0 to 300;ubound($a) $b[$column,$cell]=$oXL.cells($column,$cell) Next Next
|
|
Top
|
|
|
|
#110781 - 2003-12-22 06:42 PM
Re: syntax frustration
|
JJscorpio
Fresh Scripter
Registered: 2002-06-17
Posts: 34
Loc: Houston, Tx
|
This is a modification of the READEXCEL() UDF by Kdyer I wanted to be able to read and filter the array later in the script without having to read the source file over and over... But I am doing something wrong.
Thank You for your help! Code:
FUNCTION READEXCEL($cfilename) DIM $Rc,$Column,$Cell,$oXL,$a[300],$b[300] ;$a=1,2,3,4,5,6,7;columns and order to be read IF 0=EXIST($cfilename) ;insure the file exists ?'Excel file not found' SLEEP 4 RETURN ;leave ENDIF $oXL=Createobject('Excel.application') ;Check to insure that Excel is available IF 0<>@error ?@error ' Excel Application is not found' SLEEP 4 RETURN ENDIF $Rc=$oXL.workbooks.open($cfilename) For $Column=0 to 6 For $cell=0 to 300;ubound($a) $b[$column,$cell]=$oXL.cells($column,$cell) Next Next $oXL.quit ;quit Excel $oXL=0 ;set the object to 0
|
|
Top
|
|
|
|
#110784 - 2003-12-22 06:55 PM
Re: syntax frustration
|
JJscorpio
Fresh Scripter
Registered: 2002-06-17
Posts: 34
Loc: Houston, Tx
|
Jens, Thank you! That was the problem... I have found lots of information on single dimension arrays, but very little on multidimensional. This is the second "fundamental" array question you have helped me understand  Thank you.
|
|
Top
|
|
|
|
#110785 - 2003-12-22 07:02 PM
Re: syntax frustration
|
JJscorpio
Fresh Scripter
Registered: 2002-06-17
Posts: 34
Loc: Houston, Tx
|
Les, Good point! That was my next challange, I have not been able to call the results outside the udf! I think I just missed the endfunction in my cut and paste. The complete code is as follows (minus kixform) Code:
$ListViewEx1.Items.Clear $Populate = Readexcel('C:\Clients\Able\Price.xls') ;Excel file is a hundred or so rows of data 6 columns wide
For $LColumn=0 to 6 $List = $ListViewEx1.items.add For $Lcell=0 to ubound($a) List.SubItems($Lcell).text = $b[$Lcolumn,$Lcell] Next Next
$Form.Show While $Form.Visible $=Execute($Form.DoEvents()) Loop Exit 1
;*************************************
FUNCTION READEXCEL($cfilename) DIM $Rc,$Column,$Cell,$oXL,$a[300],$b[6,300] ;$a=1,2,3,4,5,6,7;columns and order to be read IF 0=EXIST($cfilename) ;insure the file exists ?'Excel file not found' SLEEP 4 RETURN ;leave ENDIF $oXL=Createobject('Excel.application') ;Check to insure that Excel is available IF 0<>@error ?@error ' Excel Application is not found' SLEEP 4 RETURN ENDIF $Rc=$oXL.workbooks.open($cfilename) For $Column=0 to 6 For $cell=0 to 300;ubound($a) $b[$column,$cell]=$oXL.cells($column,$cell) Next Next $oXL.quit ;quit Excel $oXL=0 ;set the object to 0
ENDFUNCTION
I have a little cleaning to do... I did not use $a at all and need to global dim the array. I have been trying to learn arrays from others code. Not understaing all of it this code is a bit of a mess!
|
|
Top
|
|
|
|
#110788 - 2003-12-22 07:20 PM
Re: syntax frustration
|
JJscorpio
Fresh Scripter
Registered: 2002-06-17
Posts: 34
Loc: Houston, Tx
|
Les, I have not written many udf's and am really just starting to get the more advanced concepts available in Kix. I see if you Globally Dim in a UDF you could wipe out values other have used in the scripts. Jens, I will RTFM again and referance the FAQ's (I did RTFM before posting, but did not know the DIM was my problem, so I was not reading the right information) Thank You for the direction. I will follow-up once I think I have the code corrected.
|
|
Top
|
|
|
|
#110789 - 2003-12-22 08:24 PM
Re: syntax frustration
|
JJscorpio
Fresh Scripter
Registered: 2002-06-17
Posts: 34
Loc: Houston, Tx
|
OK, after reading up I found that for this to work I would have to DIM the variable globally, which is bad UDF form (due to the potential to step on other main-script variables. So I cannot do what I want (properly). So it seems to me that the Function in this case should be part of the main script instead of a function...
|
|
Top
|
|
|
|
#110790 - 2003-12-22 08:37 PM
Re: syntax frustration
|
Sealeopard
KiX Master
   
Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
|

If I understand you correctly, you want the array $b be available outside the UDF, right?
Then you just assign $b to the return variable which is the UDF name with a preceeding $-sign as explained in both the KiXtart Manual and the FAQ Forum.
Code:
FUNCTION READEXCEL($cfilename) DIM $Rc,$Column,$Cell,$oXL,$a[300],$b[6,300] ;$a=1,2,3,4,5,6,7 ;columns and order to be read IF 0=EXIST($cfilename) ;insure the file exists ?'Excel file not found' SLEEP 4 RETURN ;leave ENDIF $oXL=Createobject('Excel.application') ;Check to insure that Excel is available IF 0<>@error ?@error ' Excel Application is not found' SLEEP 4 RETURN ENDIF $Rc=$oXL.workbooks.open($cfilename)
For $Column=0 to 6 For $cell=0 to 300;ubound($a) $b[$column,$cell]=$oXL.cells($column,$cell) Next Next
$oXL.quit ;quit Excel $oXL=0 ;set the object to 0 $ReadExcel=$b ENDFUNCTION
BTW, to exit UDFs, one should use EXIT and the appropriate error code as illustated in the FAQ Forum and the UDF Guidelines.
It is also bad etiquette to rework an existing UDF to make it fit a specific case and not even rename the UDF. UDFs are supposed to be general-purpose functions with specifics provided through parameters.
_________________________
There are two types of vessels, submarines and targets.
|
|
Top
|
|
|
|
#110791 - 2003-12-22 09:01 PM
Re: syntax frustration
|
JJscorpio
Fresh Scripter
Registered: 2002-06-17
Posts: 34
Loc: Houston, Tx
|
Jens, Yes... that is what I was after. Thank you for all the constructive guidance. I will frequent the FAQ's and work on the etiquette. I am trying to understand the more advanced functions in Kix and am still missing some of the more basic ones
|
|
Top
|
|
|
|
#110794 - 2003-12-23 02:26 PM
Re: syntax frustration
|
JJscorpio
Fresh Scripter
Registered: 2002-06-17
Posts: 34
Loc: Houston, Tx
|
Thank You All. I have learned quite a bit here and really enjoy the Board. I keep learning I don't know as much as I think I do 
Happy Holidays to you All.
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1452 anonymous users online.
|
|
|