Skip to content

Commit

Permalink
Bumped gem version and limited save batches to 500 cells.
Browse files Browse the repository at this point in the history
  • Loading branch information
nalanj committed Sep 5, 2009
1 parent 59bbc85 commit d777a02
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
Binary file added google-spreadsheet-ruby-0.0.5.gem
Binary file not shown.
2 changes: 1 addition & 1 deletion google-spreadsheet-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = %q{google-spreadsheet-ruby}
s.version = "0.0.4"
s.version = "0.0.5"
s.authors = ["Hiroshi Ichikawa"]
s.date = %q{2009-07-13}
s.description = %q{This is a library to read/write Google Spreadsheet.}
Expand Down
79 changes: 46 additions & 33 deletions lib/google_spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,42 +481,55 @@ def save()
end

# Updates cell values using batch operation.
xml = <<-"EOS"
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:batch="http://schemas.google.com/gdata/batch"
xmlns:gs="http://schemas.google.com/spreadsheets/2006">
<id>#{h(@cells_feed_url)}</id>
EOS
for row, col in @modified
value = @cells[[row, col]]
entry = cell_entries[[row, col]]
id = entry.search("id").text
edit_url = entry.search("link[@rel='edit']")[0]["href"]
cells = @modified.size
current_cell = 0

while current_cell < cells
batch_count = 0
xml = <<-"EOS"
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:batch="http://schemas.google.com/gdata/batch"
xmlns:gs="http://schemas.google.com/spreadsheets/2006">
<id>#{h(@cells_feed_url)}</id>
EOS

while batch_count <= 500
row,col = @modified[current_cell]
value = @cells[[row, col]]
entry = cell_entries[[row, col]]
id = entry.search("id").text
edit_url = entry.search("link[@rel='edit']")[0]["href"]
xml << <<-"EOS"
<entry>
<batch:id>#{h(row)},#{h(col)}</batch:id>
<batch:operation type="update"/>
<id>#{h(id)}</id>
<link rel="edit" type="application/atom+xml"
href="#{h(edit_url)}"/>
<gs:cell row="#{h(row)}" col="#{h(col)}" inputValue="#{h(value)}"/>
</entry>
EOS

# close each batch out at 500 cells
current_cell++
batch_count++
end

xml << <<-"EOS"
<entry>
<batch:id>#{h(row)},#{h(col)}</batch:id>
<batch:operation type="update"/>
<id>#{h(id)}</id>
<link rel="edit" type="application/atom+xml"
href="#{h(edit_url)}"/>
<gs:cell row="#{h(row)}" col="#{h(col)}" inputValue="#{h(value)}"/>
</entry>
</feed>
EOS
end
xml << <<-"EOS"
</feed>
EOS

result = @session.post("#{@cells_feed_url}/batch", xml)
for entry in result.search("atom:entry")
interrupted = entry.search("batch:interrupted")[0]
if interrupted
raise(GoogleSpreadsheet::Error, "Update has failed: %s" %
interrupted["reason"])
end
if !(entry.search("batch:status")[0]["code"] =~ /^2/)
raise(GoogleSpreadsheet::Error, "Updating cell %s has failed: %s" %
[entry.search("atom:id").text, entry.search("batch:status")[0]["reason"]])
result = @session.post("#{@cells_feed_url}/batch", xml)
for entry in result.search("atom:entry")
interrupted = entry.search("batch:interrupted")[0]
if interrupted
raise(GoogleSpreadsheet::Error, "Update has failed: %s" %
interrupted["reason"])
end
if !(entry.search("batch:status")[0]["code"] =~ /^2/)
raise(GoogleSpreadsheet::Error, "Updating cell %s has failed: %s" %
[entry.search("atom:id").text, entry.search("batch:status")[0]["reason"]])
end
end
end

Expand Down

0 comments on commit d777a02

Please sign in to comment.