Advertisement
berban1

Untitled

Feb 8th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ParseCSV("C:\file.csv", "oField_*_*")
  2. Return
  3.  
  4. ScrollLock::
  5. n++
  6. Clip(Field_%n%_1)
  7. Send {Tab}
  8. Clip(Field_%n%_2)
  9. Send {Tab}
  10. Clip(Field_%n%_3)
  11. Send {Tab}
  12. Send % Field_%n%_4
  13. Return
  14.  
  15. ;============================================= Functions (Ignore this part) =============================================
  16.  
  17. ParseCSV(File, Options="") ; http://www.autohotkey.com/community/viewtopic.php?t=78105
  18. {
  19.     @5 := 1, Literal := """", Commands := "r|c|d|l|o|t|nl", Column := 1, Row := 1, Tolerance := 1
  20.     ;---------------------------------OPTIONS:-----------------------------------------------------------
  21.     ; DEFAULT                 USER DEFAULT            NAME                    ABOUT
  22.     , r := "All",             r_user := ""            ;r = Rows               Rows to query. Use commas & dashes (e.g. 1,3,5-9) as in the windows "pages to print" dialog
  23.     , c := "All",             c_user := ""            ;c = Columns            Columns to query. Same format as above
  24.     , d := ",",               d_user := "`t"          ;d = Delimiter          Character that delimits cells
  25.     , l := """",              l_user := "'"           ;l = Literal            Character that indicates literal text
  26.     , o := "",                o_user := "Array_*_*"   ;o = Output Array       Name of output pseudoarray created using Global(). First asterisk is rows and second is columns
  27.     , t := True,              t_user := False         ;t = Trim               Trims the rightmost row if it is empty in all cells
  28.     , nl := "`r`n",           nl_user := ""           ;nl = New Line          Any of these characters will be considered a new line
  29.     ;---------------------------------USER CONFIGURATIONS:-----------------------------------------------
  30.     , UserConfig_Foo := "x12 y34"
  31.     , UserConfig_Bar := "cWhite -a"
  32.     ;----------------------------------------------------------------------------------------------------
  33.     While (@5 := RegExMatch(Options, "i)(?:^|\s)(?:!(\w+)|(\+|-)?(" Commands ")(" Literal "(?:[^" Literal "]|" Literal Literal ")*" Literal "(?=\s|$)|\S*))", @, @5 + StrLen(@)))
  34.         If (@1 <> "")
  35.             Options := SubStr(Options, 1, @5 + StrLen(@)) UserConfig_%@1% SubStr(Options, @5 + StrLen(@))
  36.         Else If (@4 <> "") {
  37.             If (InStr(@4, Literal) = 1) and (@4 <> Literal) and (SubStr(@4, 0, 1) = Literal) and (@4 := SubStr(@4, 2, -1))
  38.                 StringReplace, @4, @4, %Literal%%Literal%, %Literal%, All
  39.             %@3% := @4
  40.         } Else
  41.             %@3% := @2 = "+" ? True : @2 = "-" ? False : %@3%_user
  42.     FileRead, File, % @ := File
  43.     If ErrorLevel {
  44.         MsgBox, 262160, %A_ScriptName% - %A_ThisFunc%(): Error, Could not read CSV file`n"%File%"
  45.         Return
  46.     }
  47.     Loop, Parse, File
  48.     {
  49.         If PrimeRow
  50.             If !(PrimeRow -= 1) {
  51.                 If !Widest or (Widest - Column <= Tolerance)
  52.                     Row += 1
  53.                 Column := 0, PrimeColumn := True
  54.             } Else
  55.                 Continue
  56.         If !InStr(nl, A_LoopField) or IsLiteral {
  57.             If PrimeColumn
  58.                 Column += 1, PrimeColumn := False
  59.             If (A_LoopField = l) {
  60.                 If (Previous = l)
  61.                     Array_%Row%_%Column% .= l
  62.                 IsLiteral := !IsLiteral
  63.             } Else If (A_LoopField <> d) or IsLiteral
  64.                 Array_%Row%_%Column% .= A_LoopField
  65.             Else If t
  66.                 PrimeColumn := True
  67.             Else
  68.                 Column += 1
  69.             If (Column > Widest)
  70.                 Widest := Column
  71.         } Else If !InStr(nl, Previous)
  72.             PrimeColumn := False, PrimeRow := 2
  73.         Previous := A_LoopField
  74.     }
  75.     If (o <> "") {
  76.         If !InStr(o, "*")
  77.             o := o "_*_*"
  78.         Loop %Row% {
  79.             StringReplace, @, o, *, % ThisRow := A_Index
  80.             Loop %Widest% {
  81.                 StringReplace, @@, @, *, %A_Index%
  82.                 Global(@@, Array_%ThisRow%_%A_Index%)
  83.             }
  84.         }
  85.     }
  86.     If (r <> "") or (c <> "") {
  87.         r := Expand(!r or (r = "All") ? "1-" Row : SubStr(r, 0) = "-" ? r Row : r), c := Expand(!c or (c = "All") ? "1-" Widest : SubStr(c, 0) = "-" ? c Widest : c)
  88.         Loop, Parse, r, `,
  89.         {
  90.             @ := A_LoopField
  91.             Loop, Parse, c, `,
  92.                 Output .= "," CSV(Array_%@%_%A_LoopField%)
  93.         }
  94.         ErrorLevel := Row A_Space Widest
  95.         Return SubStr(Output, 2)
  96.     } Else
  97.         Return Row A_Space Widest
  98. }
  99.  
  100. Global(Var, Value)
  101. {
  102.     Global
  103.     %Var% := Value
  104. }
  105.  
  106. Expand(Input)
  107. {
  108.     Loop, Parse, Input, `,, %A_Tab%%A_Space%
  109.     {
  110.         StringSplit, Term, A_LoopField, -
  111.         If ((Term1 <> "") + (Term2 <> "") + (Term3 <> "") + (Term4 <> "") > 1) {
  112.             Loop 4
  113.                 If (Start <> "") {
  114.                     If (Term%A_Index% <> "") {
  115.                         End := A_Index = 4 ? -Term%A_Index% : Term%A_Index%
  116.                         Break
  117.                     }
  118.                 } Else If (Term%A_Index% <> "")
  119.                     Start := A_Index = 2 ? -Term%A_Index% : Term%A_Index%
  120.             While (A_Index <= End - Start + 1)
  121.                 Output .= "," Start + A_Index - 1
  122.             Term1 := "", Term2 := "", Term3 := "", Term4 := "", Start := ""
  123.         } Else
  124.             Output .= "," A_LoopField
  125.     }
  126.     Return SubStr(Output, 2)
  127. }
  128.  
  129. CSV(Text, Delimiter=",", Literal="""")
  130. {
  131.     If (InStr(Text, Literal) = 1) or InStr(Text, Delimiter) or InStr(Text, "`n") or InStr(Text, "`r") {
  132.         StringReplace, Text, Text, %Literal%, %Literal%%Literal%, All
  133.         Text := Literal Text Literal
  134.     }
  135.     Return Text
  136. }
  137.  
  138. Clip(Text="", Reselect="") ; http://www.autohotkey.com/forum/viewtopic.php?p=467710 , modified February 19, 2013
  139. {
  140.     Static BackUpClip, Stored, LastClip
  141.     If (A_ThisLabel = A_ThisFunc) {
  142.         If (Clipboard == LastClip)
  143.             Clipboard := BackUpClip
  144.         BackUpClip := LastClip := Stored := ""
  145.     } Else {
  146.         If !Stored {
  147.             Stored := True
  148.             BackUpClip := ClipboardAll ; ClipboardAll must be on its own line
  149.         } Else
  150.             SetTimer, %A_ThisFunc%, Off
  151.         LongCopy := A_TickCount, Clipboard := "", LongCopy -= A_TickCount ; LongCopy gauges the amount of time it takes to empty the clipboard which can predict how long the subsequent clipwait will need
  152.         If (Text = "") {
  153.             SendInput, ^c
  154.             ClipWait, LongCopy ? 0.6 : 0.2, True
  155.         } Else {
  156.             Clipboard := LastClip := Text
  157.             ClipWait, 10
  158.             SendInput, ^v
  159.         }
  160.         SetTimer, %A_ThisFunc%, -700
  161.         Sleep 20 ; Short sleep in case Clip() is followed by more keystrokes such as {Enter}
  162.         If (Text = "")
  163.             Return LastClip := Clipboard
  164.         Else If (ReSelect = True) or (Reselect and (StrLen(Text) < 3000)) {
  165.             StringReplace, Text, Text, `r, , All
  166.             SendInput, % "{Shift Down}{Left " StrLen(Text) "}{Shift Up}"
  167.         }
  168.     }
  169.     Return
  170.     Clip:
  171.     Return Clip()
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement