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

92 lines
2.7 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)
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 "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"
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]
end
when "editor"
site_in_use_locales.sort.each do |locale|
row << column.content_translations[locale.to_s]
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
end
end
sheet.add_row row
end
end