mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-06 04:34:06 +08:00
Merge pull request #7685 from artoscinote/ma_SCI_10856
Fix export date formatting, empty export file name [SCI-10856]
This commit is contained in:
commit
b9e2c71972
4 changed files with 34 additions and 12 deletions
|
@ -264,6 +264,7 @@ class RepositoriesController < ApplicationController
|
|||
parsed_file = ImportRepository::ParseRepository.new(
|
||||
file: import_params[:file],
|
||||
repository: @repository,
|
||||
date_format: current_user.settings['date_format'],
|
||||
session: session
|
||||
)
|
||||
if parsed_file.too_large?
|
||||
|
@ -334,7 +335,11 @@ class RepositoriesController < ApplicationController
|
|||
|
||||
xlsx = RepositoryXlsxExport.to_empty_xlsx(@repository, col_ids)
|
||||
|
||||
send_data xlsx, filename: "empty_repository.xlsx", type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
||||
send_data(
|
||||
xlsx,
|
||||
filename: "#{@repository.name.gsub(/\s/, '_')}_template_#{Date.current}.xlsx",
|
||||
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
||||
)
|
||||
end
|
||||
|
||||
def export_repository
|
||||
|
|
|
@ -45,7 +45,7 @@ module RepositoryCsvExport
|
|||
when -1, -2
|
||||
next
|
||||
when -3
|
||||
csv_row << (repository.is_a?(RepositorySnapshot) ? row.parent_id : row.code)
|
||||
csv_row << (repository.is_a?(RepositorySnapshot) ? row.parent.code : row.code)
|
||||
when -4
|
||||
csv_row << row.name
|
||||
when -5
|
||||
|
@ -53,7 +53,7 @@ module RepositoryCsvExport
|
|||
when -6
|
||||
csv_row << I18n.l(row.created_at, format: :full)
|
||||
when -7
|
||||
csv_row << row.updated_at ? I18n.l(row.updated_at, format: :full) : ''
|
||||
csv_row << (row.updated_at ? I18n.l(row.updated_at, format: :full) : '')
|
||||
when -8
|
||||
csv_row << row.last_modified_by.full_name
|
||||
when -9
|
||||
|
|
|
@ -16,6 +16,9 @@ module RepositoryXlsxExport
|
|||
def self.to_xlsx(rows, column_ids, user, repository, handle_file_name_func, in_module)
|
||||
package = Axlsx::Package.new
|
||||
workbook = package.workbook
|
||||
datetime_style = workbook.styles.add_style format_code: 'dd-mmm-yyyy hh:mm:ss'
|
||||
date_style = workbook.styles.add_style format_code: 'dd-mmm-yyyy'
|
||||
|
||||
add_consumption = in_module && !repository.is_a?(RepositorySnapshot) && repository.has_stock_management?
|
||||
|
||||
workbook.add_worksheet(name: 'Data Export') do |sheet|
|
||||
|
@ -28,30 +31,31 @@ module RepositoryXlsxExport
|
|||
when -1, -2
|
||||
next
|
||||
when -3
|
||||
row_data << (repository.is_a?(RepositorySnapshot) ? row.parent_id : row.id)
|
||||
row_data << (repository.is_a?(RepositorySnapshot) ? row.parent.code : row.code)
|
||||
when -4
|
||||
row_data << row.name
|
||||
when -5
|
||||
row_data << row.created_by.full_name
|
||||
when -6
|
||||
row_data << I18n.l(row.created_at, format: :full)
|
||||
row_data << row.created_at
|
||||
when -7
|
||||
row_data << row.updated_at ? I18n.l(row.updated_at, format: :full) : ''
|
||||
row_data << row.updated_at
|
||||
when -8
|
||||
row_data << row.last_modified_by.full_name
|
||||
when -9
|
||||
row_data << (row.archived? && row.archived_by.present? ? row.archived_by.full_name : '')
|
||||
when -10
|
||||
row_data << (row.archived? && row.archived_on.present? ? I18n.l(row.archived_on, format: :full) : '')
|
||||
row_data << (row.archived? && row.archived_on)
|
||||
when -11
|
||||
row_data << row.parent_repository_rows.map(&:code).join(' | ')
|
||||
row_data << row.child_repository_rows.map(&:code).join(' | ')
|
||||
else
|
||||
cell = row.repository_cells.find_by(repository_column_id: c_id)
|
||||
|
||||
row_data << if cell
|
||||
if cell.value_type == 'RepositoryAssetValue' && handle_file_name_func
|
||||
handle_file_name_func.call(cell.value.asset)
|
||||
elsif cell.value_type == 'RepositoryDateTimeValueBase'
|
||||
cell.value.data
|
||||
else
|
||||
cell.value.export_formatted
|
||||
end
|
||||
|
@ -59,15 +63,26 @@ module RepositoryXlsxExport
|
|||
end
|
||||
end
|
||||
row_data << row.row_consumption(row.stock_consumption) if add_consumption
|
||||
sheet.add_row row_data
|
||||
|
||||
style = row_data.map do |c|
|
||||
case c
|
||||
when ActiveSupport::TimeWithZone
|
||||
datetime_style
|
||||
when Time # Date values are of class Time for some reason
|
||||
date_style
|
||||
end
|
||||
end
|
||||
|
||||
sheet.add_row(
|
||||
row_data,
|
||||
style: style
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
package.to_stream.read
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.prepare_header(repository, column_ids, add_consumption)
|
||||
header = []
|
||||
column_ids.each do |c_id|
|
||||
|
|
|
@ -60,7 +60,9 @@ class SpreadsheetParser
|
|||
if cell.is_a?(Roo::Excelx::Cell::Number) && cell.format == 'General'
|
||||
cell&.value&.to_d
|
||||
elsif date_format && cell&.value.is_a?(Date)
|
||||
cell&.value&.strftime(date_format)
|
||||
cell&.value&.strftime(
|
||||
"#{date_format} #{' %H:%M' if cell.value.is_a?(DateTime)}"
|
||||
)
|
||||
else
|
||||
cell&.formatted_value
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue