weshatheleopard / rubyXL

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

creation and configuration of panes (split) #312

Closed koenhandekyn closed 6 years ago

koenhandekyn commented 6 years ago

is there a way with the current API to create panes and split configurations? thanks for any hints if possible, i'll write out some documentation from it and contribute it to the main readme.

weshatheleopard commented 6 years ago
require 'rubyXL'
workbook = RubyXL::Workbook.new
worksheet = workbook[0]

worksheetviews = RubyXL::WorksheetViews.new
worksheetviews << RubyXL::WorksheetView.new(:pane => RubyXL::Pane.new(:top_left_cell => RubyXL::Reference.new(4,8), :x_split => 4000, :y_split => 2000))

worksheet.sheet_views = worksheetviews
workbook.save "test.xlsx"

(Obviously, use your particular numbers). Regarding calculation for x_split and y_split in split mode, see https://stackoverflow.com/questions/4577546/calculating-height-width-and-ysplit-xsplit-for-open-xml-spreadsheets ; in frozen and frozenSplit modes they are simply numbers of rows/columns.

If you think of making a more streamlined API for these circumstances, please make the proposal of what such API should look like, and we'll discuss/implement it.

koenhandekyn commented 6 years ago

thanks a lot for the hint.

first i got the split variant working. the calculations for the width from the above referenced perl script where correct but not for the height, but some trial and error solved that.

but it seemed that i needed the 'frozenSplit' variant and for me still unknown reasons i got it to work only with xsplit=1 and ysplit=1 values (inspired from other places on the web), and the result in Mac OS X Office at least seems correct, the previous values rendered corrupt xlsx files.

      worksheetview = RubyXL::WorksheetView.new
      worksheetview.pane = RubyXL::Pane.new(:top_left_cell => RubyXL::Reference.new(1,1),
                                            :x_split => 1,
                                            :y_split => 1,
                                            :state => 'frozenSplit')

      worksheetviews = RubyXL::WorksheetViews.new
      worksheetviews << worksheetview
      worksheet.sheet_views = worksheetviews
weshatheleopard commented 6 years ago

@koenhandekyn your problem is that in frozen and frozenSplit modes the values for xSplit and ySplit must be not greater than the cell referenced by topLeftCell. Since you specified topLeftCell to be A1, neither can be greater than 1. Change that value and try again.