This repository has been archived by the owner on Mar 22, 2020. It is now read-only.
/
xls_kvs.rb
147 lines (126 loc) · 3.22 KB
/
xls_kvs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# XLS_KVS -- key-value store class library using Microsoft Excel.
# dependent on win32ole & Microsoft Excel
#
# Programmed by Toshiaki <koshiba+rubyforge@4038nullpointer.com>
# License : Ruby License
require 'win32ole'
require 'YAML'
class XLS_KVS
# <description>
#
# ARGS
#
# <arg> : <description>
#
# RETURNS
# <>
#
# Example
#
class << self
def load(path, sheet, is_readonly = false)
XLS_KVS::Hash.new(path, sheet, is_readonly)
end
end
class Hash
# include Enumerable
FIRSTITEM = 1
KEYVALUE_COLS = 'A:B'
KEY_COLS = 'A:A'
def Hash.finalize(app, book, is_readonly)
proc {
book.save if (book && !(is_readonly))
book.close({'SaveChanges' => !(is_readonly)}) if book
app.Quit if app
}
end
def initialize(path, sheet, is_readonly, server_name = nil)
@app = WIN32OLE.new('Excel.Application', server_name)
#@app.visible = true
@book = @app.Workbooks.Open(path,{'ReadOnly' => is_readonly})
@sheet = @book.sheets(sheet)
@sheet.activate
ObjectSpace.define_finalizer(self, Hash.finalize(@app, @book, is_readonly))
@lock = Mutex.new
rescue
close(false)
raise IOError.new
end
def [](key)
YAML.load(@app.WorksheetFunction.VLookup(YAML.dump(key),
@sheet.Range(KEYVALUE_COLS),
2,
false)
)
rescue
nil
end
def empty?
size == 0
end
def size
@app.WorksheetFunction.Counta(@sheet.Range(KEY_COLS))
end
def clear
@lock.synchronize {
@sheet.UsedRange.clear
}
self
end
def delete(key)
value = nil
@lock.synchronize {
value = self[key]
target_row = find(key).Row
@sheet.Range("#{target_row}:#{target_row}").Delete (-4162)
} if key?(key)
value
end
def store(key, value)
case @app.WorksheetFunction.CountIf(@sheet.Range(KEY_COLS),
YAML.dump(key)).to_i
when 0
insert(key, value)
when 1
replace(key, value)
else
raise IOError.new
end
value
end
def replace(key, value)
@lock.synchronize {
range = find(key)
range.offset(0, 1).value = YAML.dump(value) if range
}
end
def find(key)
range = @sheet.range(KEY_COLS).Find(YAML.dump(key),
@app.ActiveCell,
-4163, #xlValues,
1, #xlWhole,
1, #xlByRows,
1, #xlNext,
true,
false)
range
rescue
nil
end
def key?(key)
find(key) ? true : false
end
def insert(key, value)
@lock.synchronize {
max_row = @sheet.UsedRange.Row + @sheet.UsedRange.Rows.count
@sheet.range("A#{max_row}").value = YAML.dump(key)
@sheet.range("B#{max_row}").value = YAML.dump(value)
}
end
def close(is_save = true)
@book.save if (@book && is_save)
@book.close({'SaveChanges' => is_save}) if @book
@app.Quit if @app
end
end
end