mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-18 07:52:23 +01:00
(bug) unable to export to excel
This commit is contained in:
parent
80a2be0872
commit
7ceb03ef8a
@ -1,5 +1,7 @@
|
|||||||
# Changelog Fab-manager
|
# Changelog Fab-manager
|
||||||
|
|
||||||
|
- Fix a bug: in some cases, unable to export to excel files
|
||||||
|
|
||||||
## v5.9.0 2023 March 20
|
## v5.9.0 2023 March 20
|
||||||
|
|
||||||
- Ability to restrict machine reservations per plan
|
- Ability to restrict machine reservations per plan
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Allows splinting a StatisticIndex into multiple types.
|
||||||
|
# e.g. The StatisticIndex "subscriptions" may have types like "1 month", "1 year", etc.
|
||||||
class StatisticType < ApplicationRecord
|
class StatisticType < ApplicationRecord
|
||||||
has_one :statistic_index
|
belongs_to :statistic_index
|
||||||
has_many :statistic_type_sub_types
|
has_many :statistic_type_sub_types, dependent: :destroy
|
||||||
has_many :statistic_sub_types, through: :statistic_type_sub_types
|
has_many :statistic_sub_types, through: :statistic_type_sub_types
|
||||||
has_many :statistic_custom_aggregations
|
has_many :statistic_custom_aggregations, dependent: :destroy
|
||||||
end
|
end
|
||||||
|
30
app/services/excel_service.rb
Normal file
30
app/services/excel_service.rb
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Provides methods around Excel files specification
|
||||||
|
class ExcelService
|
||||||
|
class << self
|
||||||
|
# remove all unauthorized characters from the given Excel's worksheet name
|
||||||
|
# @param name [String]
|
||||||
|
# @param replace [String]
|
||||||
|
# @return [String]
|
||||||
|
def name_safe(name, replace = '-')
|
||||||
|
name.gsub(%r{[*|\\:"<>?/]}, replace).truncate(31)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Generate a name for the current type, compatible with Excel worksheet names
|
||||||
|
# @param type [StatisticType]
|
||||||
|
# @param workbook [Axlsx::Workbook]
|
||||||
|
# @return [String]
|
||||||
|
def statistic_type_sheet_name(type, workbook)
|
||||||
|
# see https://msdn.microsoft.com/fr-fr/library/c6bdca6y(v=vs.90).aspx for unauthorized character list
|
||||||
|
name = "#{type.statistic_index.label} - #{type.label}".gsub(%r{[*|\\:"<>?/]}, '')
|
||||||
|
# sheet name is limited to 31 characters
|
||||||
|
name = if name.length > 31
|
||||||
|
"#{type.statistic_index.label.truncate(4, omission: '.')} - #{type.label}".gsub(%r{[*|\\:"<>?/]}, '').truncate(31)
|
||||||
|
end
|
||||||
|
# we cannot have two sheets with the same name
|
||||||
|
name = name[0..30] + String((rand * 10).to_i) until workbook.sheet_by_name(name).nil?
|
||||||
|
name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -6,7 +6,7 @@ header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('
|
|||||||
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
||||||
|
|
||||||
## Machines slots
|
## Machines slots
|
||||||
wb.add_worksheet(name: t('export_availabilities.machines')) do |sheet|
|
wb.add_worksheet(name: ExcelService.name_safe(t('export_availabilities.machines'))) do |sheet|
|
||||||
|
|
||||||
## data table
|
## data table
|
||||||
# heading labels
|
# heading labels
|
||||||
@ -45,7 +45,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
## Trainings availabilities
|
## Trainings availabilities
|
||||||
wb.add_worksheet(name: t('export_availabilities.trainings')) do |sheet|
|
wb.add_worksheet(name: ExcelService.name_safe(t('export_availabilities.trainings'))) do |sheet|
|
||||||
|
|
||||||
## data table
|
## data table
|
||||||
# heading labels
|
# heading labels
|
||||||
@ -73,7 +73,7 @@ end
|
|||||||
|
|
||||||
## Spaces slots
|
## Spaces slots
|
||||||
if Setting.get('spaces_module')
|
if Setting.get('spaces_module')
|
||||||
wb.add_worksheet(name: t('export_availabilities.spaces')) do |sheet|
|
wb.add_worksheet(name: ExcelService.name_safe(t('export_availabilities.spaces'))) do |sheet|
|
||||||
|
|
||||||
## data table
|
## data table
|
||||||
# heading labels
|
# heading labels
|
||||||
@ -109,7 +109,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
## Events availabilities
|
## Events availabilities
|
||||||
wb.add_worksheet(name: t('export_availabilities.events')) do |sheet|
|
wb.add_worksheet(name: ExcelService.name_safe(t('export_availabilities.events'))) do |sheet|
|
||||||
|
|
||||||
## data table
|
## data table
|
||||||
# heading labels
|
# heading labels
|
||||||
|
@ -6,12 +6,10 @@ bold = wb.styles.add_style b: true
|
|||||||
header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
|
header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
|
||||||
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
||||||
|
|
||||||
wb.add_worksheet(name: @index.label) do |sheet|
|
wb.add_worksheet(name: ExcelService.name_safe(@index.label)) do |sheet|
|
||||||
## heading stats for the current page
|
## heading stats for the current page
|
||||||
sheet.add_row [t('export.entries'), @results['hits']['total']], style: [bold, nil], types: %i[string integer]
|
sheet.add_row [t('export.entries'), @results['hits']['total']], style: [bold, nil], types: %i[string integer]
|
||||||
if @index.ca
|
sheet.add_row [t('export.revenue'), @results['aggregations']['total_ca']['value']], style: [bold, nil], types: %i[string float] if @index.ca
|
||||||
sheet.add_row [t('export.revenue'), @results['aggregations']['total_ca']['value']], style: [bold, nil], types: %i[string float]
|
|
||||||
end
|
|
||||||
sheet.add_row [t('export.average_age'), @results['aggregations']['average_age']['value']], style: [bold, nil], types: %i[string float]
|
sheet.add_row [t('export.average_age'), @results['aggregations']['average_age']['value']], style: [bold, nil], types: %i[string float]
|
||||||
unless @type.simple
|
unless @type.simple
|
||||||
sheet.add_row ["#{t('export.total')} #{@type.label}", @results['aggregations']['total_stat']['value']],
|
sheet.add_row ["#{t('export.total')} #{@type.label}", @results['aggregations']['total_stat']['value']],
|
||||||
|
@ -9,9 +9,7 @@ date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_for
|
|||||||
next unless index.table
|
next unless index.table
|
||||||
|
|
||||||
index.statistic_types.each do |type|
|
index.statistic_types.each do |type|
|
||||||
# see https://msdn.microsoft.com/fr-fr/library/c6bdca6y(v=vs.90).aspx for unauthorized character list
|
wb.add_worksheet(name: ExcelService.statistic_type_sheet_name(type, wb)) do |sheet|
|
||||||
sheet_name = "#{index.label} - #{type.label}".gsub(%r{[*|\\:"<>?/]}, '').truncate(31)
|
|
||||||
wb.add_worksheet(name: sheet_name) do |sheet|
|
|
||||||
## data table
|
## data table
|
||||||
# heading labels
|
# heading labels
|
||||||
columns = [t('export.date'), t('export.user'), t('export.email'), t('export.phone'), t('export.gender'), t('export.age'),
|
columns = [t('export.date'), t('export.user'), t('export.email'), t('export.phone'), t('export.gender'), t('export.age'),
|
||||||
|
@ -5,7 +5,7 @@ wb = xlsx_package.workbook
|
|||||||
header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
|
header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
|
||||||
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
||||||
|
|
||||||
wb.add_worksheet(name: t('export_members.members')) do |sheet|
|
wb.add_worksheet(name: ExcelService.name_safe(t('export_members.members'))) do |sheet|
|
||||||
## data table
|
## data table
|
||||||
# heading labels
|
# heading labels
|
||||||
columns = [t('export_members.id'),
|
columns = [t('export_members.id'),
|
||||||
|
@ -5,7 +5,7 @@ wb = xlsx_package.workbook
|
|||||||
header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
|
header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
|
||||||
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
||||||
|
|
||||||
wb.add_worksheet(name: t('export_reservations.reservations')) do |sheet|
|
wb.add_worksheet(name: ExcelService.name_safe(t('export_reservations.reservations'))) do |sheet|
|
||||||
|
|
||||||
## data table
|
## data table
|
||||||
# heading labels
|
# heading labels
|
||||||
|
@ -5,7 +5,7 @@ wb = xlsx_package.workbook
|
|||||||
header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
|
header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
|
||||||
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
||||||
|
|
||||||
wb.add_worksheet(name: t('export_subscriptions.subscriptions')) do |sheet|
|
wb.add_worksheet(name: ExcelService.name_safe(t('export_subscriptions.subscriptions'))) do |sheet|
|
||||||
|
|
||||||
## data table
|
## data table
|
||||||
# heading labels
|
# heading labels
|
||||||
|
Loading…
x
Reference in New Issue
Block a user