2017-10-10 41 views
0

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

列数据是这样的:

Inspection.first.results 
=> {"soiled"=>"oil on back", 
"assigned_to"=>"[email protected]", 
"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"=>""} 

在我的检查模式,我定义了以下方法:

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"=>""} 

是否有可能得到方法的输出到单元而不是哈希对象?

回答

1

问题是您的print_results方法打印出你想要的stdout(即控制台),但仍然返回原始散列。该方法的回报值对Roo而言都很重要。

你想要做的是重写print_results返回格式化字符串:

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

这将返回一个字符串(注意使用.join.map返回的字符串数组结合),您可以抛出进入Roo并获得您想要的输出。

+0

完美。 Upvoted和接受。我忘了'puts'的返回值并不是打印到控制台的东西。行换行不起作用,但这是因为我必须弄清楚如何获取应用于行的换行样式。 – Beartech