Rails Roo gem .xlsx 输出包含对象而不是方法的输出

Rails Roo gem .xlsx output contains the object not the output of method

我正在使用 Roo gem 从 Rails 应用程序输出电子表格。我的专栏之一是散列 (Postgres DB)。我想将单元格内容格式化为更具可读性的内容。我正在使用一种方法 return 一个人类可读的单元格。

列数据如下所示:

Inspection.first.results
=> {"soiled"=>"oil on back",
"assigned_to"=>"Warehouse@firedatasolutions.com",
"contaminated"=>"blood on left cuff",
"inspection_date"=>"01/01/2017",
"physical_damage_seam_integrity"=>"",
"physical_damage_thermal_damage"=>"",
"physical_damage_reflective_trim"=>"",
"physical_damage_rips_tears_cuts"=>"small tear on right sleeve",
"correct_assembly_size_compatibility_of_shell_liner_and_drd"=>"",
"physical_damage_damaged_or_missing_hardware_or_closure_systems"=>""}

在我的 Inspections 模型中,我定义了以下方法:

def print_results
  self.results.each do |k,v|
    puts "#{k.titleize}:#{v.humanize}\r\n"
  end
end

所以在控制台中我得到了这个:

Inspection.first.print_results

Soiled:Oil on back
Assigned To:Warehouse
Contaminated:Blood on left cuff
Inspection Date:01/01/2017
Physical Damage Seam Integrity:
Physical Damage Thermal Damage:
Physical Damage Reflective Trim:
Physical Damage Rips Tears Cuts:Small tear on right sleeve
Correct Assembly Size Compatibility Of Shell Liner And Drd:
Physical Damage Damaged Or Missing Hardware Or Closure Systems:
=> {"soiled"=>"oil on back",
 "assigned_to"=>"Warehouse",
 "contaminated"=>"blood on left cuff",
 "inspection_date"=>"01/01/2017",
 "physical_damage_seam_integrity"=>"",
 "physical_damage_thermal_damage"=>"",
 "physical_damage_reflective_trim"=>"",
 "physical_damage_rips_tears_cuts"=>"small tear on right sleeve",
 "correct_assembly_size_compatibility_of_shell_liner_and_drd"=>"",
 "physical_damage_damaged_or_missing_hardware_or_closure_systems"=>""}

但是当我将其放入 index.xlsx.axlsx 文件时

wb = xlsx_package.workbook
wb.add_worksheet(name: "Inspections") do |sheet|
  sheet.add_row ['Serial Number', 'Category', 'Inspection Type', 'Date', 
   'Pass/Fail', 'Assigned To', 'Inspected By', 'Inspection Details']
  @inspections.each do |inspection|
    sheet.add_row [inspection.ppe.serial, inspection.ppe.category, 
    inspection.advanced? ? 'Advanced' : 'Routine',
    inspection.results['inspection_date'], 
    inspection.passed? ? 'Pass' : 'Fail', 
    inspection.ppe.user.last_first_name,
    inspection.user.last_first_name, 
    inspection.print_results]
  end
end

电子表格中的输出是原始散列,而不是打印语句的结果。

{"soiled"=>"oil on back", 
"assigned_to"=>"Warehouse", 
"contaminated"=>"blood on left cuff", "inspection_date"=>"01/01/2017", 
"physical_damage_seam_integrity"=>"", 
"physical_damage_thermal_damage"=>"", 
"physical_damage_reflective_trim"=>"", 
"physical_damage_rips_tears_cuts"=>"small tear on right sleeve", 
"correct_assembly_size_compatibility_of_shell_liner_and_drd"=>"", 
"physical_damage_damaged_or_missing_hardware_or_closure_systems"=>""}

是否可以将方法的输出放入单元格而不是散列对象?

问题是您的 print_results 方法打印出您想要输出到标准输出(即控制台)的内容,但仍然 return 是原始哈希。方法的 return 值对 Roo 来说很重要。

您要做的是将 print_results 重写为 return 格式化字符串:

def print_results
  self.results.map do |k,v|
    "#{k.titleize}:#{v.humanize}\r\n"
  end.join
end

这将 return 一个字符串(注意使用 .join 来组合由 .map 编辑的字符串数组 return),您可以将其放入 Roo 和得到你想要的输出。