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

135 lines
3.9 KiB
Plaintext
Executable File

# 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 = []
row << "UID"
row1 << "uid"
row2 << "uid"
table.table_columns.asc(:order).each do |column|
case column.type
when "text", "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 "integer"
row << column.title
row1 << column.key
row2 << column.type
when "image"
row << column.title
row1 << column.key
row2 << "Public URL"
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} (Link)"
row1 << column.key
row2 << "Separate the files by ;"
row << "#{column.title} 註解"
row1 << column.key
row2 << "file_title-#{locale}"
end
end
row << t("universal_table.hashtags")
row1 << "table_tags"
row2 << "Separate tags by ;"
row << t("universal_table.related_entries")
row1 << "related_entries"
row2 << "Separate UIDs with ;"
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 = []
row << entry.uid
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"
row << (column&.image&.url.present? ? (url + column.image.url) : "")
when "date"
format_str = case col.date_format
when "yyyy/MM/dd hh:mm" then "%Y/%m/%d %H:%M"
when "yyyy/MM/dd" then "%Y/%m/%d"
when "yyyy/MM" then "%Y/%m"
when "yyyy" then "%Y"
end
row << (column.date.strftime(format_str) rescue "")
when "period"
format_str = case col.date_format
when "yyyy/MM/dd hh:mm" then "%Y/%m/%d %H:%M"
when "yyyy/MM/dd" then "%Y/%m/%d"
when "yyyy/MM" then "%Y/%m"
when "yyyy" then "%Y"
end
from = (column.period_from.strftime(format_str) rescue "")
to = (column.period_to.strftime(format_str) rescue "")
row << "#{from} ~ #{to}"
when "file"
file_links = []
file_titles = []
locale = "zh_tw"
if column
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)
title = if entry_file.respond_to?(:file_title_translations) && entry_file.file_title_translations.is_a?(Hash)
entry_file.file_title_translations[locale]
elsif entry_file.file_title.is_a?(Hash)
entry_file.file_title[locale]
else
entry_file.file_title
end
title = entry_file.file.filename.to_s if title.blank?
file_titles << title
end
end
row << file_links.join(";")
row << file_titles.join(";")
end
end
row << entry.table_tags.pluck("title").join("; ")
row << entry.get_related_entries_uid
sheet.add_row row, style: wrap
end
end