lib/fast_excel.rb in fast_excel-0.1.3 vs lib/fast_excel.rb in fast_excel-0.1.4

- old
+ new

@@ -63,11 +63,32 @@ end time.to_f / XLSX_DATE_DAY + XLSX_DATE_EPOCH_DIFF + offset / XLSX_DATE_DAY end + module AttributeHelper + def set(value) + value.each do |key, value| + if respond_to?("#{key}=") + send("#{key}=", value) + else + self[key] = value + end + end + end + + def pretty_print(pp) + res = {} + members.each do |key| + res[key] = respond_to?(key) ? send(key) : self[key] + end + pp res + end + end + module WorkbookExt + include AttributeHelper def bold_cell_format bold = add_format bold.set_bold bold @@ -85,10 +106,11 @@ super end end module WorksheetExt + include AttributeHelper def write_row(row_number, values, formats = nil) values.each_with_index do |value, index| format = if formats formats.is_a?(Array) ? formats[index] : formats @@ -107,13 +129,64 @@ end end end end + + module FormatExt + include AttributeHelper + + [:font_size, :font_name, :underline, :font_script, :num_format, :align, :rotation, :indent, :pattern, :border].each do |prop| + define_method(prop) do + self[prop] + end + define_method("#{prop}=") do |value| + send("set_#{prop}", value) + end + end + + [:bold, :italic, :font_outline, :font_shadow, :hidden, :text_wrap, :font_strikeout, :shrink, :text_justlast].each do |prop| + define_method(prop) do + self[prop] + end + define_method("#{prop}=") do |value| + value ? send("set_#{prop}") : self[prop] = false + end + end + + [:num_format, :font_name].each do |prop| + define_method(prop) do + self[prop].to_ptr.read_string + end + + define_method("#{prop}=") do |value| + send("set_#{prop}", value) + end + end + + def set_font_size(value) + if value < 0 + raise ArgumentError, "font size should be >= 0 (use 0 for user default font size)" + end + super(value) + end + + def font_family + font_name + end + + def font_family=(value) + self.font_name = value + end + end end Libxlsxwriter::Workbook.instance_eval do include FastExcel::WorkbookExt +end + +Libxlsxwriter::Format.instance_eval do + include FastExcel::FormatExt end Libxlsxwriter::Worksheet.instance_eval do include FastExcel::WorksheetExt end \ No newline at end of file