From c3cc50d293f7a300ef1aa59a1eb98fd45ad5482f Mon Sep 17 00:00:00 2001 From: Eric Tsai Date: Thu, 6 Feb 2020 18:10:34 +0800 Subject: [PATCH] add admin :download_excel --- .../admin/activities_controller.rb | 11 ++++ .../admin/personal_activities_helper.rb | 13 +++++ .../activities/download_excel.xlsx.axlsx | 54 +++++++++++++++++++ config/routes.rb | 4 +- 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 app/views/admin/activities/download_excel.xlsx.axlsx diff --git a/app/controllers/admin/activities_controller.rb b/app/controllers/admin/activities_controller.rb index 739ce94..d02e2ab 100644 --- a/app/controllers/admin/activities_controller.rb +++ b/app/controllers/admin/activities_controller.rb @@ -72,6 +72,17 @@ class Admin::ActivitiesController < OrbitMemberController end end + def download_excel + year_start = params[:year_start].to_i + year_end = params[:year_end].to_i + @data = get_data_for_excel(year_start,year_end) + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="activities.xlsx"' + } + end + end + private def set_activity diff --git a/app/helpers/admin/personal_activities_helper.rb b/app/helpers/admin/personal_activities_helper.rb index 2865c9c..31fc27f 100644 --- a/app/helpers/admin/personal_activities_helper.rb +++ b/app/helpers/admin/personal_activities_helper.rb @@ -18,4 +18,17 @@ module Admin::PersonalActivitiesHelper finaldata << data finaldata end + + def get_data_for_excel(year_start,year_end) + data = [] + roles = Role.where(:disabled => false, :title.ne => "", :title.ne => nil).asc(:key) + roles.each do |role| + d = {} + d["name"] = role.title + mps = role.member_profile_ids + d["data"] = Activity.where(:year.gte => year_start, :year.lte => year_end, :member_profile_id.in => mps) rescue [] + data << d + end + return data + end end diff --git a/app/views/admin/activities/download_excel.xlsx.axlsx b/app/views/admin/activities/download_excel.xlsx.axlsx new file mode 100644 index 0000000..552c1c5 --- /dev/null +++ b/app/views/admin/activities/download_excel.xlsx.axlsx @@ -0,0 +1,54 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook +@data.each_with_index do |role,idx| + data = role["data"] + wb.add_worksheet(name: role["name"] + "-" + idx.to_s) do |sheet| + + heading = sheet.styles.add_style(:b => true, :locked => true) + + row = ["Name"] + + @site_in_use_locales.each do |locale| + row << t("personal_activity.activity_name") + " - " + t(locale.to_s) + end + @site_in_use_locales.each do |locale| + row << t("personal_activity.activity_organizer") + " - " + t(locale.to_s) + end + @site_in_use_locales.each do |locale| + row << t("personal_activity.activity_area") + " - " + t(locale.to_s) + end + + row << t("personal_activity.year") + + row << t("personal_activity.activity_start_date") + + row << t("personal_activity.activity_end_date") + + row << t("personal_activity.note") + + sheet.add_row row, :style => heading + + data.each do |activity| + row = [activity.member_profile.name] + @site_in_use_locales.each do |locale| + row << activity.activity_name_translations[locale.to_s] + end + @site_in_use_locales.each do |locale| + row << activity.activity_organizer_translations[locale.to_s] + end + @site_in_use_locales.each do |locale| + row << activity.activity_area_translations[locale.to_s] + end + + row << activity.year + row << activity.activity_start_date + row << activity.activity_end_date + row << activity.note + sheet.add_row row + end + end +end + + + diff --git a/config/routes.rb b/config/routes.rb index b5fc50e..1194e92 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ Rails.application.routes.draw do scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do get 'activities/download_excel_format' => 'activities#download_excel_format' - #post 'books/import_from_excel' => 'books#import_from_excel' + #post 'activities/import_from_excel' => 'activities#import_from_excel' resources :activities do collection do @@ -12,7 +12,7 @@ Rails.application.routes.draw do # get 'toggle_hide' => 'books#toggle_hide' get 'analysis' get 'analysis_report' - # get "download_excel" + get 'download_excel' end end end