universal_table/app/views/utable_export/export.xlsx.axlsx

111 lines
3.4 KiB
Plaintext

# encoding: utf-8
wb = xlsx_package.workbook
wb.add_worksheet(name: "Structure") do |sheet|
heading = sheet.styles.add_style(:b => true, :locked => true)
type = sheet.styles.add_style(:i => true)
wrap = sheet.styles.add_style alignment: {wrap_text: true}
row = []
row1 = []
row2 = []
table.table_columns.asc(:order).each do |column|
case column.type
when "text"
site_in_use_locales.sort.each do |locale|
row << column.title + " - " + t(locale.to_s)
row1 << column.key
row2 << column.type + "-#{locale}"
end
when "integer"
row << column.title
row1 << column.key
row2 << column.type
when "editor"
site_in_use_locales.sort.each do |locale|
row << column.title + " - " + t(locale.to_s)
row1 << column.key
row2 << column.type + "-#{locale}"
end
when "image"
row << column.title
row1 << column.key
row2 << "Please leave this column blank. Upload the image manually."
when "date"
row << column.title
row1 << column.key
row2 << column.type + " : " + column.date_format.upcase
when "period"
row << column.title + "-From ~ To"
row1 << column.key
row2 << column.type + " : " + column.date_format.upcase + "-period_from ~ period_to"
when "file"
row << column.title
row1 << column.key
row2 << "Please leave this column blank. Upload the files manually."
end
end
sheet.add_row row, :style => heading
sheet.add_row row1
sheet.add_row row2, :style => type
table.table_entries.asc(:created_at).each do |entry|
row = []
table.table_columns.asc(:order).each do |col|
column = entry.column_entries.where(:table_column_id => col.id).first
case col.type
when "text"
site_in_use_locales.sort.each do |locale|
row << (column.text_translations[locale.to_s] rescue "")
end
when "integer"
row << column.number
when "editor"
site_in_use_locales.sort.each do |locale|
row << (column.content_translations[locale.to_s] rescue "")
end
when "image"
if !column.image.url.nil?
row << url + column.image.url
else
row << ""
end
when "date"
case col.date_format
when "yyyy/MM/dd hh:mm"
row << (column.date.strftime("%Y/%m/%d %H:%M") rescue "")
when "yyyy/MM/dd"
row << (column.date.strftime("%Y/%m/%d") rescue "")
when "yyyy/MM"
row << (column.date.strftime("%Y/%m/") rescue "")
when "yyyy"
row << (column.date.strftime("%Y") rescue "")
end
when "period"
case col.date_format
when "yyyy/MM/dd hh:mm"
row << (column.period_from.strftime("%Y/%m/%d %H:%M")rescue "") + " ~ " + (column.period_to.strftime("%Y/%m/%d %H:%M") rescue "")
when "yyyy/MM/dd"
row << (column.period_from.strftime("%Y/%m/%d")rescue "") + " ~ " + (column.period_to.strftime("%Y/%m/%d") rescue "")
when "yyyy/MM"
row << (column.period_from.strftime("%Y/%m")rescue "") + " ~ " + (column.period_to.strftime("%Y/%m") rescue "")
when "yyyy"
row << (column.period_from.strftime("%Y")rescue "") + " ~ " + (column.period_to.strftime("%Y") rescue "")
end
when "file"
file_links = []
locale = I18n.locale.to_s
column.column_entry_files.desc(:sort_number).each do |entry_file|
next unless entry_file.choose_lang_display(locale)
file_links << (url + entry_file.get_link)
end
row << file_links.join("\r\n")
end
end
sheet.add_row row, style: wrap
end
end