zdavatz / spreadsheet

The Ruby Spreadsheet by ywesee GmbH
http://spreadsheet.ch
GNU General Public License v3.0
1.13k stars 240 forks source link

Error writing to file #246

Closed srehne closed 4 years ago

srehne commented 5 years ago

I have tested the solution on this document:

http://somebody.dk/wp-content/uploads/2019/11/TOMv330.xls

But trying to write to a cell destroys elements of the document

This is my code:

module Excel
  class SendContract2
    require "spreadsheet"

    def self.return_contract

      Spreadsheet.client_encoding = 'UTF-8'

      tempXLSX = Rails.root.join('app/assets/spreadsheets/contract/TOMv330.xls')
      target = Rails.root.join('2019-11-05_190100.xls')
      target2 = Rails.root.join('2019-11-05_190100_out.xls')

      FileUtils.copy_file(tempXLSX, target)

      book = Spreadsheet.open(target)
      sheet1 = book.worksheet 0

      puts sheet1[0,0]
      sheet1[0,0] = "Tester"

      book.write target2

      return target2
    end
  end
end

I call it from console this way:

f = Excel::SendContract2.return_contract The output says:

KONTRAKT OPLYSNINGER
=> #<Pathname:/Users/sre/Project/Motorrummet/web/2019-11-05_190100.xls>

I open the f file using:

`open #{f}`

And then Excel says: "Alert: File error: data may have been lost" After this alert: "Alert: Unable to read file" Then: "Alert: We found a problem with some content in xxxx Do you want us to try to recover as much as we can? .... " Saying yes opens up the document The good thing: The cell headline was changed ;-) The bad: All formatting is lost ... And even 1 of the sheets is completely missing.

Anyone who can help? Kind Regards Søren Rehné

zdavatz commented 5 years ago

thanks for reporting this!

srehne commented 5 years ago

Nothing happens if no-one is giving good examples. I hope this might be fixed in the future. I will be looking at writing my own functionality to edit xlsx files ... it's "just" zip files with XML - right? ;-) But I would love if this gem - over time - would be more "robust". I have more projects that would like to use XL I'm really not good enough with ruby to try and work out the problem with the sourcecode myself.

Since I don't want the Excel spreadsheet to be available "forever" ... could you please let me know when you have taken a copy? Kind Regards Søren Rehné

zdavatz commented 5 years ago

spreadsheet does NOT support XLSX files.

srehne commented 5 years ago

I'm very aware of this. But if it worked with my .xls document - I would stick to that format ;-)