added excel import

This commit is contained in:
Harry Bomrah 2015-12-14 19:49:14 +08:00
parent 4193303093
commit 14743f682b
5 changed files with 181 additions and 1 deletions

View File

@ -96,6 +96,33 @@ class Admin::ResearchsController < OrbitMemberController
end
end
def excel_format
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = 'attachment; filename="research_project_format.xlsx"'
}
end
end
def import_from_excel
workbook = RubyXL::Parser.parse(params["import_file"].tempfile)
sheet = workbook[0]
if sheet.count <= 503
sheet.each_with_index do |row, i|
next if i < 3
user = User.where(:user_name => row.cells[0].value).first rescue nil
if !user.nil?
mp = user.member_profile
import_this_research(row,mp)
end
end
redirect_to admin_researchs_url
else
redirect_to admin_researchs_url(:error => "1")
end
end
def toggle_hide
if params[:ids]
@researchs = Research.any_in(_id: params[:ids])

View File

@ -25,4 +25,58 @@ module Admin::PersonalResearchesHelper
end
researchs
end
def import_this_research(row,mp)
value = nil
research = Research.new
row.cells.each_with_index do |cell,index|
next if index < 2
next if cell.nil?
val = cell.value
next if val.nil? || val == ""
case index
when 2
value = {"en" => val}
when 3
begin
value["zh_tw"] = val
rescue
value = {"zh_tw" => val}
end
research.research_title_translations = value
when 4
value = {"en" => val}
when 5
begin
value["zh_tw"] = val
rescue
value = {"zh_tw" => val}
end
research.extracted_chapters_translations = value
when 6
value = {"en" => val}
when 7
begin
value["zh_tw"] = val
rescue
value = {"zh_tw" => val}
end
research.authors_translations = value
when 8
research.year = val
when 9
research.language = val
when 10
research.publish_date = val
when 11
research.url = val
when 12
research.keywords = val
when 13
research.note = val
end
end
research.member_profile = mp
research.save
end
end

View File

@ -0,0 +1,77 @@
# encoding: utf-8
wb = xlsx_package.workbook
wb.add_worksheet(name: "Journal Paper") do |sheet|
heading = sheet.styles.add_style(:b => true, :locked => true)
example = sheet.styles.add_style(:i => true)
row = ["user_id"]
row1 = [""]
row2 = [""]
row << "name"
row1 << ""
row2 << ""
row << t("personal_research.research_title") + " - " + t("en")
row1 << "textfield"
row2 << ""
row << t("personal_research.research_title") + " - " + t("zh_tw")
row1 << "textfield"
row2 << ""
row << t("personal_research.extracted_chapters") + " - " + t("en")
row1 << "textfield"
row2 << ""
row << t("personal_research.extracted_chapters") + " - " + t("zh_tw")
row1 << "textfield"
row2 << ""
row << t("personal_research.authors") + " - " + t("en")
row1 << "textfield"
row2 << ""
row << t("personal_research.authors") + " - " + t("zh_tw")
row1 << "textfield"
row2 << ""
row << t("personal_research.year")
row1 << "number"
row2 << "Example : 2015 or 2014 or 1987"
row << t("personal_research.language")
row1 << "select"
row2 << "en -> English, zh_tw -> Chinese"
row << t("personal_research.publication_date")
row1 << "date"
row2 << "Format: YYYY/MM, Example: 2015/12"
row << t("personal_research.url")
row1 << "textfield"
row2 << "http://domain.com/path"
row << t("personal_research.keywords")
row1 << "textfield"
row2 << "Example: keyword1,keyword2"
row << t("personal_research.note")
row1 << "textarea"
row2 << ""
sheet.add_row row, :style => heading
sheet.add_row row1
sheet.add_row row2, :style => example
User.where(:user_name.ne => "rulingcom").each do |user|
r = [user.user_name]
r << user.name
sheet.add_row r
end
end

View File

@ -14,9 +14,29 @@
<div class="bottomnav clearfix">
<div class="action pull-right">
<div class="dropup upload-button">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
<i class="icon-upload-alt icon-white"></i>上傳
<span class="caret"></span>
</button>
<div class="dropdown-menu upload-box">
<form action="/admin/researchs/import_from_excel" method="post" enctype="multipart/form-data">
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<input type="file" name="import_file" >
<button class="btn btn-primary" type="submit"><%= t(:submit) %></button>
<a class="" href="/admin/researchs/download_excel_format.xlsx">Download excel format</a>
</form>
</div>
</div>
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_research_path, :class => 'btn btn-primary' %>
</div>
<div class="pagination pagination-centered">
<%= content_tag :div, paginate(@researchs), class: "pagination pagination-centered" %>
</div>
</div>
</div>
<% if params[:error] == "1" %>
<script type="text/javascript">
alert("File cannot be imported. File has more than 500 entries. Please seperate the entries in different files.");
window.location.href = "<%= admin_researchs_url %>"
</script>
<% end %>

View File

@ -3,6 +3,8 @@ Rails.application.routes.draw do
scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do
get 'research_setting' => "researchs#setting"
get 'researchs/download_excel_format' => 'researchs#excel_format'
post 'researchs/import_from_excel' => 'researchs#import_from_excel'
resources :researchs do
collection do