google-spreadsheet-ruby 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/google_spreadsheet.rb +226 -8
- metadata +8 -8
data/lib/google_spreadsheet.rb
CHANGED
@@ -290,7 +290,7 @@ module GoogleSpreadsheet
|
|
290
290
|
def login(mail, password)
|
291
291
|
if !@fetcher.is_a?(ClientLoginFetcher)
|
292
292
|
raise(GoogleSpreadsheet::Error,
|
293
|
-
"Cannot call login for session created by login_with_oauth
|
293
|
+
"Cannot call login for session created by login_with_oauth.")
|
294
294
|
end
|
295
295
|
begin
|
296
296
|
@fetcher.auth_tokens = {
|
@@ -308,7 +308,7 @@ module GoogleSpreadsheet
|
|
308
308
|
if !@fetcher.is_a?(ClientLoginFetcher)
|
309
309
|
raise(GoogleSpreadsheet::Error,
|
310
310
|
"Cannot call auth_tokens for session created by " +
|
311
|
-
"login_with_oauth
|
311
|
+
"login_with_oauth.")
|
312
312
|
end
|
313
313
|
return @fetcher.auth_tokens
|
314
314
|
end
|
@@ -443,6 +443,10 @@ module GoogleSpreadsheet
|
|
443
443
|
return convert_response(response, response_type)
|
444
444
|
end
|
445
445
|
end
|
446
|
+
|
447
|
+
def inspect
|
448
|
+
return '#<%p:0x%x>' % [self.class, self.object_id]
|
449
|
+
end
|
446
450
|
|
447
451
|
private
|
448
452
|
|
@@ -651,6 +655,14 @@ module GoogleSpreadsheet
|
|
651
655
|
end
|
652
656
|
return result.freeze()
|
653
657
|
end
|
658
|
+
|
659
|
+
# Returns a GoogleSpreadsheet::Worksheet with the given title in the spreadsheet.
|
660
|
+
#
|
661
|
+
# Returns nil if not found. Returns the first one when multiple worksheets with the
|
662
|
+
# title are found.
|
663
|
+
def worksheet_by_title(title)
|
664
|
+
return self.worksheets.find(){ |ws| ws.title == title }
|
665
|
+
end
|
654
666
|
|
655
667
|
# Adds a new worksheet to the spreadsheet. Returns added GoogleSpreadsheet::Worksheet.
|
656
668
|
def add_worksheet(title, max_rows = 100, max_cols = 20)
|
@@ -679,6 +691,12 @@ module GoogleSpreadsheet
|
|
679
691
|
doc = @session.request(:get, self.tables_feed_url)
|
680
692
|
return doc.css('entry').map(){ |e| Table.new(@session, e) }.freeze()
|
681
693
|
end
|
694
|
+
|
695
|
+
def inspect
|
696
|
+
fields = {:worksheets_feed_url => self.worksheets_feed_url}
|
697
|
+
fields[:title] = @title if @title
|
698
|
+
return '#<%p %s>' % [self.class, fields.map(){ |k, v| "%s=%p" % [k, v] }.join(", ")]
|
699
|
+
end
|
682
700
|
|
683
701
|
end
|
684
702
|
|
@@ -780,6 +798,7 @@ module GoogleSpreadsheet
|
|
780
798
|
|
781
799
|
end
|
782
800
|
|
801
|
+
# A worksheet (i.e. a tab) in a spreadsheet.
|
783
802
|
# Use GoogleSpreadsheet::Spreadsheet#worksheets to get GoogleSpreadsheet::Worksheet object.
|
784
803
|
class Worksheet
|
785
804
|
|
@@ -794,6 +813,7 @@ module GoogleSpreadsheet
|
|
794
813
|
@cells = nil
|
795
814
|
@input_values = nil
|
796
815
|
@modified = Set.new()
|
816
|
+
@list = nil
|
797
817
|
end
|
798
818
|
|
799
819
|
# URL of cell-based feed of the worksheet.
|
@@ -848,8 +868,9 @@ module GoogleSpreadsheet
|
|
848
868
|
|
849
869
|
# Returns the value or the formula of the cell. Top-left cell is [1, 1].
|
850
870
|
#
|
851
|
-
# If user input "=A1+B1" to cell [1, 3]
|
852
|
-
#
|
871
|
+
# If user input "=A1+B1" to cell [1, 3]:
|
872
|
+
# worksheet[1, 3] #=> "3" for example
|
873
|
+
# worksheet.input_value(1, 3) #=> "=RC[-2]+RC[-1]"
|
853
874
|
def input_value(row, col)
|
854
875
|
reload() if !@cells
|
855
876
|
return @input_values[[row, col]] || ""
|
@@ -858,13 +879,13 @@ module GoogleSpreadsheet
|
|
858
879
|
# Row number of the bottom-most non-empty row.
|
859
880
|
def num_rows
|
860
881
|
reload() if !@cells
|
861
|
-
return @
|
882
|
+
return @input_values.select(){ |(r, c), v| !v.empty? }.map(){ |(r, c), v| r }.max || 0
|
862
883
|
end
|
863
884
|
|
864
885
|
# Column number of the right-most non-empty column.
|
865
886
|
def num_cols
|
866
887
|
reload() if !@cells
|
867
|
-
return @
|
888
|
+
return @input_values.select(){ |(r, c), v| !v.empty? }.map(){ |(r, c), v| c }.max || 0
|
868
889
|
end
|
869
890
|
|
870
891
|
# Number of rows including empty rows.
|
@@ -926,7 +947,7 @@ module GoogleSpreadsheet
|
|
926
947
|
end
|
927
948
|
|
928
949
|
# Reloads content of the worksheets from the server.
|
929
|
-
# Note that changes you made by []= is discarded if you haven't called save().
|
950
|
+
# Note that changes you made by []= etc. is discarded if you haven't called save().
|
930
951
|
def reload()
|
931
952
|
doc = @session.request(:get, @cells_feed_url)
|
932
953
|
@max_rows = doc.css('gs|rowCount').text.to_i
|
@@ -941,7 +962,6 @@ module GoogleSpreadsheet
|
|
941
962
|
col = cell["col"].to_i()
|
942
963
|
@cells[[row, col]] = cell.inner_text
|
943
964
|
@input_values[[row, col]] = cell["inputValue"]
|
944
|
-
|
945
965
|
end
|
946
966
|
@modified.clear()
|
947
967
|
@meta_modified = false
|
@@ -1110,8 +1130,206 @@ module GoogleSpreadsheet
|
|
1110
1130
|
return entry.css(
|
1111
1131
|
"link[@rel='http://schemas.google.com/spreadsheets/2006#listfeed']").first['href']
|
1112
1132
|
end
|
1133
|
+
|
1134
|
+
# Provides access to cells using column names, assuming the first row contains column
|
1135
|
+
# names. Returned object is GoogleSpreadsheet::List which you can use mostly as
|
1136
|
+
# Array of Hash.
|
1137
|
+
#
|
1138
|
+
# e.g. Assuming the first row is ["x", "y"]:
|
1139
|
+
# worksheet.list[0]["x"] #=> "1" # i.e. worksheet[2, 1]
|
1140
|
+
# worksheet.list[0]["y"] #=> "2" # i.e. worksheet[2, 2]
|
1141
|
+
# worksheet.list[1]["x"] = "3" # i.e. worksheet[3, 1] = "3"
|
1142
|
+
# worksheet.list[1]["y"] = "4" # i.e. worksheet[3, 2] = "4"
|
1143
|
+
# worksheet.list.push({"x" => "5", "y" => "6"})
|
1144
|
+
#
|
1145
|
+
# Note that update is not sent to the server until you call save().
|
1146
|
+
def list
|
1147
|
+
return @list ||= List.new(self)
|
1148
|
+
end
|
1149
|
+
|
1150
|
+
def inspect
|
1151
|
+
fields = {:worksheet_feed_url => self.worksheet_feed_url}
|
1152
|
+
fields[:title] = @title if @title
|
1153
|
+
return '#<%p %s>' % [self.class, fields.map(){ |k, v| "%s=%p" % [k, v] }.join(", ")]
|
1154
|
+
end
|
1113
1155
|
|
1114
1156
|
end
|
1115
1157
|
|
1158
|
+
|
1159
|
+
# Provides access to cells using column names.
|
1160
|
+
# Use GoogleSpreadsheet::Worksheet#list to get GoogleSpreadsheet::List object.
|
1161
|
+
#--
|
1162
|
+
# This is implemented as wrapper of GoogleSpreadsheet::Worksheet i.e. using cells
|
1163
|
+
# feed, not list feed. In this way, we can easily provide consistent API as
|
1164
|
+
# GoogleSpreadsheet::Worksheet using save()/reload().
|
1165
|
+
class List
|
1166
|
+
|
1167
|
+
include(Enumerable)
|
1168
|
+
|
1169
|
+
def initialize(worksheet) #:nodoc:
|
1170
|
+
@worksheet = worksheet
|
1171
|
+
end
|
1172
|
+
|
1173
|
+
# Number of non-empty rows in the worksheet excluding the first row.
|
1174
|
+
def size
|
1175
|
+
return @worksheet.num_rows - 1
|
1176
|
+
end
|
1177
|
+
|
1178
|
+
# Returns Hash-like object (GoogleSpreadsheet::ListRow) for the row with the
|
1179
|
+
# index. Keys of the object are colum names (the first row).
|
1180
|
+
# The second row has index 0.
|
1181
|
+
#
|
1182
|
+
# Note that updates to the returned object are not sent to the server until
|
1183
|
+
# you call GoogleSpreadsheet::Worksheet#save().
|
1184
|
+
def [](index)
|
1185
|
+
return ListRow.new(self, index)
|
1186
|
+
end
|
1187
|
+
|
1188
|
+
# Updates the row with the index with the given Hash object.
|
1189
|
+
# Keys of +hash+ are colum names (the first row).
|
1190
|
+
# The second row has index 0.
|
1191
|
+
#
|
1192
|
+
# Note that update is not sent to the server until
|
1193
|
+
# you call GoogleSpreadsheet::Worksheet#save().
|
1194
|
+
def []=(index, hash)
|
1195
|
+
self[index].replace(hash)
|
1196
|
+
end
|
1197
|
+
|
1198
|
+
# Iterates over Hash-like object (GoogleSpreadsheet::ListRow) for each row
|
1199
|
+
# (except for the first row).
|
1200
|
+
# Keys of the object are colum names (the first row).
|
1201
|
+
def each(&block)
|
1202
|
+
for i in 0...self.size
|
1203
|
+
yield(self[i])
|
1204
|
+
end
|
1205
|
+
end
|
1206
|
+
|
1207
|
+
# Column names i.e. the contents of the first row.
|
1208
|
+
# Duplicates are removed.
|
1209
|
+
def keys
|
1210
|
+
return (1..@worksheet.num_cols).map(){ |i| @worksheet[1, i] }.uniq()
|
1211
|
+
end
|
1212
|
+
|
1213
|
+
# Updates column names i.e. the contents of the first row.
|
1214
|
+
#
|
1215
|
+
# Note that update is not sent to the server until
|
1216
|
+
# you call GoogleSpreadsheet::Worksheet#save().
|
1217
|
+
def keys=(ary)
|
1218
|
+
for i in 1..ary.size
|
1219
|
+
@worksheet[1, i] = ary[i - 1]
|
1220
|
+
end
|
1221
|
+
for i in (ary.size + 1)..@worksheet.num_cols
|
1222
|
+
@worksheet[1, i] = ""
|
1223
|
+
end
|
1224
|
+
end
|
1225
|
+
|
1226
|
+
# Adds a new row to the bottom.
|
1227
|
+
# Keys of +hash+ are colum names (the first row).
|
1228
|
+
# Returns GoogleSpreadsheet::ListRow for the new row.
|
1229
|
+
#
|
1230
|
+
# Note that update is not sent to the server until
|
1231
|
+
# you call GoogleSpreadsheet::Worksheet#save().
|
1232
|
+
def push(hash)
|
1233
|
+
row = self[self.size]
|
1234
|
+
row.update(hash)
|
1235
|
+
return row
|
1236
|
+
end
|
1237
|
+
|
1238
|
+
# Returns all rows (except for the first row) as Array of Hash.
|
1239
|
+
# Keys of Hash objects are colum names (the first row).
|
1240
|
+
def to_hash_array()
|
1241
|
+
return self.map(){ |r| r.to_hash() }
|
1242
|
+
end
|
1243
|
+
|
1244
|
+
def get(index, key) #:nodoc:
|
1245
|
+
return @worksheet[index + 2, key_to_col(key)]
|
1246
|
+
end
|
1247
|
+
|
1248
|
+
def set(index, key, value) #:nodoc:
|
1249
|
+
@worksheet[index + 2, key_to_col(key)] = value
|
1250
|
+
end
|
1251
|
+
|
1252
|
+
private
|
1253
|
+
|
1254
|
+
def key_to_col(key)
|
1255
|
+
key = key.to_s()
|
1256
|
+
col = (1..@worksheet.num_cols).find(){ |c| @worksheet[1, c] == key }
|
1257
|
+
raise(GoogleSpreadsheet::Error, "colunm doesn't exist: %p" % key) if !col
|
1258
|
+
return col
|
1259
|
+
end
|
1260
|
+
|
1261
|
+
end
|
1116
1262
|
|
1263
|
+
# Hash-like object returned by GoogleSpreadsheet::List#[].
|
1264
|
+
class ListRow
|
1265
|
+
|
1266
|
+
include(Enumerable)
|
1267
|
+
extend(Forwardable)
|
1268
|
+
|
1269
|
+
def_delegators(:to_hash,
|
1270
|
+
:keys, :values, :each_key, :each_value, :each, :each_pair, :hash,
|
1271
|
+
:assoc, :fetch, :flatten, :key, :invert, :size, :length, :rassoc,
|
1272
|
+
:merge, :reject, :select, :sort, :to_a, :values_at)
|
1273
|
+
|
1274
|
+
def initialize(list, index) #:nodoc:
|
1275
|
+
@list = list
|
1276
|
+
@index = index
|
1277
|
+
end
|
1278
|
+
|
1279
|
+
def [](key)
|
1280
|
+
return @list.get(@index, key)
|
1281
|
+
end
|
1282
|
+
|
1283
|
+
def []=(key, value)
|
1284
|
+
@list.set(@index, key, value)
|
1285
|
+
end
|
1286
|
+
|
1287
|
+
def has_key?(key)
|
1288
|
+
return @list.keys.include?(key)
|
1289
|
+
end
|
1290
|
+
|
1291
|
+
alias include? has_key?
|
1292
|
+
alias key? has_key?
|
1293
|
+
alias member? has_key?
|
1294
|
+
|
1295
|
+
def update(hash)
|
1296
|
+
for k, v in hash
|
1297
|
+
self[k] = v
|
1298
|
+
end
|
1299
|
+
end
|
1300
|
+
|
1301
|
+
alias merge! update
|
1302
|
+
|
1303
|
+
def replace(hash)
|
1304
|
+
clear()
|
1305
|
+
update(hash)
|
1306
|
+
end
|
1307
|
+
|
1308
|
+
def clear()
|
1309
|
+
for key in @list.keys
|
1310
|
+
self[key] = ""
|
1311
|
+
end
|
1312
|
+
end
|
1313
|
+
|
1314
|
+
def to_hash()
|
1315
|
+
result = {}
|
1316
|
+
for key in @list.keys
|
1317
|
+
result[key] = self[key]
|
1318
|
+
end
|
1319
|
+
return result
|
1320
|
+
end
|
1321
|
+
|
1322
|
+
def ==(other)
|
1323
|
+
return self.class == other.class && self.to_hash() == other.to_hash()
|
1324
|
+
end
|
1325
|
+
|
1326
|
+
alias === ==
|
1327
|
+
alias eql? ==
|
1328
|
+
|
1329
|
+
def inspect
|
1330
|
+
return "\#<%p %p>" % [self.class, to_hash()]
|
1331
|
+
end
|
1332
|
+
|
1333
|
+
end
|
1334
|
+
|
1117
1335
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-spreadsheet-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-02-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
16
|
-
requirement: &
|
16
|
+
requirement: &75716330 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.4.3.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *75716330
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: oauth
|
27
|
-
requirement: &
|
27
|
+
requirement: &75715910 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 0.3.6
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *75715910
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: oauth2
|
38
|
-
requirement: &
|
38
|
+
requirement: &75715490 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 0.5.0
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *75715490
|
47
47
|
description: This is a library to read/write Google Spreadsheet.
|
48
48
|
email:
|
49
49
|
- gimite+github@gmail.com
|