Last active
September 25, 2018 04:43
-
-
Save uttesh/02258f65f398b84a227e9a95e40925be to your computer and use it in GitHub Desktop.
find data in excel file and return row data as JSON using Autoit script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Array.au3> | |
#include <Excel.au3> | |
#include <MsgBoxConstants.au3> | |
#include "JSONgen.au3" | |
$oJson = New_Json() | |
Local $oExcel = _Excel_Open() | |
Local $oWorkbook = _Excel_BookOpen($oExcel, "users.xlsx") | |
$sSearch = 'user_fn_8' | |
Local $aResult = _Excel_RangeFind($oWorkbook, $sSearch, Default, Default, $xlWhole) | |
ConsoleWrite("result : "& _ArrayToString($aResult)& @CRLF) | |
ConsoleWrite("ubound "& ubound($aResult)& @CRLF) | |
for $a=0 to ubound($aResult)-1 | |
Local $values = StringSplit($aResult[$a][2], "$") | |
ConsoleWrite("row id :"& $values[3]& @crlf) | |
getResponseJson($values[3],getColumnCount()) | |
Next | |
Func getResponseJson($row,$columCount) | |
For $i=1 To $columCount Step +1 | |
Json_AddElement($oJson, getValue(1,$i), getValue($row,$i)) | |
Next | |
EndFunc | |
Func getValue($row,$cell) | |
return $oExcel.Application.Cells($row,$cell).Value | |
EndFunc | |
Func getColumnCount() | |
return $oWorkbook.ActiveSheet.Usedrange.Columns.Count | |
EndFunc | |
$sTheJsonCode = Json_GetJson($oJson) | |
ConsoleWrite($sTheJsonCode) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#cs | |
JSONgen - JSON generator for AutoIt | |
Taken as base JSON spec at json.org | |
#ce | |
#include-once | |
Func New_JSON() | |
Dim $aSelf[1][3] | |
Return $aSelf | |
EndFunc | |
Func JSON_AddElement(ByRef $aSelf, $sKey, $sValue = Default) | |
$this = UBound($aSelf) | |
ReDim $aSelf[$this+1][3] | |
If $sValue <> Default Then | |
$aSelf[$this][1] = $sKey | |
$aSelf[$this][2] = $sValue | |
Return $aSelf | |
Else | |
$aSelf[$this][1] = $sKey | |
$aSelf[$this][2] = Default | |
EndIf | |
EndFunc | |
; Avoiderror is just to avoid problems if the user supplies the value | |
Func JSON_DeleteElement(ByRef $aSelf, $sKey ,$sAvoiderror = Null) | |
$j = UBound($aSelf)-1 | |
Dim $aNewArr[1][3] | |
$k = 0 | |
For $i = 1 To $j | |
If $aSelf[$i][1] <> $sKey Then | |
$k += 1 | |
ReDim $aNewArr[$k+1][3] | |
$aNewArr[$k][1] = $aSelf[$i][1] | |
$aNewArr[$k][2] = $aSelf[$i][2] | |
EndIf | |
Next | |
$aSelf = $aNewArr | |
Return $aNewArr | |
EndFunc | |
Func JSON_EditElement(ByRef $aSelf, $sKey, $sNewvalue) | |
JSON_DeleteElement($aSelf, $sKey) | |
JSON_AddElement($aSelf, $sKey, $sNewvalue) | |
Return $aSelf | |
EndFunc | |
Func JSON_GetJson($aSelf, $bHumanReadable = False) | |
$sOutput = "{" | |
$j = UBound($aSelf)-1 | |
For $i = 1 To $j | |
$iThis = $i-1 | |
$sKey = $aSelf[$i][1] | |
$sValue = $aSelf[$i][2] | |
If IsArray($sKey) And $sValue = Default Then | |
; Stand-alone array | |
$sOutput &= '"' & $iThis & '":' & __JSON_ArrayHelper($sKey) | |
$sOutput &= "," | |
ElseIf IsString($sKey) And $sValue = Default Then | |
; Stand-alone string/int | |
$sOutput &= '"' & $iThis & '":"' & __JSON_Filter($sKey) & '",' | |
ElseIf IsString($sKey) And Not IsArray($sValue) Then | |
; Associated (str)key=>(mix)value | |
If IsString($sValue) Then | |
$sOutput &= '"' & __JSON_Filter($sKey) & '":"' & __JSON_Filter($sValue) & '",' | |
ElseIf IsNumber($sValue) Then | |
$sOutput &= '"' & __JSON_Filter($sKey) & '":' & $sValue & "," | |
ElseIf IsBool($sValue) Then | |
$sOutput &= '"' & __JSON_Filter($sKey) & '":' & ($sValue ? 'true' : 'false') & ',' | |
ElseIf $sValue = Null Then | |
$sOutput &= '"' & __JSON_Filter($sKey) & '":null,' | |
EndIf | |
ElseIf IsString($sKey) And IsArray($sValue) Then | |
; Associated (str)key=>(arr)value | |
$sOutput &= '"' & __JSON_Filter($sKey) & '":' & __JSON_ArrayHelper($sValue) & ',' | |
EndIf | |
Next | |
$sOutput = StringTrimRight($sOutput, 1) & "}" | |
; Let's make it more human-readable | |
If $bHumanReadable Then | |
$sOutput = StringReplace($sOutput, "{", "{ ") | |
$sOutput = StringReplace($sOutput, "}", " }") | |
$sOutput = StringReplace($sOutput, ",", ", ") | |
$sOutput = StringReplace($sOutput, ":", ": ") | |
$sOutput = StringReplace($sOutput, "[", " [ ") | |
$sOutput = StringReplace($sOutput, "]", " ] ") | |
EndIf | |
Return $sOutput | |
EndFunc | |
; =========== internal use only ============== | |
Func __JSON_Filter($sValue) | |
$sValue = StringReplace($sValue, '"', '\"') | |
$sValue = StringReplace($sValue, '\', '\\') | |
$sValue = StringReplace($sValue, @CRLF, '\n') | |
$sValue = StringReplace($sValue, @CR, '\n') | |
$sValue = StringReplace($sValue, @LF, '\n') | |
Return $sValue | |
EndFunc | |
Func __JSON_ArrayHelper($aArr) | |
$output = "[" | |
For $mItem In $aArr | |
If IsNumber($mItem) Then | |
$output &= $mItem | |
ElseIf IsString($mItem) Then | |
$output &= '"' & __JSON_Filter($mItem) & '"' | |
ElseIf IsBool($mItem) Then | |
$output &= ($mItem ? 'true' : 'false') | |
ElseIf IsArray($mItem) Then | |
$output &= __JSON_ArrayHelper($mItem) | |
EndIf | |
$output &= "," | |
Next | |
$output = StringTrimRight($output, 1) & "]" | |
Return $output | |
EndFunc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment