From 0e6994b3ac964cfd78caa37c06912665267121d3 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 1 Jun 2016 19:39:51 +0800 Subject: [PATCH] added excel export --- .../admin/journal_papers_controller.rb | 13 ++- app/helpers/admin/journal_papers_helper.rb | 15 ++++ .../admin/journal_papers/analysis.html.erb | 7 +- .../journal_papers/download_excel.xlsx.axlsx | 90 +++++++++++++++++++ config/routes.rb | 1 + 5 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 app/views/admin/journal_papers/download_excel.xlsx.axlsx diff --git a/app/controllers/admin/journal_papers_controller.rb b/app/controllers/admin/journal_papers_controller.rb index f528a9a..d8c675c 100644 --- a/app/controllers/admin/journal_papers_controller.rb +++ b/app/controllers/admin/journal_papers_controller.rb @@ -33,10 +33,21 @@ class Admin::JournalPapersController < OrbitMemberController graph_by = params[:graph_by] @data = get_chart_data(year_start,year_end,role,params[:graph_by]) - + render :layout => false 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="journal_papers.xlsx"' + } + end + end + def create if !journal_paper_params['member_profile_id'].blank? diff --git a/app/helpers/admin/journal_papers_helper.rb b/app/helpers/admin/journal_papers_helper.rb index 49b3861..881af51 100644 --- a/app/helpers/admin/journal_papers_helper.rb +++ b/app/helpers/admin/journal_papers_helper.rb @@ -116,6 +116,21 @@ module Admin::JournalPapersHelper jp.save end + def get_data_for_excel(year_start,year_end) + data = [] + roles = Role.where(:disabled => false, :title.ne => "", :title.ne => nil) + roles.each do |role| + d = {} + d["name"] = role.title + mps = role.member_profile_ids + d1 = DateTime.new(year_start,1,1,0,0) + d2 = DateTime.new(year_end,12,31,23,59) + d["data"] = JournalPaper.where(:publication_date.gte => d1, :publication_date.lte => d2, :member_profile_id.in => mps) rescue [] + data << d + end + return data + end + def get_chart_data(year_start,year_end,role,type) case type when "paper_type" diff --git a/app/views/admin/journal_papers/analysis.html.erb b/app/views/admin/journal_papers/analysis.html.erb index 5c30677..c0dc089 100644 --- a/app/views/admin/journal_papers/analysis.html.erb +++ b/app/views/admin/journal_papers/analysis.html.erb @@ -56,7 +56,7 @@
- +
@@ -76,6 +76,11 @@ roleArea = $("#analysis-area .analysis-show-area"), totalRoles = roleArea.find(".role").length; form.form.on("submit",function(){return false;}) + + $("#generate_excel").on("click",function(){ + window.location.href = "/admin/journal_papers/download_excel.xlsx?" + "year_start=" + form.form.find("select[name=start_year]").val() + "&year_end=" + form.form.find("select[name=end_year]").val(); + }) + $("#generate_graph").on("click",function(){ if(form.isFormValidated()){ generateCharts(0); diff --git a/app/views/admin/journal_papers/download_excel.xlsx.axlsx b/app/views/admin/journal_papers/download_excel.xlsx.axlsx new file mode 100644 index 0000000..0e36354 --- /dev/null +++ b/app/views/admin/journal_papers/download_excel.xlsx.axlsx @@ -0,0 +1,90 @@ +# 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_journal.paper_title") + " - " + t(locale.to_s) + end + + @site_in_use_locales.each do |locale| + row << t("personal_journal.journal_title") + " - " + t(locale.to_s) + end + + @site_in_use_locales.each do |locale| + row << t("personal_journal.authors") + " - " + t(locale) + end + + row << t("personal_journal.year") + + row << t("personal_journal.language") + + row << t("personal_journal.vol_no") + + row << t("personal_journal.issue_no") + + row << t("personal_journal.total_pages") + + row << t("personal_journal.level_type") + + row << t("personal_journal.paper_type") + + row << t("personal_journal.author_type") + + row << t("personal_journal.number_of_authors") + + row << t("personal_journal.isbn") + + row << t("personal_journal.publication_date") + + row << t("personal_journal.url") + + row << t("personal_journal.keywords") + + row << t("personal_journal.abstract") + + row << t("personal_journal.note") + + row << t("personal_journal.associated_project") + + sheet.add_row row, :style => heading + + data.each do |jp| + row = [jp.member_profile.name] + @site_in_use_locales.each do |locale| + row << jp.paper_title_translations[locale.to_s] + end + @site_in_use_locales.each do |locale| + row << jp.journal_title_translations[locale.to_s] + end + @site_in_use_locales.each do |locale| + row << jp.authors_translations[locale.to_s] + end + row << jp.year + row << jp.language + row << jp.vol_no + row << jp.issue_no + row << jp.total_pages + row << (jp.journal_levels.map{|jl| jl.title}.join(", ") rescue "") + row << (jp.journal_paper_type.title rescue "") + row << (jp.journal_paper_author_types.map{|jl| jl.title}.join(", ") rescue "") + row << jp.number_of_authors + row << jp.isbn + row << jp.publication_date.strftime("%Y-%m-%d") rescue "" + row << jp.url + row << jp.keywords + row << jp.abstract + row << jp.note + row << jp.associated_project + sheet.add_row row + end + end +end + + diff --git a/config/routes.rb b/config/routes.rb index 06bb00c..510e2a4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,7 @@ Rails.application.routes.draw do get 'toggle_hide' => 'journal_papers#toggle_hide' get 'analysis' => 'journal_papers#analysis' get 'analysis_report' => 'journal_papers#analysis_report' + get "download_excel" end end