Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

apply_styles method on sheet #9

Closed
westonganger opened this issue Jun 18, 2016 · 11 comments
Closed

apply_styles method on sheet #9

westonganger opened this issue Jun 18, 2016 · 11 comments

Comments

@westonganger
Copy link
Collaborator

In my gem SpreadsheetArchitect I have the ability to create axlsx sheets individually. I was wondering if we can get the workbook.apply_styles to also be available as sheet.apply_styles so that I can apply the styling before returning the sheet.

@sakovias
Copy link
Collaborator

If you work with vanilla axlsx, you probably still need to call axlsx.serialize <my_workbook> when you're done with the whole workbook (all the worksheets included) to have it processed and saved as .xlsx file. If I understand it correctly, axlsx sets styles on the entire workbook, which is something I can't change.

As far as axlsx_styler goes, I think there's an opportunity to hide the call workbook.apply_styles from the end user. Would it solve your problem? Fell free to use examples to help me understand where your problem is.

@westonganger
Copy link
Collaborator Author

I think hiding that might solve the issue. How would it work though?

@sakovias
Copy link
Collaborator

sakovias commented Jun 18, 2016

It's possible that we can patch axlsx.serialize from this gem's side, but I don't know for sure yet.

@westonganger
Copy link
Collaborator Author

I like that idea. Make sure to also patch the to_stream method. I use this method instead of serialize so that I don't have to save to a file.

@westonganger
Copy link
Collaborator Author

westonganger commented Jun 19, 2016

We could do it like this:

module AxlsxStyler
  module Axlsx
    module Package

      original_serialize = instance_method(:serialize)

      define_method :serialize do
        @workbook.apply_styles
        original_serialize.bind(self).()
      end

      original_to_stream = instance_method(:to_stream)

      define_method :to_stream do
        @workbook.apply_styles
        original_to_stream.bind(self).()
      end

    end
  end
end

I read this method of patching from this stackoverflow post http://stackoverflow.com/questions/4470108/when-monkey-patching-a-method-can-you-call-the-overridden-method-from-the-new-i

@sakovias
Copy link
Collaborator

Thanks for the example. I will look at it some time next week.

In the meantime it would be great to know what is the root cause of your problems. workbook.apply_styles should work, as all styles (even in the original axlsx) are defined on the workbook, not on its worksheets.

@westonganger
Copy link
Collaborator Author

Sorry I should have mentioned that its not an issue. I was just wondering if that method could possibly done on the sheet level but you say that's not possible so that's okay with me.

@sakovias
Copy link
Collaborator

Ah, good. I'm just going to close this one then.

@westonganger
Copy link
Collaborator Author

Are you still going to try and hide the call to apply_styles?

@sakovias
Copy link
Collaborator

I thought to slack away for a bit, but you're puting me on the spot 😄

Could you please review this change?

@sakovias
Copy link
Collaborator

Also, if you want to make your own PR, please feel free to do so.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants