Skip to content

Commit

Permalink
Sync latest libxlsxwriter, Add sheet.write_comment()
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Evstigneev committed Dec 30, 2023
1 parent 9dd3aea commit 6c2280e
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 61 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
#### Version 0.5.0

* Support ruby 3.3
* Sync latest libxlsxwriter
* Add support for writing cell comments
* Add more examples
* Fix compilation of minizip on NixOS (Thanks to @Mange)
* On close set empty columns to default width (Thanks to @steffansluis)

#### Version 0.4.1 - 13 jan 2023

* Support ruby 3.2
Expand Down
2 changes: 2 additions & 0 deletions examples/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

worksheet.write_row(0, ["message", "price", "date", "complete"], bold)

worksheet.write_comment(2, 2, "Comment to field")

for i in 1..1000
worksheet.write_row(i, ["Hello", (rand * 10_000_000).round(2), Time.now, i % 2 == 0])

Expand Down
45 changes: 26 additions & 19 deletions lib/fast_excel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ def self.open(filename = nil, constant_memory: false, default_format: nil)

filename = filename.to_s if defined?(Pathname) && filename.is_a?(Pathname)

workbook = if constant_memory
opt = Libxlsxwriter::WorkbookOptions.new
opt[:constant_memory] = 1
Libxlsxwriter.workbook_new_opt(filename, opt)
else
Libxlsxwriter.workbook_new(filename)
end
opt = Libxlsxwriter::WorkbookOptions.new
opt[:constant_memory] = constant_memory ? 1 : 0
workbook = Libxlsxwriter.workbook_new_opt(filename, opt)

result = Libxlsxwriter::Workbook.new(workbook)

if default_format
Expand Down Expand Up @@ -105,23 +102,33 @@ def self.date_num(time, offset = nil)
time.to_f / XLSX_DATE_DAY + XLSX_DATE_EPOCH_DIFF + offset / XLSX_DATE_DAY
end

def self.print_ffi_obj(value)
puts "#{value.class}"
def self.print_ffi_obj(value, do_print: true, offset: "", deep: false)
result = "#{value.class}"

value.members.each do |key|
field_val = if value[key].is_a?(FFI::Pointer) && value[key].null? || value[key].nil?
fval = value[key]
field_val = if fval.is_a?(FFI::Pointer) && fval.null? || fval.nil?
"nil"
elsif value[key].is_a?(FFI::StructLayout::CharArray)
value[key].to_str.inspect
elsif value[key].is_a?(String)
value[key].inspect
elsif value[key].is_a?(Symbol)
value[key].inspect
elsif fval.is_a?(FFI::StructLayout::CharArray)
fval.to_str.inspect
elsif fval.is_a?(String)
fval.inspect
elsif fval.is_a?(Symbol)
fval.inspect
elsif fval.is_a?(FFI::Struct) && deep
print_ffi_obj(fval, do_print: false, offset: offset + " ", deep: deep)
else
value[key]
fval
end
puts "* #{key}: #{field_val}"

result += "\n#{offset}* #{key}: #{field_val}"
end

if do_print
puts result
else
return result
end
nil
end


Expand Down
14 changes: 7 additions & 7 deletions lib/fast_excel/binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,13 @@ def zip_errno(_Z_ERRNO)
:error_zip_internal_error, 8,
:error_zip_file_add, 9,
:error_zip_close, 10,
:error_null_parameter_ignored, 11,
:error_parameter_validation, 12,
:error_sheetname_length_exceeded, 13,
:error_invalid_sheetname_character, 14,
:error_sheetname_start_end_apostrophe, 15,
:error_sheetname_already_used, 16,
:error_sheetname_reserved, 17,
:error_feature_not_supported, 11,
:error_null_parameter_ignored, 12,
:error_parameter_validation, 13,
:error_sheetname_length_exceeded, 14,
:error_invalid_sheetname_character, 15,
:error_sheetname_start_end_apostrophe, 16,
:error_sheetname_already_used, 17,
:error_32_string_length_exceeded, 18,
:error_128_string_length_exceeded, 19,
:error_255_string_length_exceeded, 20,
Expand Down
17 changes: 17 additions & 0 deletions lib/fast_excel/binding/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,14 @@ class Format < FFI::Struct
include FormatWrappers
layout :file, :pointer,
:xf_format_indices, :pointer, #HashTable.ptr,
:dxf_format_indices, :pointer,
:num_xf_formats, :pointer,
:num_dxf_formats, :pointer,

:xf_index, :int32_t,
:dxf_index, :int32_t,
:xf_id, :int32_t,

:num_format, [:char, 128],
:font_name, [:char, 128],
:font_scheme, [:char, 128],
Expand All @@ -708,6 +713,7 @@ class Format < FFI::Struct
:has_font, :uchar,
:has_dxf_font, :uchar,
:font_size, :double,

:bold, :uchar,
:italic, :uchar,
:font_color, :int,
Expand All @@ -722,24 +728,32 @@ class Format < FFI::Struct
:font_extend, :uchar,
:theme, :uchar,
:hyperlink, :uchar,

:hidden, :uchar,
:locked, :uchar,

:text_h_align, :uchar,
:text_wrap, :uchar,
:text_v_align, :uchar,
:text_justlast, :uchar,
:rotation, :short,

:fg_color, :int,
:bg_color, :int,
:dxf_fg_color, :int,
:dxf_bg_color, :int,

:pattern, :uchar,
:has_fill, :uchar,
:has_dxf_fill, :uchar,
:fill_index, :int,
:fill_count, :int,

:border_index, :int,
:has_border, :uchar,
:has_dxf_border, :uchar,
:border_count, :int,

:bottom, :uchar,
:diag_border, :uchar,
:diag_type, :uchar,
Expand All @@ -751,13 +765,16 @@ class Format < FFI::Struct
:left_color, :int,
:right_color, :int,
:top_color, :int,

:indent, :uchar,
:shrink, :uchar,
:merge_range, :uchar,
:reading_order, :uchar,
:just_distrib, :uchar,
:color_indexed, :uchar,
:font_only, :uchar,

:quote_prefix, :uchar,
:list_pointers, FormatListPointers.by_value
end

Expand Down
56 changes: 39 additions & 17 deletions lib/fast_excel/binding/workbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module Libxlsxwriter
class WorkbookOptions < FFI::Struct
layout :constant_memory, :uchar,
:tmpdir, :string,
:use_zip64, :uchar
:use_zip64, :uchar,
:output_buffer, :pointer,
:output_buffer_size, :pointer
end

# = Fields:
Expand Down Expand Up @@ -174,33 +176,53 @@ class Workbook < FFI::Struct
:chartsheets, :pointer,
:worksheet_names, WorksheetNames.ptr,
:chartsheet_names, :pointer,
:image_md5s, :pointer,
:header_image_md5s, :pointer,
:background_md5s, :pointer,
:charts, Charts.ptr,
:ordered_charts, Charts.ptr,
:formats, Formats.ptr,
:defined_names, DefinedNames.ptr,
:sst, Sst.ptr,
:properties, DocProperties.ptr,
:custom_properties, CustomProperties.ptr,
:filename, :pointer,
:filename, :string,
:options, WorkbookOptions.by_value,
:num_sheets, :uint16,
:num_worksheets, :uint16,
:num_chartsheets, :uint16,
:first_sheet, :uint16,
:active_sheet, :uint16,
:num_xf_formats, :uint16,

:num_sheets, :uint16,
:num_worksheets, :uint16,
:num_chartsheets, :uint16,
:first_sheet, :uint16,
:active_sheet, :uint16,
:num_xf_formats, :uint16,
:num_dxf_formats, :uint16,
:num_format_count, :uint16,
:drawing_count, :uint16,
:font_count, :uint16,
:border_count, :uint16,
:fill_count, :uint16,
:optimize, :uchar,
:has_png, :uchar,
:drawing_count, :uint16,
:comment_count, :uint16,

:font_count, :uint16,
:border_count, :uint16,
:fill_count, :uint16,
:optimize, :uchar,
:max_url_length, :uint16,
:read_only, :uchar,

:has_png, :uchar,
:has_jpeg, :uchar,
:has_bmp, :uchar,
:has_bmp, :uchar,
:has_gif, :uchar,
:has_vml, :uchar,
:has_comments, :uchar,
:has_metadata, :uchar,

:used_xf_formats, HashTable.ptr,
:vba_project, :pointer,
:vba_codename, :pointer
:used_dxf_formats, HashTable.ptr,

:vba_project, :string,
:vba_project_signature, :string,
:vba_codename, :string,

:default_url_format, :pointer
end

attach_function :workbook_default_format, :workbook_default_format, [Workbook], Format
Expand Down
Loading

0 comments on commit 6c2280e

Please sign in to comment.