caxlsx / caxlsx_rails

A Rails plugin to provide templates for the axlsx gem
MIT License
735 stars 84 forks source link

Adding comments at the end of a .axlsx file causes an error #162

Closed atestu closed 1 year ago

atestu commented 1 year ago

It took me a while to figure this out so I thought I'd share in case someone like me searches everywhere for a solution to this weird problem.

When adding a comment at the end of a .axlsx file like so:

wb = xlsx_package.workbook

wb.add_worksheet(name: "Posts") do |sheet|
    @posts.each do |p|
        sheet.add_row [p.title]
    end
end

# won't work anymore!

If you remove the last line, it works fine. If you keep it, it causes this error: undefined method 'empty?' for #<Axlsx::Worksheet:0x0000000105d15dc0>

Full trace:

rack (2.2.4) lib/rack/etag.rb:71:in `block in digest_body'
actionpack (7.0.3.1) lib/action_dispatch/http/response.rb:146:in `each'
actionpack (7.0.3.1) lib/action_dispatch/http/response.rb:146:in `each_chunk'
actionpack (7.0.3.1) lib/action_dispatch/http/response.rb:128:in `each'
actionpack (7.0.3.1) lib/action_dispatch/http/response.rb:76:in `each'
actionpack (7.0.3.1) lib/action_dispatch/http/response.rb:481:in `each'
rack (2.2.4) lib/rack/body_proxy.rb:41:in `method_missing'
rack (2.2.4) lib/rack/etag.rb:69:in `digest_body'
rack (2.2.4) lib/rack/etag.rb:33:in `call'
rack (2.2.4) lib/rack/conditional_get.rb:27:in `call'
rack (2.2.4) lib/rack/head.rb:12:in `call'
actionpack (7.0.3.1) lib/action_dispatch/http/permissions_policy.rb:38:in `call'
actionpack (7.0.3.1) lib/action_dispatch/http/content_security_policy.rb:36:in `call'
rack (2.2.4) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.4) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/cookies.rb:697:in `call'
activerecord (7.0.3.1) lib/active_record/migration.rb:603:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (7.0.3.1) lib/active_support/callbacks.rb:99:in `run_callbacks'
actionpack (7.0.3.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
rollbar (3.3.0) lib/rollbar/middleware/rails/rollbar.rb:25:in `block in call'
rollbar (3.3.0) lib/rollbar.rb:145:in `scoped'
rollbar (3.3.0) lib/rollbar/middleware/rails/rollbar.rb:22:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'
rollbar (3.3.0) lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
web-console (4.2.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.2.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.2.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.2.0) lib/web_console/middleware.rb:17:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'
railties (7.0.3.1) lib/rails/rack/logger.rb:40:in `call_app'
railties (7.0.3.1) lib/rails/rack/logger.rb:25:in `block in call'
activesupport (7.0.3.1) lib/active_support/tagged_logging.rb:114:in `block in tagged'
activesupport (7.0.3.1) lib/active_support/tagged_logging.rb:38:in `tagged'
activesupport (7.0.3.1) lib/active_support/tagged_logging.rb:114:in `tagged'
railties (7.0.3.1) lib/rails/rack/logger.rb:25:in `call'
ahoy_matey (4.1.0) lib/ahoy/engine.rb:22:in `call_with_quiet_ahoy'
sprockets-rails (3.4.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/remote_ip.rb:93:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/request_id.rb:26:in `call'
rack (2.2.4) lib/rack/method_override.rb:24:in `call'
rack (2.2.4) lib/rack/runtime.rb:22:in `call'
activesupport (7.0.3.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/static.rb:23:in `call'
rack (2.2.4) lib/rack/sendfile.rb:110:in `call'
actionpack (7.0.3.1) lib/action_dispatch/middleware/host_authorization.rb:137:in `call'
railties (7.0.3.1) lib/rails/engine.rb:530:in `call'
puma (4.3.12) lib/puma/configuration.rb:228:in `call'
puma (4.3.12) lib/puma/server.rb:727:in `handle_request'
puma (4.3.12) lib/puma/server.rb:476:in `process_client'
puma (4.3.12) lib/puma/server.rb:332:in `block in run'
puma (4.3.12) lib/puma/thread_pool.rb:134:in `block in spawn_thread' 
straydogstudio commented 1 year ago

Interesting. Did you have a new line at the end of that comment? If you didn't, try adding it.

I can probably fix it by forcing a new line at the end of the insert.

The relevant code is here: https://github.com/caxlsx/caxlsx_rails/blob/fa1ad7c1fcba92adcf499dac0520b709137b1096/lib/axlsx_rails/template_handler.rb#L32

atestu commented 1 year ago

Yes, adding a new line after the comment works fine!

dwieringa commented 1 year ago

I came here to report this same bug. I'm in the process of upgrading an old Rails app, and I just upgraded from axlsx_rails 0.1.5 to caxlsx_rails 0.6.3 and spent a fair amount of time tracking this down.

The issue only seems to exist if the last line in your template is a comment. In this case, render_to_string returns an Axlsx::Worksheet rather than a string.

If you add a blank line after the comment, the issue goes away.

I'm glad to see this is already resolved!

straydogstudio commented 1 year ago

@dwieringa Thanks for confirming the fix and contributing your comment.