cxn03651 / writeexcel Goto Github PK
View Code? Open in Web Editor NEWruby gem for Write to a cross-platform Excel binary file.
Home Page: http://wiki.github.com/cxn03651/writeexcel/
License: MIT License
ruby gem for Write to a cross-platform Excel binary file.
Home Page: http://wiki.github.com/cxn03651/writeexcel/
License: MIT License
When using writeexcel to create an Excel file with VLOOKUP formulas, MS Excel displays them as "#VALUE" and I've to edit (no changes) the cell and press Enter to see the actual intended value.
On looking around, this issue seems to have been there in the Perl module as well. http://stackoverflow.com/questions/2229844/how-do-i-get-perls-spreadsheetwriteexcel-to-create-formulas-with-vlookup
What is the Ruby workaround for this?
Thanks.
lib/writeexcel/shared_string_table.rb:1:in `<top (required)>': Workbook is not a class (TypeError)
Does the gem support opening existing spreadsheets?
in 0.6.11 the store_formula method gives the error:
testExcel.rb:21:in <main>': private method
store_formula' called for #Writeexcel::Worksheet:0x13a1840 (NoMethodError)
reverting back to 0.6.9 fixed the issue.
I've got a NoMethodError in controller:
undefined method `write' for #<Pathname:0x00000005ad3bf8>
Action:
workbook = WriteExcel.new(path)
xls = workbook.add_worksheet
row = 1
orders.each do |c|
xls.write(row, 0, c.id.to_s)
xls.write(row, 1, c.code.to_s)
row += 1
end
workbook.close
Using "D1" or any uppercase letter followed by a number together with autofilter generates an exception. Lowercase initial letter is instead ok. Try this simple test code:
require 'rubygems'
require 'writeexcel'
workbook = WriteExcel.new(open('test.xls', 'w'))
worksheet = workbook.add_worksheet('D1')
worksheet.autofilter(0, 0, 0, 10)
workbook.close
I get:
Racc::ParseError:
parse error on value "!" (error)
from /home/drsound/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/racc/parser.rb:350:in on_error' from /home/drsound/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/racc/parser.rb:99:in
_racc_do_parse_c'
from /home/drsound/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/racc/parser.rb:99:in __send__' from /home/drsound/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/racc/parser.rb:99:in
do_parse'
from /home/drsound/.rvm/gems/ruby-1.8.7-p352/gems/writeexcel-0.6.8/lib/writeexcel/formula.rb:206:inparse' from /home/drsound/.rvm/gems/ruby-1.8.7-p352/gems/writeexcel-0.6.8/lib/writeexcel/formula.rb:40:in
parse_formula'
from /home/drsound/.rvm/gems/ruby-1.8.7-p352/gems/writeexcel-0.6.8/lib/writeexcel/worksheet.rb:2952:instore_formula' from /home/drsound/.rvm/gems/ruby-1.8.7-p352/gems/writeexcel-0.6.8/lib/writeexcel/worksheet.rb:7237:in
store_filters'
from /home/drsound/.rvm/gems/ruby-1.8.7-p352/gems/writeexcel-0.6.8/lib/writeexcel/worksheet.rb:264:inclose' from /home/drsound/.rvm/gems/ruby-1.8.7-p352/gems/writeexcel-0.6.8/lib/writeexcel/workbook.rb:1284:in
calc_sheet_offsets'
from /home/drsound/.rvm/gems/ruby-1.8.7-p352/gems/writeexcel-0.6.8/lib/writeexcel/workbook.rb:1282:ineach' from /home/drsound/.rvm/gems/ruby-1.8.7-p352/gems/writeexcel-0.6.8/lib/writeexcel/workbook.rb:1282:in
calc_sheet_offsets'
from /home/drsound/.rvm/gems/ruby-1.8.7-p352/gems/writeexcel-0.6.8/lib/writeexcel/workbook.rb:1143:instore_workbook' from /home/drsound/.rvm/gems/ruby-1.8.7-p352/gems/writeexcel-0.6.8/lib/writeexcel/workbook.rb:152:in
close'
from (irb):136
parse error on value ":" (error)
from /usr/local/lib/ruby/2.1.0/racc/parser.rb:529:in on_error' from /usr/local/lib/ruby/2.1.0/racc/parser.rb:258:in
_racc_do_parse_c'
from /usr/local/lib/ruby/2.1.0/racc/parser.rb:258:in `do_parse'
on a line of code like:
worksheet.write(row_index, col_index, col_value.to_s)
where col_value is a String similar to
word : word : word\nword : ....
Hello,
I came across a wierd problem using freeze_panes. I've observed that it does not get applied for third and later worksheets. simply:
wb = WriteExcel.new("test_freeze_panes.xls")
10.times{|i| ws = wb.add_worksheet("Pane #{i}"); ws.freeze_panes(2,0); }
wb.close
..and panes are freezed only in two first worksheets.
using :
-writeexcel both 1.0.0, 1.0.1
-ruby 1.9.3p385 (2013-02-06 revision 39114) [x86_64-linux]
-libreoffice for view the .xls
Could you make it work?
I made column chart , it's rendering perfect and work fine as i need. Now i want to put data label on chart so i can easily recognize value on column chart but i can't find any method for this. Can any one help me on this.
Good day, i assume that writing some different data to the same cells will produce
'data may have been lost'
while opening xls file.
Original perl module suggest to use compatibility_module which will use xls compatibility mode.
Never helps, anyway.
My question mostly about logic. If i need some area used as a square with borders, only way to achieve it to write format to all cells which are perimeter of that block. When it is done - all is good, file opens as expected, but when i try e.g. merge some cells within block it will produce above warning.
What i'm doing wrong ?
This gem defines commonly used class names like Format, Chart, Colors, etc. In an application of any significant size, including this gem will cause conflicts with existing code. Any way these could be namespaced better?
This line will cause incompatible character encodings error if '#{name}' is #Encoding:UTF-16BE.
https://github.com/cxn03651/writeexcel/blob/master/lib/writeexcel/workbook.rb#L983
I change the encoding to solve the problem.
raise "Worksheet name '#{name.encode('UTF-8')}', with case ignored, is already in use"
Please see this gist https://gist.github.com/22b247538083968a24c8
So french text gets displayed as Chinese in xls file generated.
Halp.
This is very much an edge case. However I have some Perl that did this using insert_bitmap before insert_image was added to the Ruby version. rebuilding now in ruby I get either a hang or no additional images in the cell.
here is some example code
x = 9
y = 5
worksheet1.set_row(0, 200)
worksheet1.insert_image('A1', File.join(File.dirname(File.expand_path(FILE)), 'conductor_watercourse.png'), x, y)
worksheet1.write('B1', "1")
worksheet1.insert_image('A1', File.join(File.dirname(File.expand_path(FILE)), 'conductor_watercourse.png'), x, 25)
worksheet1.write('B1', "2")
worksheet1.insert_image('A1', File.join(File.dirname(File.expand_path(FILE)), 'conductor_watercourse.png'), x, 45)
worksheet1.write('B1', "3")
in Perl i simply added to the y axis before writing the image
Any ideas where I am going wrong.
Cheers
Steven
I'm not sure if this is currently implemented and not documented, or if it's not currently implemented, but I'd like to be able to change the name of sheets. I have a script to generate a report of EBS volumes against a given AWS account, with different sheets for each supported AWS region. I'd like to be able to rename Sheet 1 to us-east-1, Sheet 2 to us-west-2, Sheet 3 to eu-west-1, etc.
I had a problem where a user submitted multiple =
in a text field and exporting the data failed with
parse error on value "=" ("=")
/home/ubuntu/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/racc/parser.rb:351:in `on_error'
(eval):3:in `_racc_do_parse_c'
(eval):3:in `do_parse'
Is there any way to treat data always as string and skipping the formula parsing?
Hi,
I'm getting this error from Excel 2010 on simple xls file with 10000 rows and 20 columns without formatting or formulas. It is rather hard to get which row or cell leads to such error. Because after removing 1000 rows excel opens file without issues(with 90000 rows) and file with those 1000 rows excel able to open as well. Do you have any idea how to fix this issue?
ruby 1.9.3p545
magic utf8 comment were added to each source file
i'm using just write_row and put force_encoding("UTF-8") to any row field
Thanks.
In Numbers, it looks like only the first hyperlink of the document will work. In Google Spreadsheet, no hyperlinks work. And it's fine in Excel.
I tried using the HYPERLINK
formula instead but it doesn't look like it's supported by this gem.
I am not familiar with the Excel binary format but links are working with all platform using the Spreadsheet gem so I guess it's fixable.
This issue was raised on Stackoverflow.
The problem is in the store_num_format() method in the workbook.rb class. After converting the string to utf16 the character count should be reset:
...
# Handle utf8 strings
if is_utf8?(format)
format = utf8_to_16be(format)
encoding = 1
cch = format.bytesize # Add this.
end
...
I'm trying to create an excel file with a lot of comments and I've noticed that, when I put more than one note per row, only the last one is created.
require 'rubygems'
require 'writeexcel'
workbook = WriteExcel.new("comments0.xls")
worksheet = workbook.add_worksheet
worksheet.write(0, 0, 'Hello a1')
worksheet.write(0, 1, 'Hello b1')
worksheet.write(1, 0, 'Hello a2')
worksheet.write(1, 1, 'Hello b2')
worksheet.write_comment('A1', 'This is a comment a1', :author=>'arr')
worksheet.write_comment('A2', 'This is a comment a2', :author=>'arr')
worksheet.write_comment('B1', 'This is a comment b1', :author=>'arr')
worksheet.write_comment('B2', 'This is a comment b2', :author=>'arr')
workbook.close
As you can see I try to create one comment for each cell, but the resulting excel has only 2 comments on the last columns. (https://skitch.com/david.saitta/gkbbq/microsoft-excel)
It seems that the library consider only the last comment for row.
Thanks for your work!!!
If you have multiple merge_range functions in your code and you try to set the height of ANY row using set_row only the first merge_range works properly. All others merge the needed cells but do not write text in them.
Example code to reproduce bug:
require 'rubygems'
require 'writeexcel'
workbook = WriteExcel.new('test.xls')
worksheet = workbook.add_worksheet('worksheet')
heading = workbook.add_format(
:align => 'center',
:size => 12,
:bold => 1
)
subHeading = workbook.add_format(
:align => 'center',
:size => 12
)
worksheet.merge_range('A2:H2', 'first row', heading)
worksheet.merge_range('A3:H3', 'Second row', subHeading) # text isn't visible. if you comment out set_row it will be visible again
worksheet.set_row(3, 30)
workbook.close()
Weird bug, seems like the gem works just fine in 1.9, until you set a property (like company or whatnot) .. this somehow causes the whole file to get corrupted and unreadable.
Sorry I can't really give you any real useful tests here .. all the hex stuff in your test suite is basically useless unless you intimately understand the XLS format itself ...
My basic repro for this is ..
Now try to open the generated file (I've used the roo gem to attempt to open it as my unit test for this, if it crashes .. its corrupt, if it does not its ok)
On 1.8.7 its ok, on 1.9.3 it is not ..
I want to write 1537 rows into file and get this error:
parse error on value "=" ("=")
racc/parser.rb:350:in `on_error'
Backtrace
racc/parser.rb:350:in on_error' racc/parser.rb:99:in
_racc_do_parse_c'
racc/parser.rb:99:in __send__' racc/parser.rb:99:in
do_parse'
[RAILS_ROOT]/vendor/plugins/writeexcel/lib/writeexcel/formula.rb:206:inparse' [RAILS_ROOT]/vendor/plugins/writeexcel/lib/writeexcel/formula.rb:40:in
parse_formula'
[RAILS_ROOT]/vendor/plugins/writeexcel/lib/writeexcel/worksheet.rb:2844:inwrite_formula' [RAILS_ROOT]/vendor/plugins/writeexcel/lib/writeexcel/worksheet.rb:2247:in
write'
[RAILS_ROOT]/lib/to_xls.rb:22:increate' [RAILS_ROOT]/vendor/plugins/writeexcel/lib/writeexcel/format.rb:1278:in
each_with_index'
[RAILS_ROOT]/lib/to_xls.rb:21:ineach' [RAILS_ROOT]/lib/to_xls.rb:21:in
each_with_index'
[RAILS_ROOT]/lib/to_xls.rb:21:increate' [RAILS_ROOT]/vendor/plugins/writeexcel/lib/writeexcel/format.rb:1278:in
each_with_index'
[RAILS_ROOT]/lib/to_xls.rb:20:ineach' [RAILS_ROOT]/lib/to_xls.rb:20:in
each_with_index'
[RAILS_ROOT]/lib/to_xls.rb:20:increate' [RAILS_ROOT]/app/models/questionnare/questionnare.rb:59:in
to_xls'
What does it meen? Maybe I want to write some data that cannot parse?
How can I fix this?
Tnx.
I'm trying to create a data_validation list from an array with 10 elements.
I'm getting this error :
TypeError - can't convert String into Integer:
writeexcel (1.0.4) lib/writeexcel/formula.rb:302:in `convert_string'
writeexcel (1.0.4) lib/writeexcel/formula.rb:101:in `parse_tokens'
writeexcel (1.0.4) lib/writeexcel/formula.rb:52:in `parse_formula'
writeexcel (1.0.4) lib/writeexcel/data_validations.rb:278:in `pack_dv_formula'
writeexcel (1.0.4) lib/writeexcel/data_validations.rb:120:in `dv_record'
...
This test throught the error at line 302 of formula.rb :
exit "String in formula has more than 255 chars\n" if length > 255
Concatenation of my 10 array elements gave a string of more than 255 length chars.
i'm trying to make a list data validation using this:
worksheet.data_validation(row, 0, row+50, 0,
{
:validate => 'list',
:source =>["a>b", "a<b", "a?b", "a*b", "?><", "'C", ""D""]
})
where one of the string is enclose with " and it gives me this error:
Racc::ParseError in MyController#write_to_excel
parse error on value "D" (error)
In version 0.6.5 of the writeexcel gem, I'm getting the following warnings:
lib/writeexcel/workbook.rb:35: warning: private attribute?
lib/writeexcel/workbook.rb:36: warning: private attribute?
Is this intentional?
There are changes in the repository to support ruby 3.2 but there is no release.
Please, deploy a new release with these fixes.
Hi!
Adding writeexcel gem dependence brakes up .to_json method.
To reproduce:
It results a error:
ArgumentError: wrong number of arguments (2 for 1)
from /usr/lib/ruby/gems/1.8/testadmin/ruby/1.8/gems/activesupport-3.0.1/lib/active_support/json/encoding.rb:106:in encode' from /usr/lib/ruby/gems/1.8/testadmin/ruby/1.8/gems/activesupport-3.0.1/lib/active_support/json/encoding.rb:106:in
escape'
from /usr/lib/ruby/gems/1.8/testadmin/ruby/1.8/gems/activesupport-3.0.1/lib/active_support/json/encoding.rb:51:in `escape'
...
.to_json works correctly when 'writeexcel' dependence is removed.
writeexcel/formula.rb:515:in `get_name_index': Unknown defined name PI in formula (RuntimeError)
This happens when I try to do things like:
worksheet.write('A4', '=SIN(PI/4)')
Take the following example:
require 'writeexcel'
io = File.open('gridlines-worksheets.xls', 'w')
book = WriteExcel.new(io)
s1 = book.add_worksheet('Foo')
s2 = book.add_worksheet('Bar')
s3 = book.add_worksheet('Baz')
s1.hide_gridlines(2)
s2.hide_gridlines(2)
s3.hide_gridlines(2)
book.close
The gridlines are only hidden in the first two worksheets. They remain visible on the third worksheet, despite the call to hide_gridlines
. Result is similar when more worksheets are present: only the first two hide their gridlines.
In workbook.rb the store_externsheet method sorts sheet names by string, therefore sheet references mix up if more than 10 sheets generated
HI,
in one of the our projects we use Writeexcel and I think is great.
One problem what we found so far is encoding issues with some of the characters
« » ² ³ µ ¶
We think because they belongs to totally different characters range..Please take a look:
https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts#Superscripts_and_subscripts_block
When we generate excel file, the characters either missing or we got ??????...
In all our files we have ruby encoding flag:
# coding: UTF-8
Any help or idea how we can fix this it would be great...
Regards,
Aleksandar
Hi!
It seems that writeexcel beaks e-mail sending in Rails 3 (tested in 3.0.1, ruby 1.8.7). Header (from, to) fields are forced to use UTF8 encoding that results error on sending (unknown sender/receiver domain/address).
Without this gem ActionMailer gives something like
mail = MyMailer.notice
=> #<Mail::Message:-618977088, Multipart: true, Headers: <From: [email protected]>, <To: [email protected]>, <Subject: Notice>, <Mime-Version: 1.0>,
<Content-Type: multipart/alternative; boundary="--==_mimepart_4cf6e4dde3fc0_2899241d3726737ce"; charset=UTF-8>>
mail.from
=> ["[email protected]"]
mail.to
=> ["[email protected]"]
When writeexcel is enabled then
mail = MyMailer.notice
=> #<Mail::Message:-620247058, Multipart: true, Headers: <From: [email protected]>, <To: [email protected]>, <Subject: Notice>, <Mime-Version: 1.0>,
<Content-Type: multipart/alternative; boundary=utf8'en'--==_mimepart_4cf6e20aace22_27cc241c8722624c2; charset=utf8'en'UTF-8>>
mail.to
=> ["=?UTF-8?B?IHRlc3RAdGVzdC5lZQ==?="]
mail.from
=> ["=?UTF-8?B?IHRlc3RAdGVzdC5lZQ==?="]
Notice changes in :from and :to outputs and :charset.
The last one causes error on sending (Gmail example)
mail.deliver
=> Net::SMTPFatalError: 553-5.1.2 We weren't able to find the recipient domain. Please check for any
from /usr/lib/ruby/1.8/net/smtp.rb:930:in `check_response'
Both example should give identical results, but in the second one, the sheets references are swapped...
require 'rubygems'
require 'writeexcel'
# Working example
workbook = WriteExcel.new('test-working.xls')
worksheet = workbook.add_worksheet("First")
worksheet.write(0, 0, 1)
worksheet = workbook.add_worksheet("Second")
worksheet.write(0, 0, 2)
worksheet = workbook.add_worksheet("Third")
worksheet.write(0, 0, "First :")
worksheet.write_formula(0, 1, %q{='First'!A1})
worksheet.write(1, 0, "Second :")
worksheet.write_formula(1, 1, %q{='Second'!A1})
workbook.close
# Failing example
workbook = WriteExcel.new('test-failing.xls')
worksheet = workbook.add_worksheet("Second")
worksheet.write(0, 0, 2)
worksheet = workbook.add_worksheet("First")
worksheet.write(0, 0, 1)
worksheet = workbook.add_worksheet("Third")
worksheet.write(0, 0, "First :")
worksheet.write_formula(0, 1, %q{='First'!A1})
worksheet.write(1, 0, "Second :")
worksheet.write_formula(1, 1, %q{='Second'!A1})
workbook.close
This is a bug in ascii_to_16be function, ascii.unpack("C_").pack("n_") won't work.
Change the code to ascii = ascii.unpack("C_").pack("n_") should be ok.
https://github.com/cxn03651/writeexcel/blob/master/lib/writeexcel/helper.rb
def ascii_to_16be(ascii)
ascii.unpack("C_").pack("n_")
ruby_19 { ascii.force_encoding('UTF-16BE') }
ascii
end
error027240_01.xml
"*** ERROR: exception Couldn't locate http://s3.amazonaws.com/****alamy.jpg?1426000192: $!"
Code:
@worksheet.insert_image("A#{row_name}",usage.asset.image(:small))
#usage.asset.image(:small) - s3 url in string class
I was patching a fork of the roo gem to fix several xls parsing error and I ran into this weird state, where the characters are encoded in a different format than expected when loading files generated by this gem. I traced it down to write_string
and found this nugget:
# Handle utf8 strings
if is_utf8?(str)
str_utf16le = utf8_to_16le(str)
return write_utf16le_string(row, col, str_utf16le, args[3])
end
This causes some cells to be arbitrarily encoded in UTF-16LE, which gets loaded as UTF-8 by the spreadsheet gem. I noticed the same conversion, but to UTF-16BE in chart axis saving. --don't mix the streams--
Then I went to read the write_utf16le_string
method to see if it was doing something odd. I did not expect it to convert the string again, but here it calls utf16be_to_16le
on an already encoded utf-16le string.
def write_utf16le_string(*args)
# Check for a cell reference in A1 notation and substitute row and column
args = row_col_notation(args)
return -1 if (args.size < 3) # Check the number of args
row, col, str, format = args
# Change from UTF16 big-endian to little endian
str = utf16be_to_16le(str)
write_utf16be_string(row, col, str, format)
end
Which in turn doesn't even use encoding but instead unpacks and repacks the string.
def utf16be_to_16le(utf16be)
utf16be.unpack('n*').pack('v*')
end
And then the write_utf16be_string
at the end of write_utf16le_string
itself calls utf16be_to_16le
to convert it back to 16le. At this point I starting losing track of be's and le's in trying to figure out what the actual encoding becomes.
def utf16be_to_16le(...)
...
# Change from UTF16 big-endian to little endian
str = utf16be_to_16le(str)
WHY does it do this??? Is it a mistake, or something to appease an older version of excel? There's 3 levels of arbitrary re-encoding for utf-8 without clarifying comments and it clearly produces an output that other readers can't correctly interpret and which differs from excel 2010's xls outputs.
Excel generated on ubuntu is ok but on windows it is showing data may have lost so i used compatibility mode now it is showing blank cells but on click on blank cell showing data on formula bar
This is what I get when using this gem on Ruby 3.3.0:
/Users/rodrigo.rosas/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/writeexcel-1.0.5/lib/writeexcel/workbook.rb:14: warning: nkf was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.4.0. Add nkf to your Gemfile or gemspec. Also contact author of writeexcel-1.0.5 to add nkf into its gemspec.
Thanks in advance.
Hi.
workbook = WriteExcel.new('ruby.xls') # Step 1
worksheet = workbook.add_worksheet # Step 2
worksheet.write('A1', 'Hi Excel!') # Step 3
workbook.close
I believe the .close could be eliminated, just as
it is possible in
File.open() {
}
Simply use a block style, like so:
WriteExcel.new('ruby.xls') { # Step 1
worksheet = add_worksheet # Step 2
worksheet.write('A1', 'Hi Excel!') # Step 3
}
Lots of exceptions are generated using writeexcel 0.6.0 with rails 2.3.8. Here is a test case for rails 2.3.8:
append
/usr/lib/ruby/gems/1.8/gems/writeexcel-0.6.0/lib/writeexcel/worksheet.rb:2756 in store_with_compatibility
/usr/lib/ruby/gems/1.8/gems/writeexcel-0.6.0/lib/writeexcel/worksheet.rb:2747 in write_string
/usr/lib/ruby/gems/1.8/gems/writeexcel-0.6.0/lib/writeexcel/worksheet.rb:2584 in write
/home/drsound/scambio/test/writeexcel_test2/(irb):4 in irb_binding
/usr/lib/ruby/1.8/irb/workspace.rb:52 in irb_binding
FD 00 0A 00 00 00 00 00 0F 00 00 00 00 00
=> 0
--- EXCEPTION END ---
No problems at all if I use the same instructions within rails 3.0.1 or a plain ruby program.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.