Class: Labimotion::ExportDataset
- Inherits:
-
Object
- Object
- Labimotion::ExportDataset
- Defined in:
- lib/labimotion/libs/export_dataset.rb
Overview
ExportDataset
Constant Summary collapse
- DEFAULT_ROW_WIDTH =
100- DEFAULT_ROW_HEIGHT =
20
Instance Method Summary collapse
- #description(ds, id) ⇒ Object
- #export(id) ⇒ Object
- #header ⇒ Object
-
#initialize(**args) ⇒ ExportDataset
constructor
A new instance of ExportDataset.
- #ols_name(id) ⇒ Object
- #read ⇒ Object
- #res_name(id) ⇒ Object
- #spectra(id) ⇒ Object
Constructor Details
#initialize(**args) ⇒ ExportDataset
Returns a new instance of ExportDataset.
11 12 13 14 15 |
# File 'lib/labimotion/libs/export_dataset.rb', line 11 def initialize(**args) @xfile = Axlsx::Package.new @file_extension = 'xlsx' @xfile.workbook.styles.fonts.first.name = 'Calibri' end |
Instance Method Details
#description(ds, id) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/labimotion/libs/export_dataset.rb', line 37 def description(ds, id) wb = @xfile.workbook sheet = @xfile.workbook.add_worksheet(name: 'Description') header_style = sheet.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] }) sheet.add_row(['File name', res_name(id)]) sheet.add_row(['Time', Time.now.strftime("%Y-%m-%d %H:%M:%S %Z")] ) sheet.add_row(['(This file is automatically generated by the system.)']) sheet.add_row(['']) sheet.add_row(['']) sheet.add_row(['Fields description of sheet:' + ds.dataset_klass.label]) sheet.add_row(['Fields', 'Field description'], style: header_style) sheet.add_row(['Layer Label', 'The label of the layer']) sheet.add_row(['Field Label', 'The label of the field']) sheet.add_row(['Value', 'The current value of the field']) sheet.add_row(['Unit', 'The unit of the field']) sheet.add_row(['Name', 'The key of the field, can be used to identify the field']) sheet.add_row(['Type', 'The type of the field']) sheet.add_row(['Source?', '[Device] from device, [Chemotion] from Chemotion']) sheet.add_row(['Source identifier', 'The source identifier']) sheet.add_row(['Source data', 'The data from Device or Chemotion, cannot be modified once a generic dataset is created']) sheet.add_row(['']) sheet.add_row(['']) end |
#export(id) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/labimotion/libs/export_dataset.rb', line 61 def export(id) ds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container') return if ds.nil? description(ds, id) wb = @xfile.workbook name = ols_name(id) return if name.nil? sheet = @xfile.workbook.add_worksheet(name: name) sheet.add_row([ds.dataset_klass.label]) header_style = sheet.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] }) layer_style = sheet.styles.add_style(b: true, bg_color: 'CEECF5') sheet.add_row(header, style: header_style) layers = ds.properties['layers'] || {} layer_keys = layers.keys.sort_by { |key| layers[key]['position'] } layer_keys.each do |key| layer = layers[key] sheet.add_row([layer['label'], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], style: layer_style) sorted_fields = layer['fields'].sort_by { |obj| obj['position'] } sorted_fields.each do |field| next if field['type'] == 'dummy' type = field['type'] from_device = field['device'].present? ? 'Device' : '' from_device = field['system'].present? ? 'Chemotion' : from_device type = "#{field['type']}-#{field['option_layers']}" if field['type'] == 'select' || field['type'] == 'system-defined' show_value = field['value'] =~ /\A\d+,\d+\z/ ? field['value']&.gsub(',', '.') : field['value'] sheet.add_row([' ', field['label'], nil, field['value_system'], field['field'], type, from_device, field['dkey'], nil].freeze) if %w[system-defined integer].include? field['type'] sheet.rows.last.cells[2].type = :integer sheet.rows.last.cells[8].type = :integer else sheet.rows.last.cells[2].type = :string sheet.rows.last.cells[8].type = :string end sheet.rows.last.cells[2].value = show_value sheet.rows.last.cells[8].value = field['system'] || field['device'] end # sheet.column_widths nil, nil, nil, nil, 0, 0, 0, 0, 0 end end |
#header ⇒ Object
151 152 153 |
# File 'lib/labimotion/libs/export_dataset.rb', line 151 def header ['Layer Label', 'Field Label', 'Value', 'Unit', 'Name', 'Type', 'Source?', 'Source identifier', 'Source data'].freeze end |
#ols_name(id) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/labimotion/libs/export_dataset.rb', line 23 def ols_name(id) ds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container') return nil if ds.nil? name = ds.dataset_klass.label match = name.match(/\((.*?)\)/) name = match && match.length > 1 ? match[1] : name name = '1H NMR' if ds.dataset_klass.ols_term_id == 'CHMO:0000593' name = '13C NMR' if ds.dataset_klass.ols_term_id == 'CHMO:0000595' name.slice(0, 26) end |
#read ⇒ Object
155 156 157 |
# File 'lib/labimotion/libs/export_dataset.rb', line 155 def read @xfile.to_stream.read end |
#res_name(id) ⇒ Object
17 18 19 20 21 |
# File 'lib/labimotion/libs/export_dataset.rb', line 17 def res_name(id) element_name = Container.find(id)&.root_element&.short_label ols = ols_name(id) "#{element_name}_#{ols.gsub(' ', '_')}.xlsx" end |
#spectra(id) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/labimotion/libs/export_dataset.rb', line 110 def spectra(id) name_mapping = [] wb = @xfile.workbook gds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container') cds = Container.find(id) cds_csv = cds..where(aasm_state: 'csv').order(:filename) csv_length = cds_csv.length return if csv_length.zero? cds_csv.each_with_index do |att, idx| sheet_name = "Sheet#{idx+1}" sheet = @xfile.workbook.add_worksheet(name: sheet_name) name_mapping.push([sheet_name, att.filename]) if Labimotion::IS_RAILS5 == true File.open(att.store.path) do |fi| fi.each_line do |line| sheet.add_row(line.split(',')) end end else File.open(att.) do |fi| fi.each_line do |line| sheet.add_row(line.split(',')) end end end end if name_mapping.length > 1 first_sheet = @xfile.workbook.worksheets&.first header_style = first_sheet&.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] }) first_sheet&.add_row(['Sheet name', 'File name'], style: header_style) name_mapping&.each do |mapping| next if mapping.length < 2 @xfile.workbook.worksheets&.first&.add_row([mapping[0].to_s, mapping[1].to_s]) end end end |