weshatheleopard / rubyXL

Ruby lib for reading/writing/modifying .xlsx and .xlsm files
MIT License
1.28k stars 255 forks source link

Certain illegal characters in worksheet names aren't caught #231

Closed moonhouse closed 8 years ago

moonhouse commented 8 years ago

I had a problem last year with Excel (for Mac 201) having to repair my workbook and then having a worksheet with the name along the lines Repaired sheet ("Återskapad_Blad1" in Swedish). When it happened to me again now I realized that it was a colon (:) that caused it to be broken. Apparently there are (at least) seven different characters that are disallowed in worksheet names and that Excel won't let you enter if you do it manually.

require 'rubyXL'

workbook = RubyXL::Workbook.new

# List based on http://www.accountingweb.com/technology/excel/seven-characters-you-cant-use-in-worksheet-names
# Added @ as example of character without this problem
chars = '@\\/*[]:?'
chars.each_char do |char|
  worksheet = workbook.add_worksheet("Test sheet #{char}")
  worksheet.add_cell(0, 0, "Test sheet #{char}")
end
workbook.write("illegal-chars.xlsx")

If I open this file in my version of Excel (Mac, v. 14.6.4) I now see:

excel-worksheet-illegal-chars

("Återskapad_Blad" meaning "Recreated sheet" in Swedish)

Apparently Excel replaces [ and ] with ( and ) respectively but first after it has complained about a corrupt file and asked to repair it.

I personally haven't made up my mind if I would like rubyXL to just silently remove these characters or raise an exception.

weshatheleopard commented 8 years ago

Thanks, fixed in https://github.com/weshatheleopard/rubyXL/commit/21d39dd7ea6765d6c1b5c88b549488df974b1f6b