class PIM::MappingExporter::ExcelPageExportDocument

Constants

ZIP_CONTENT_TYPE

Attributes

zip_archive[R]

Public Class Methods

matches?(mapping, template_asset) click to toggle source
# File mapping-exporter.rb, line 223
def self.matches? mapping, template_asset
  mapping.template_type.to_s == 'PAGE' && mapping.content_type == ZIP_CONTENT_TYPE && template_asset.content_type == EXCEL_CONTENT_TYPE
end
new(*args) click to toggle source
# File mapping-exporter.rb, line 227
def initialize *args
  super

  @item_count = 0

  @template_extension = File.extname(template_asset.path)
  @template_basename = File.basename(template_asset.path, @template_extension)
  @template_dirname = File.dirname(template_asset.path)

end

Public Instance Methods

create_workbook() click to toggle source
# File mapping-exporter.rb, line 252
def create_workbook
  PIM.log_debug "creating workbook from template at #{template_asset}"
  open_xlsx_workbook(template_asset.path, template_asset.gathering_key)
end
output_content_type() click to toggle source
# File mapping-exporter.rb, line 238
def output_content_type
  ZIP_CONTENT_TYPE
end
start() click to toggle source
# File mapping-exporter.rb, line 242
def start
  @gathering_key = PIM::Services::AssetService.create_gathering_key
  PIM.log_debug "created gathering key #{@gathering_key}"
end
stop() click to toggle source
# File mapping-exporter.rb, line 247
def stop
  PIM.log_debug "storing workbooks from gathering into zip archive"
  PIM::Services::ZipService.zip_gathering(@gathering_key, output)
end
write_mapped_item(mapped_item) click to toggle source
# File mapping-exporter.rb, line 257
def write_mapped_item mapped_item

  workbook = create_workbook

  mapped_item.mapped_values.each do |mapped_value|

    location = mapped_value[:location]
    sheet, column, row = split_location(location)

    # TODO: Check if it was possible to only use 'column' or 'external_name' to identify location!
    if PIM.is_empty?(column) or PIM.is_empty?(row)
      external_name = mapped_value[:external_name] || '<not set>'
      PIM.log_debug "#{self.class}#write_mapped_item: column and/or row are empty for location #{location}, external_name: #{external_name}"
      next
    end

    sheet ||= 0
    location = "#{column}#{row}"

    value = mapped_value[:mapped_value]
    PIM.log_debug "#{self.class}#write_mapped_item, sheet: #{sheet}, location: #{location}, value: #{value}, value.class: #{value.class}"

    begin
      set_cell_value_at workbook, sheet, "#{location}", value
    rescue Exception => e
      PIM.log_error("Could not set cell value '#{value}' at #{location}", e)
    end

  end

  # TODO: Provide a way to calculate the resulting filename

  suffix = PIM.get_value(mapped_item[:item], 'primaryKey', 'primaryKey__')
  suffix = "#{@item_count}" if PIM.is_empty?(suffix)
  filename = PIM.cleanup_filename("#{@template_basename}-#{suffix}#{@template_extension}")

  PIM.log_debug "storing workbook"
  save_workbook(workbook, filename, @gathering_key)

  @item_count += 1

end