├── README.md ├── autoload └── excelview.vim ├── doc ├── excelview1.png └── excelview2.png └── plugin └── excelview.vim /README.md: -------------------------------------------------------------------------------- 1 | # excelview-vim 2 | 3 | ![](https://raw.github.com/mattn/excelview-vim/master/doc/excelview1.png) 4 | 5 | ![](https://raw.github.com/mattn/excelview-vim/master/doc/excelview2.png) 6 | 7 | # Usage 8 | 9 | :ExcelView c:/path/to/Book1.xlsx 10 | 11 | You can specify sheet number. 12 | 13 | :ExcelView c:/path/to/Book1.xlsx 2 14 | 15 | # Requirements 16 | 17 | * [webapi-vim](https://github.com/mattn/webapi-vim) 18 | 19 | # License 20 | 21 | MIT 22 | 23 | # Author 24 | 25 | Yasuhiro Matsumoto 26 | -------------------------------------------------------------------------------- /autoload/excelview.vim: -------------------------------------------------------------------------------- 1 | function! s:loadSharedStrings(f) 2 | let ss = [] 3 | try 4 | let xml = system(printf("unzip -p -- %s xl/sharedStrings.xml", shellescape(a:f))) 5 | let doc = webapi#xml#parse(xml) 6 | for si in doc.childNodes("si") 7 | let t = si.childNode("t") 8 | call add(ss, t.value()) 9 | endfor 10 | catch 11 | endtry 12 | return ss 13 | endfunction 14 | 15 | function! s:loadSheetData(f, s) 16 | let xml = system(printf("unzip -p -- %s xl/worksheets/sheet%d.xml", shellescape(a:f), a:s)) 17 | 18 | let ss = s:loadSharedStrings(a:f) 19 | let doc = webapi#xml#parse(xml) 20 | let rows = doc.childNode("sheetData").childNodes("row") 21 | let cells = map(range(1, 256), 'map(range(1,256), "''''")') 22 | let aa = char2nr('A') 23 | for row in rows 24 | for col in row.childNodes("c") 25 | let r = col.attr["r"] 26 | let nv = col.childNode("v") 27 | let v = empty(nv) ? "" : nv.value() 28 | if has_key(col.attr, "s") && col.attr["s"] == "2" 29 | let v = strftime("%Y/%m/%d %H:%M:%S", (v - 25569) * 86400 - 32400) 30 | endif 31 | if has_key(col.attr, "t") && col.attr["t"] == "s" 32 | let v = ss[v] 33 | endif 34 | let x = char2nr(r[0]) - aa 35 | let y = matchstr(r, '\d\+') 36 | let cells[y][x+1] = v 37 | endfor 38 | endfor 39 | for y in range(len(cells)-1) 40 | let cells[y+1][0] = y + 1 41 | endfor 42 | for x in range(len(cells[0])-1) 43 | let nx = x / 26 44 | if nx == 0 45 | let cells[0][x+1] = nr2char(aa+x) 46 | else 47 | let cells[0][x+1] = nr2char(aa+nx-1) . nr2char(aa+x%26) 48 | endif 49 | endfor 50 | return cells 51 | endfunction 52 | 53 | function! s:fillColumns(rows) 54 | let rows = a:rows 55 | if type(rows) != 3 || type(rows[0]) != 3 56 | return [[]] 57 | endif 58 | let cols = len(rows[0]) 59 | for c in range(cols) 60 | let m = 0 61 | let w = range(len(rows)) 62 | for r in range(len(w)) 63 | if type(rows[r][c]) == 2 64 | let s = string(rows[r][c]) 65 | endif 66 | let w[r] = strdisplaywidth(rows[r][c]) 67 | let m = max([m, w[r]]) 68 | endfor 69 | for r in range(len(w)) 70 | let rows[r][c] = ' ' . rows[r][c] . repeat(' ', m - w[r]) . ' ' 71 | endfor 72 | endfor 73 | return rows 74 | endfunction 75 | 76 | function! excelview#view(...) abort 77 | if a:0 > 2 78 | echohl Error | echon "Usage: :ExcelView [filename] {[sheet-number]}" | echohl None 79 | return 80 | endif 81 | let [f, s] = a:0 == 1 ? [a:1, 1] : [a:1, a:2] 82 | try 83 | let data = s:loadSheetData(f, s) 84 | catch 85 | let e = v:exception 86 | echohl Error | echon printf("Error while loading sheet%d: %s", s, e) | echohl None 87 | return 88 | endtry 89 | new 90 | setlocal noswapfile buftype=nofile bufhidden=delete nowrap norightleft modifiable nolist nonumber 91 | let data = s:fillColumns(data) 92 | let sep = "+" . join(map(copy(data[0]), 'repeat("-", len(v:val))'), '+') . "+" 93 | call setline(1, sep) 94 | let r = 2 95 | for row in data 96 | let line = join(row, '|') 97 | call setline(r, '|'.join(row, '|').'|') 98 | call setline(r + 1, sep) 99 | let r += 2 100 | endfor 101 | setlocal nomodifiable 102 | endfunction 103 | -------------------------------------------------------------------------------- /doc/excelview1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattn/excelview-vim/a91ab834471dd84844d0290cba881b97bd237d6d/doc/excelview1.png -------------------------------------------------------------------------------- /doc/excelview2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattn/excelview-vim/a91ab834471dd84844d0290cba881b97bd237d6d/doc/excelview2.png -------------------------------------------------------------------------------- /plugin/excelview.vim: -------------------------------------------------------------------------------- 1 | command! -nargs=+ -complete=file ExcelView call excelview#view() 2 | --------------------------------------------------------------------------------