Kazuhiro Yoshida ThinReports 再び 9 April 2014 at Hamamatsu.rb #38
開発の流れ1. 現状の作業をエクセルにて可能なかぎり自動化
2. それをRubyでコーディングしてコンソールアプリにする
3. さらにそれをsinatraでWEBアプリにする
目標:作業時間の短縮 15分 を 15秒に
開発イメージ
受注オーダーファイル
.xls
帳票作成プログラム
pdicard.rbthinreports
sinatra
require 'spreadsheet'!require 'sequel'!require 'thinreports'
コントローラー
pdicard_controller.rb
テンプレート
upload.haml%html! %body! %h1 PDi Card Generate! %form(method="post" enctype='multipart/form-data') ! %input(type='file' name='myfile') ! %br! %input(type='submit' value='Upload!')
require 'rubygems'!require 'sinatra'!require 'haml'!require 'pp'!require './pdicard'!require 'tempfile'!require 'sinatra/reloader'
ファイルアップロード画面とコード
%html! %body! %h1 PDi Card Generate! %form(method="post" enctype='multipart/form-data') ! %input(type='file' name='myfile') ! %br! %input(type='submit' value='Upload!')
#!/usr/bin/ruby!# encoding:utf-8!require 'spreadsheet'!require 'sequel'!require 'thinreports'!require 'pp'!!class ItemTB! DB = Sequel.sqlite! DB.create_table :items do! primary_key :id! String :item_no! String :item_name! String :code_of_customer! String :line_code_of_production! end! DataSet = DB[:items] # Create a dataset!! book = Spreadsheet.open("品目マスター.xls")!
sheet = book.worksheet(0)! sheet.each do |row|!
ThinReportエディターとコード report = ThinReports::Report.create do |r|! r.use_layout 'inspections.tlf', :default => true! r.start_new_page #:layout => 'inspections.tlf'! until (label_data.empty?) do! read_as_col(label_data, 2) do |data_row_array|! r.page.list(:list).add_row do |row|! # Set details! data_row_array.each_with_index do |col_array, _index|! row.item(Symbol_table[_index][0]).value(col_array[:item_no])! row.item(Symbol_table[_index][1]).value(col_array[:date_delivery])! row.item(Symbol_table[_index][2]).value(col_array[:time_delivery])! row.item(Symbol_table[_index][3]).value(col_array[:quantity_of_order])! row.item(Symbol_table[_index][4]).value(col_array[:quantity_of_order_sum])! row.item(Symbol_table[_index][5]).value(col_array[:inspection_as_standard])! row.item(Symbol_table[_index][6]).value(col_array[:line_code_of_customer_delivery])! row.item(Symbol_table[_index][7]).value(col_array[:line_name_of_customer_delivery])! row.item(Symbol_table[_index][8]).value(col_array[:code_of_customer])! row.item(Symbol_table[_index][9]).value(col_array[:order_no])! end! end! end! end!! r.use_layout 'inspections_list.tlf', :id => :list_all! r.start_new_page :layout => :list_all! orders.DB.fetch("select * from orders where line_code_of_production Like ? ! or line_code_of_production is null", '42%') do |data_row|! r.page.list(:list).add_row do |row|! row.item(:item_no).value(data_row[:item_no])! row.item(:date_delivery).value(data_row[:date_delivery])! row.item(:time_delivery).value(data_row[:time_delivery])! row.item(:quantity_of_order).value(data_row[:quantity_of_order])!
出力帳票とコード
r.use_layout 'inspections_list.tlf', :id => :list_all! r.start_new_page :layout => :list_all! orders.DB.fetch("select * from orders where line_code_of_production Like ? ! or line_code_of_production is null", '42%') do |data_row|! r.page.list(:list).add_row do |row|! row.item(:item_no).value(data_row[:item_no])! row.item(:date_delivery).value(data_row[:date_delivery])! row.item(:time_delivery).value(data_row[:time_delivery])! row.item(:quantity_of_order).value(data_row[:quantity_of_order])! row.item(:quantity_of_order_sum).value(data_row[:quantity_of_order_sum])! row.item(:inspection_as_standard).value(data_row[:inspection_as_standard])! row.item(:line_code_of_customer_delivery).value(data_row[:line_code_of_customer_delivery])! row.item(:line_name_of_customer_delivery).value(data_row[:line_name_of_customer_delivery])! #row.item(:code_of_customer).value(data_row[:code_of_customer])! row.item(:order_no).value(data_row[:order_no])! end! end! end! report.generate!
report = ThinReports::Report.create do |r|! r.use_layout 'inspections.tlf', :default => true! r.start_new_page #:layout => 'inspections.tlf'! until (label_data.empty?) do! read_as_col(label_data, 2) do |data_row_array|! r.page.list(:list).add_row do |row|! # Set details! data_row_array.each_with_index do |col_array, _index|! row.item(Symbol_table[_index][0]).value(col_array[:item_no])! row.item(Symbol_table[_index][1]).value(col_array[:date_delivery])! row.item(Symbol_table[_index][2]).value(col_array[:time_delivery])
結果
受注オーダー ファイル
.xls
「現状」.doc
差し込み印刷
.xls
差し込みデータ 作成
「改善後」
受注オーダー ファイル
.xls 帳票作成 プログラム
PDFファイル 出力
目標作業時間 15分 -> 15秒
﹅ ﹅ ﹅
達成﹅ ﹅