#161481 - 2006-05-03 01:29 AM
How to output excel data and use in function?
|
jvdejesus
Fresh Scripter
Registered: 2005-12-15
Posts: 17
|
I'm trying to gather info from an excel sheet and using that information to be used on a fnGroupAD() and AddToMyNetworkPlaces() function. I've managed to use the readexcel2(), fngroupAD(), and AddToMyNetworkPlaces() function. Unfortunately, I have not successfully figured out how to output the information gathered from an excel sheet to be used in these functions.
Here's the code I'm managed to get so far(I did not include the functions):
Code:
$range=readexcel2('C:\test.xls',,-1,4,1,1) if @error "error occured: @serror (@error)" else for $counter=1 to ubound($range) $GroupName='$range[$counter,0]' $ShareName='$range[$counter,1]' $SharePath='$range[$counter,2]' $Comment='$range[$counter,3]' select fnGroupAD("$GroupName") case AddToMyNetworkPlaces('$ShareName','$SharePath','$Comment') endif next
It reads excel data and outputs correctly when I use the following output query:
Code:
? ""$range[$counter,0] ? ""$range[$counter,1] ? ""$range[$counter,2] ? ""$range[$counter,3]
It's probably simple that I have missed with the "For Next" command. If you can point me to the right direction, it would be greatly appreciated.
Thanks, Jimmy
|
|
Top
|
|
|
|
#161482 - 2006-05-03 08:27 PM
Re: How to output excel data and use in function?
|
jvdejesus
Fresh Scripter
Registered: 2005-12-15
Posts: 17
|
I tried the execute() function within kix32 but no go... Anyone have an idea?
|
|
Top
|
|
|
|
#161485 - 2006-05-03 08:55 PM
Re: How to output excel data and use in function?
|
jvdejesus
Fresh Scripter
Registered: 2005-12-15
Posts: 17
|
oops sorry, that's a typo. Here's what I have so far: Code:
$range=readexcel2('C:\test.xls',,-1,4,1,1) if @error "error occured: @serror (@error)" else for $counter=1 to ubound($range) $GroupName = Execute( '$range[$counter,0]' ) $ShareName = Execute( '$range[$counter,1]' ) $SharePath = Execute( '$range[$counter,2]' ) $Comment = Execute( '$range[$counter,3]' ) if fngroupAD("$GroupName") AddToMyNetworkPlaces('$ShareName','$SharePath','$Comment') endif next
Basically, I'm trying to figure out how to properly translate the $GroupName, $ShareName, etc. so that the functions (addmynetworkplaces and fngroupAD) can properly get the information from the excel file. Hope this is clear enough. Thanks.
Jimmy
Edited by jvdejesus (2006-05-03 09:10 PM)
|
|
Top
|
|
|
|
#161487 - 2006-05-04 12:10 AM
Re: How to output excel data and use in function?
|
jvdejesus
Fresh Scripter
Registered: 2005-12-15
Posts: 17
|
Quote:
I don't get why you are using Execute() and why you wrap vars in quotes.
$GroupName = $range[$counter,0]
Is there a right way of extracting excel information to be used on a function? I got the example straight from Joeel's readexcel2() function
Code:
;Example: ; ;read the default sheet (1) to the first empty field (with 3 columns). ; $range=readexcel2('C:\Documents and Settings\niemjo\Työpöytä\puhluett.xls',,-1,3) ; if @error ; "error occured: @serror (@error)" ; else ; for $counter=0 to ubound($range,2) ; ? "name: " $range[$counter,0] ; ? "phonenumber: " $range[0,1] ; ? "cellular: " $range[0,2] ; ? ; next ;
Edited by jvdejesus (2006-05-04 12:10 AM)
|
|
Top
|
|
|
|
#161489 - 2006-05-04 01:03 AM
Re: How to output excel data and use in function?
|
jvdejesus
Fresh Scripter
Registered: 2005-12-15
Posts: 17
|
Quote:
What do you mean "to be used on a function"?
You already said:
Quote:
It reads excel data and outputs correctly when I use the following output query: ? ""$range[$counter,0] ? ""$range[$counter,1] ? ""$range[$counter,2] ? ""$range[$counter,3]
So then what is your problem?
The output only shows correctly on the kixtart prompt when the "?" is used. It is not being translated when the output is used on functions such as: fngroupad() or addtomynetworkplaces().
For example: when I use it like this: fngroupad($range[$counter,0]), the function does not work and no error is given. But when I use it like this: ? ""$range[$counter,0], I get an output on the kixtart prompt. Hope that was clear enough. Thanks.
|
|
Top
|
|
|
|
#161491 - 2006-05-04 01:40 AM
Re: How to output excel data and use in function?
|
jvdejesus
Fresh Scripter
Registered: 2005-12-15
Posts: 17
|
That is just a test to see if it outputs the data correctly. I've tried doing it this way: fngroupad($range[$counter,0]). However, it does not work.
|
|
Top
|
|
|
|
#161494 - 2006-05-04 03:52 AM
Re: How to output excel data and use in function?
|
jvdejesus
Fresh Scripter
Registered: 2005-12-15
Posts: 17
|
Hi Les, I tried your suggestion but it did not do anything. Here's the whole code I'm working with. I am actually calling the functions but just so you have everything, I put it all here.
Code:
$range=readexcel2('C:\test.xls',,-1,4,1,1) if @error "error occured: @serror (@error)" else for $counter=1 to ubound($range) $GroupName = "['+$range[$counter,0]+']" $ShareName = "['+$range[$counter,1]+']" $SharePath = "['+$range[$counter,2]+']" $Comment = "['+$range[$counter,3]+']" if ingroup("$GroupName") ? "It works" AddToMyNetworkPlaces('$ShareName','$SharePath','$Comment') endif next
Function fnInGroupAD($sGroup,Optional $bComputer) Dim $objSys,$objTarget,$aMemberOf,$sMemberOf $objSys = CreateObject("ADSystemInfo") $objTarget = GetObject("LDAP://"+Iif($bComputer,$objSys.ComputerName,$objSys.UserName)) $aMemberOf = $objTarget.GetEx("memberOf") For Each $sMemberOf in $aMemberOf If InStr($sMemberOf,"CN="+$sGroup+",") $fnInGroupAD = Not 0 Exit EndIf Next $fnInGroupAD = NOT 1 EndFunction
Function AddToMyNetworkPlaces($Name,$Location,Optional $Comment) Dim $Nethood,$Shell,$S,$D,$T $AddToMyNetworkPlaces=1 If Exist($Location) $Nethood=ReadValue('HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders','NetHood') If GetFileAttr($Nethood+'\'+$Name) & 16 ; 'Link exists already. Exit the function. Exit 80 EndIf MD $Nethood+'\'+$Name $D=$Nethood+'\'+$Name+'\'+'Desktop.ini' $T=$Nethood+'\'+$Name+'\'+'target.lnk' $S = SetFileAttr($Nethood+'\'+$Name,1) $S = WriteProfileString ($D,'.ShellClassInfo','CLSID2','{0AFACED1-E828-11D1-9187-B532F1E9575D}') $S = WriteProfileString ($D,'.ShellClassInfo','FLAGS','2') $S = SetFileAttr($D,6) $Shell = CreateObject('wscript.shell') If @ERROR Exit @ERROR EndIf $S = $Shell.CreateShortcut($T) $S.TargetPath = $Location $S.WorkingDirectory = $Location If $Comment $S.Description = $Comment EndIf $S.Save If @ERROR Exit @ERROR Else $AddToMyNetworkPlaces=0 EndIf Else Exit 3 EndIf EndFunction
;Function: ; ReadExcel2() ; ;Authors: ; Lonkero ; kdyer ; ;Version: ; 2.1 ; ;Version History: ; 2.1 16.5.2003 ; added ability to read full sheet (slow) ; added option to read all data (faster ) ; 2.0 16.5.2003 ; cleaned the code from console outputs ; added proper errorcodes ; added proper parameter-definitions ; proper closing of excel ; well, totally made this a new Xperience ; ; 1.4 (initial code = ReadExcel UDF) by kdyer ; http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000121 ; ; ;Action: ; Read from Excel to 2-dimensional array ; ;Syntax: ; ReadExcel2(FILE, SHEET, ROWCOUNT, COLUMNCOUNT, [ROWSTART], [COLUMNSTART]) ; ;Parameters: ; FILE ; REQUIRED STRING ; File to read from ; SHEET ; OPTIONAL INTEGER ; Sheet index from which to read. ; default = 1 ; ROWCOUNT ; OPTIONAL INTEGER ; Amount of Rows to read ; if -1 reads until empty field found at the start of row ; if left out reads all rows ; COLUMNCOUNT ; OPTIONAL INTEGER ; Amount of Columns to read ; if -1 reads until empty field found at row 1 of Column ; if left out reads all Columns ; ROWSTART ; OPTIONAL INTEGER ; Row from which to start reading ; if not specified, defaults to 1 ; COLUMNSTART ; OPTIONAL INTEGER ; Column from which to start reading ; if not specified, defaults to 1 ; ;Remarks: ; Excel uses row and column numbers starting from 1 ; thus the resulting range that starts from [0,0] might confuse a little ; ;Returns: ; 2-dimensional array (table) or ; nothing if failed (see errorcode for reason) ; ;Errorcodes: ; 0 ERROR_SUCCESS The operation was successfully completed. ; 2 ERROR_FILE_NOT_FOUND The system cannot find the file specified. ; 87 ERROR_INVALID_PARAMETER The parameter is incorrect. ; 1154 ERROR_INVALID_DLL Excel does not exist or is too old to support com. ; ;Dependencies: ; Excel -97 or newer ; ;Example: ; ;read the default sheet (1) to the first empty field (with 3 columns). ; $range=readexcel2('C:\Documents and Settings\niemjo\Työpöytä\puhluett.xls',,-1,3) ; if @error ; "error occured: @serror (@error)" ; else ; for $counter=0 to ubound($range,2) ; ? "name: " $range[$counter,0] ; ? "phonenumber: " $range[0,1] ; ? "cellular: " $range[0,2] ; ? ; next ; ;Source: Function ReadExcel2($xlsFile,optional $xlsWS,optional $RowCount,optional $ColCount,optional $RowStart,optional $ColStart) dim $xlObj,$Counter,$Counter2 If 0=Exist($xlsFile) exit 2 Endif $xlObj=Createobject('Excel.application') If @error Exit 1154 Endif $= $xlObj.workbooks.open($xlsFile)
if 8<>vartype($xlsFile) exit 87 endif if vartype($xlsWS) if 3<>vartype($xlsWS) exit 87 endif if 1>$xlsWS exit 87 endif $xlObj.sheets($xlsWS).Activate. else $xlsWS=1 endif if vartype($RowCount) if 3<>vartype($RowCount) exit 87 endif if -1=$RowCount for $Counter=1 to $xlObj.sheets($xlsWS).rows.count if not $xlObj.cells($Counter,1).value $RowCount=$Counter-1 $Counter=$xlObj.sheets($xlsWS).rows.count endif next endif else $RowCount=$xlObj.sheets($xlsWS).rows.count
endif
if vartype($ColCount) if 3<>vartype($ColCount) exit 87 endif if -1=$ColCount for $Counter=1 to $xlObj.sheets($xlsWS).columns.count if not $xlObj.cells(1,$Counter).value $ColCount=$Counter-1 $Counter=$xlObj.sheets($xlsWS).columns.count endif next endif else $ColCount=$xlObj.sheets($xlsWS).columns.count
endif
if vartype($RowStart) if 3<>vartype($RowStart) exit 87 endif
if 1>$RowStart exit 87 endif else $RowStart=1 endif if vartype($ColStart) if 3<>vartype($ColStart) exit 87 endif if 1>$ColStart exit 87 endif else $ColStart=1 endif dim $Array[$RowCount-1,$ColCount-1]
$ReadExcel2=$Array
for $Counter=1 to $RowCount-$RowStart+1 for $Counter2=1 to $ColCount-$ColStart+1 $ReadExcel2[$Counter-1,$Counter2-1]=$xlObj.cells($Counter,$Counter2).value next next
$xlObj.workbooks.Close $xlObj.quit
EndFunction
|
|
Top
|
|
|
|
#161495 - 2006-05-04 08:10 AM
Re: How to output excel data and use in function?
|
Gargoyle
MM club member
   
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
|
Where are using this function Code:
Function fnInGroupAD($sGroup,Optional $bComputer) Dim $objSys,$objTarget,$aMemberOf,$sMemberOf $objSys = CreateObject("ADSystemInfo") $objTarget = GetObject("LDAP://"+Iif($bComputer,$objSys.ComputerName,$objSys.UserName)) $aMemberOf = $objTarget.GetEx("memberOf") For Each $sMemberOf in $aMemberOf If InStr($sMemberOf,"CN="+$sGroup+",") $fnInGroupAD = Not 0 Exit EndIf Next $fnInGroupAD = NOT 1 EndFunction
Also why are using quotes around your Vars here... Code:
AddToMyNetworkPlaces('$ShareName','$SharePath','$Comment')
_________________________
Today is the tomorrow you worried about yesterday.
|
|
Top
|
|
|
|
#161496 - 2006-05-04 02:08 PM
Re: How to output excel data and use in function?
|
jvdejesus
Fresh Scripter
Registered: 2005-12-15
Posts: 17
|
Hi gargoyle,
The the fngroupAD() function is being used. I just tested ingroup if that was working with my problem but I forgot to change that on my post. Unfortunately, it hasn't worked either with ingroup or fngroupAD. So is this what it should look like? Code:
AddToMyNetworkPlaces($ShareName,$SharePath,$Comment)
|
|
Top
|
|
|
|
#161498 - 2006-05-04 04:22 PM
Re: How to output excel data and use in function?
|
jvdejesus
Fresh Scripter
Registered: 2005-12-15
Posts: 17
|
OK here's the final code that worked for me:
Code:
$range=readexcel2('C:\test.xls',,-1,4,1,1) if @error "error occured: @serror (@error)" else for $counter=1 to ubound($range) $GroupName = ''+$range[$counter,0]+'' $ShareName = ''+$range[$counter,1]+'' $SharePath = ''+$range[$counter,2]+'' $Comment = ''+$range[$counter,3]+''? if fnIngroupAD($GroupName) '' + @ERROR + ' - ' + @SERROR ? AddToMyNetworkPlaces($ShareName,$SharePath,$Comment) endif next
Thanks to Les, Jochen and Gargoyle for their input!
Sincerely, Jimmy
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 271 anonymous users online.
|
|
|