educoder/app/imports/ecs/import_achievement_excel.rb

37 lines
965 B

class Ecs::ImportAchievementExcel < BaseImportExcel
def average_score_template?
type == :average_score
end
def detail_score_template?
type == :detail_score
end
def read_each(&block)
average_score_template? ? read_average_score(&block) : read_detail_score(&block)
end
private
def read_average_score(&block)
block.call(sheet.row(3))
end
def read_detail_score(&block)
3.upto(sheet.last_row) do |index|
data = sheet.row(index)
next if data.all?(:blank?)
block.call(data)
end
end
def type
@_type ||= sheet.cell(1, 1)&.strip == '学号' && sheet.cell(1, 2)&.strip == '姓名' ? :detail_score : :average_score
end
def check_sheet_valid!
raise_import_error('请按照模板格式导入') if sheet.last_row.nil? || sheet.last_row < 3
raise_import_error('平均成绩只能有一行数据') if average_score_template? && sheet.last_row > 3
end
end