AutoHotkey pass data using Excel and the Vim

Excel is a scenario N columns you want to copy the data back to Excel and then treated with Vim.

Vim provides an interface for other languages ​​ole call, see: h ole.txt.

  A, Excel content to the new buffer Vim

First select the region to obtain the contents of Excel

rng := ComObjActive("Excel.application").selection

  Here are two functions:

  1. Vim is currently activated and to ensure that new buffer
  2. The new Excel-arrV wrote Vim buffer
GVim_newBuffer (); open new buffer 
{ 
    IF ProcessExist ( "of gvim.exe") 
    { 
        WinShow ( "ahk_class Vim") 
        WinActivate ( "ahk_class Vim") 
        oVim: = ComObjActive ( "Vim.Application") 
        IF oVim.eval ( ' strlen (join (getline (1, "$"), "\ r \ n")) '); if two blank lines that can not empty buffer 
            oVim.eval (' execute ( "$ tabnew ") ') 
    } 
    the else 
        RUN ( "D: \ Soft \ Vim \ gvim.exe"); gvim.exe replace their path 
} 

; ARRV can be a string 
GVim_setBuffer (ARRV) 
{ 
    oVim: = ComObjActive ( "Vim.Application") 
    IF IsObject (ARRV) 
    { 
        Loop (ARRV.MaxIndex(1))
        {
            r := A_Index
            sRow := ""
            loop(arrV.MaxIndex(2))
                sRow .= arrV[r,A_Index].delete0() . A_Tab
            oVim.eval(format('setline({1},iconv("{2}","cp936","utf-8"))',r,RTrim(sRow,A_Tab)))
        }
    }
    else ;字符串
        oVim.eval(format('setline(1,iconv("{1}","cp936","utf-8"))',arrV))
  oVim.SendKeys('gg') ;顺便刷新Vim }

  Then the whole calling code:

rng := ComObjActive("Excel.application").selection
GVim_newBuffer()
if (rng.cells.count = 1)
    GVim_setBuffer(rng.value)
else
    GVim_setBuffer(rng.value)
WinActivate("ahk_class Vim")

Second, from the Vim buffer contents to Excel

 Vim can obtain the current contents of the command buffer with the following (string format), need for transcoding ahk iconv to use.

oVim := ComObjActive("Vim.application")
rs := oVim.eval('line("$")') ;行数
str := oVim.eval('iconv(join(getline(1,"$"),"\r\n"),"utf-8","cp936")')

Str first line of the tab divided by several columns of data have to know (if the number of the back rows and columns will be lower than the first error, and the number of columns can be more)

loop parse, str, "`n", "`r"
{
    cs := StrSplit(A_LoopField, A_Tab).length()
    break
}

  Can then be transformed into a ComObjArray string array, then a selection can be written Excel

arrA := ComObjArray(12, rs, cs)
loop parse, str, "`n", "`r"
{
    r := A_Index-1
    for k, v in StrSplit(A_LoopField, A_Tab)
        arrA[r,k-1] := v
}
ComObjActive("Excel.application").selection.cells(1).value := arrA

 

This is a fresh-baked code written today, hope to help you, thank you. Please indicate the source

Guess you like

Origin www.cnblogs.com/hyaray/p/11318715.html