Cross-platform Generate xlsx Document

Since this thread is closed I’m creating a new one.

In my extension I currently create spreadsheets using WinOLE.
There are several reasons I’d like to find an alternative.

  1. Excel needs to be installed for this to work.
  2. It only works on windows.
  3. It is slow because Excel needs to be started.

In c# I use ClosedXML which is realy fast, but only works on windows.

I’ve done some searching but it seems most of the methods use gems, and installing gems in SketchUp is not something I want to deal with (unless someone can show a fool proof way of delivering them with an extension).

All of these require installing gems.

Does anyone know of a way to do this in SketchUp Ruby?

I thought Ken was writing something ? @kengey

I use WriteXLSX :smiley:

Yes. I did.
If you only need to write xlsx file I would suggest having a look at writexlsx.

Regarding c#. That nowadays is cross platform.

1 Like

The WriteXLSX gem working on macOS and Win. I have used it in several of my projects. You can check it!

1 Like

You might wanr to have a look here if c# is your preference.

And, as I stated here above, c# is cross platform nowadays. I run net core apps on windows, osx, linux and even raspbian on raspberry pi.

1 Like

That looks promising. To use I just need to copy the rb files from the lib folder to my extension?

The only C# I’ve ever done is WPF which is windows only, and a bit of UWP in Windows IoT for my Raspberry pi. I’ve know that C# was more available cross platform the last while by don’t have any experience with it. Would it be possible to create a cross platform C# library that could be called from Ruby? I did a little searching but wasn’t able to find any 1,2,3 tutorials. I’m looking at write_xlsx right now.

I’m getting hung up installing write_xlsx.

The write_xlsx/zip_file_util.srb has a require to some missing file.

require 'zip/zipfilesystem'

Yeah, that is a dependency that also has to be copied over.

1 Like

yes, I essentially do that in https://extensions.sketchup.com/extension/d18f5dc3-ac1c-4615-9e4b-7afc702181cd/xlsx-dumper

Can you shed a little light on this? I copied the zip rb files from here:

I had to change the write_xlsx/zip_file_utils.rb from Zip::ZipFile to Zip::File

But now I’m still getting an error when closing the workbook. Do I have the wrong Zip library?

I had to use an older version of the zip library.


:+1:
image

FYI, with regard to RubyZip.

I had repackaged it into a SketchupExtension archive.
I have them on my disk, but do not find that I’ve posted them here before. (Possibly at SketchUcation?)

Anyway, I RBZ packaged RubyZip versions 1.1.6 and 1.2.0, PM me if you would like one or both.

But generally we’ve discussed it in the past …

A little more on installing and catching errors for loading RubyZip here in …

Disclaimer: I don’t know much about gems as my only Ruby experience in in SketchUp.

I’m assuming that only one version of a gem can be installed at a time? Since the write_xlsx gem requires RubyZip 0.9.xx, wouldn’t I be better off taking the source code of both gems and repackaging it in my own namespace? That would avoid conflict with extensions that rely on other versions.

Yes, gems.

And I also just repacked them into an RBZ but did not purposefully put them into versioned submodules.

I did however package v1.2.0 into a CommuntiyLibraries namespace, so it would be separate from any toplevel installed gem.

This has been the norm when a particular version is required.

Probably why my experiments never gained any traction.


Although why it (write_xlsx) would require a specific older version is very strange.

The NoMethodError you show above may be due to the String#unpack1 method not being in Ruby 2.0.0. It only had Sting#unpack back then (and in some older SketchUp releases.)
Even Ruby 2.2.4 only had Sting#unpack.

So only SketchUp 2019 with Ruby 2.5.1 and higher will have the String#unpack1 method.

But what I find weird is that looking at RubyZip v1.2.0 code for "lib/zip/entry.rb" the read_zip_short() method does not call Sting#unpack1, … it calls the old Sting#unpack:

      def read_zip_short(io) # :nodoc:
        io.read(2).unpack('v')[0]
      end

Oh and the gemspec for write_xlsx has …

gem.add_runtime_dependency 'rubyzip', '>= 1.0.0'

Where did you see it needed to be exactly 0.9.xx ?

Ah, okay I looked at the newest version RubyZip v2.3.0 on GitHub and it’s read zip methods are now using String#unpack1.

So it isn’t an issue with RubyZip nor write_xlsx, it is SketchUp’s packaged Ruby versions coming with SU2018 and earlier.

Right I tested this in 2018, although I’d like to maintain compatibility back to 2016.

Here:

Apparently 1.0 changed Zip::ZipFileSystem to Zip::FileSystem, and Zip::ZipFile to Zip::File
I tried changing the source of write_xlsx, but when I ran into the unpack1 issue I simply downloaded an older version and it worked.

RubyZip 2.0.02.3.0 requires Ruby >= 2.4.x which means SketchUp >= 2019.0 (Ruby 2.5.1)

RubyZip 1.0.01.3.0 requires Ruby >= 1.9.2 which means SketchUp >= 2014.0 (Ruby 2.0.0)

All older RubyZip versions require Ruby >= 1.8.7 which does not support SketchUp <= 2013 (Ruby 1.8.6)

1 Like

Also … according to the write_xlsx change log …

2013-09-07 v0.72.2
work well with rubyzip 1.0.0

2013-09-06 v0.72.1
specify rubyzip version : <1.0.0

So …

Yes RubyZip 1.0.0 simplified both some namespaces and files names. (“zip/zip_file.rb” became “zip/file.rb”, etc.)

But if you use write_xlsx greater than or equal v0.72.2 it should not be making old RubyZip calls.

The current write_xlsx version is 0.85.7. What version did you attempt to use ?

1 Like