仕組みを Web アプリケーションにする
仕組みができたとして、それを毎回どうやって動かすのか。
ファイルをダブルクリックしたら画面が出て、データを入れてボタンを押せば結果が出る。 それが Web アプリケーションです。
アプリケーションは 3 つの部分でできている
Section titled “アプリケーションは 3 つの部分でできている”難しそうな言葉ですが、中身は 3 つだけです。
| 何をするところか | 座席表でいうと | |
|---|---|---|
| 入力 | データや条件を受け取る | 名簿の CSV を選ぶボタン |
| 処理 | 受け取ったものをルールどおりに扱う | 番号の順に、クラスごとに人を座らせる部分 |
| 出力 | 結果を見せる・保存する | 並んだ座席表と「まとめて PNG で保存」 |
座席表もシフト表も、この形をしています。業務ツールはほぼ全部この形です。
下は交通費を集計するアプリの画面です。3 つのパネルが「入力」「処理」「出力」に そのまま対応しています。
左に明細を貼って「集計する」を押すと、真ん中の「処理」に何をやっているかが順番に出て、 右に結果が出ます。

注目してほしいところ
Section titled “注目してほしいところ”真ん中のパネルです。ふつうのアプリでは、処理の中身は見えません。 ボタンを押したら結果が出るだけです。
ここでは、わざと中で何をやっているかを表示しています。
- 明細を 1 行ずつに分ける(10 行)
- 日付・区間・金額に切り分ける(10 件を読み取り)
- 日付ごとにまとめる(5 日分)
- 合計を出す(3,807 円)
これが「仕組み」の正体です。 特別なことは何もしていません。 人間が手でやっている手順を、そのまま順番に並べただけです。
読み取れなかった行を出す
Section titled “読み取れなかった行を出す”もうひとつ、地味ですが大事な部分があります。
明細の中に、こういう行を足してみてください。
10/25 打ち合わせ交通費(領収書なし)金額が読み取れないので、「読み取れませんでした」と出ます。 黙って無視はしません。
これは意図した動きです。集計から勝手に消えるのがいちばん危ない。 合計が合わないことに、誰も気づけないからです。
保存も自分の PC の中で終わる
Section titled “保存も自分の PC の中で終わる”「CSV で保存」を押すと、ファイルがダウンロードされます。
このとき通信は一切起きていません。 どこかのサーバーに送って、加工してもらって、 返してもらっているわけではありません。ブラウザがその場でファイルを組み立てて、 その場で保存しています。
この点は次の節で詳しく見ます。
この節を終えたときの完成例のファイルです。Codex が作ったファイルと、書き方や細かいところが違っていて構いません。
app
<!doctype html><html lang="ja"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>交通費の集計</title> <link rel="stylesheet" href="style.css" /> <script src="main.js" defer></script> </head> <body> <h1>交通費の集計</h1> <p class="lead"> 入力 → 処理 → 出力。Web アプリケーションは、この 3 つを画面から操作できるようにしたものです。 </p>
<div class="pipeline"> <section class="stage"> <h2><span class="badge">入力</span>明細を貼る</h2> <p class="hint">1 行に 1 件。<code>日付 区間 金額</code> の順に、空白区切りで書きます。</p> <textarea id="entries" rows="12" spellcheck="false"></textarea> <button id="run" type="button">集計する</button> </section>
<section class="stage"> <h2><span class="badge">処理</span>やっていること</h2> <p class="hint">ボタンを押すと、上から順にこれが実行されます。</p> <ol id="steps" class="steps"></ol> </section>
<section class="stage"> <h2><span class="badge">出力</span>結果</h2> <table class="result"> <thead> <tr><th>日付</th><th>件数</th><th class="num">金額</th></tr> </thead> <tbody id="rows"></tbody> <tfoot> <tr><th colspan="2">合計</th><th class="num" id="total">0 円</th></tr> </tfoot> </table> <ul id="problems" class="problems"></ul> <button id="download" type="button" class="secondary">CSV で保存</button> </section> </div> </body></html>// 交通費の集計 - 「入力 → 処理 → 出力」を画面から操作できる形にした最小の例。//// ブラウザだけで動く。貼り付けた明細はこのページの中だけで処理され、どこにも送られない。
const sampleEntries = [ '10/1 新宿→品川 199', '10/1 品川→新宿 199', '10/3 新宿→横浜 483', '10/3 横浜→新宿 483', '10/8 新宿→大宮 570', '10/8 大宮→新宿 570', '10/8 大宮→浦和 157', '10/15 新宿→品川 199', '10/15 品川→新宿 199', '10/22 新宿→千葉 748',].join('\n');
const entriesInput = document.querySelector('#entries');const stepsList = document.querySelector('#steps');const rows = document.querySelector('#rows');const totalCell = document.querySelector('#total');const problemsList = document.querySelector('#problems');const runButton = document.querySelector('#run');const downloadButton = document.querySelector('#download');
entriesInput.value = sampleEntries;
// 出力した結果を CSV にするときに使い回すので、最後の集計結果を覚えておく。let lastResult = [];
function addStep(text, detail) { const item = document.createElement('li'); item.textContent = text; if (detail) { const span = document.createElement('span'); span.className = 'detail'; span.textContent = detail; item.append(span); } stepsList.append(item);}
function run() { stepsList.textContent = ''; problemsList.textContent = ''; rows.textContent = '';
// ── 手順 1: 1 行ずつに分ける const lines = entriesInput.value .split('\n') .map((line) => line.trim()) .filter((line) => line !== ''); addStep('明細を 1 行ずつに分ける', `${lines.length} 行`);
// ── 手順 2: 日付・区間・金額に切り分ける const entries = []; const problems = [];
for (const line of lines) { const parts = line.split(/\s+/); const amount = Number(parts[parts.length - 1]);
// 金額が数字として読めない行は、黙って捨てずに後で見せる。 if (!Number.isFinite(amount) || parts.length < 3) { problems.push(line); continue; }
entries.push({ date: parts[0], route: parts.slice(1, -1).join(' '), amount }); } addStep('日付・区間・金額に切り分ける', `${entries.length} 件を読み取り`);
// ── 手順 3: 日付ごとにまとめる const byDate = new Map(); for (const entry of entries) { const current = byDate.get(entry.date) || { count: 0, amount: 0 }; current.count += 1; current.amount += entry.amount; byDate.set(entry.date, current); } addStep('日付ごとにまとめる', `${byDate.size} 日分`);
// ── 手順 4: 合計する const total = entries.reduce((sum, entry) => sum + entry.amount, 0); addStep('合計を出す', `${total.toLocaleString()} 円`);
// ── 出力 lastResult = [...byDate.entries()].map(([date, value]) => ({ date, ...value }));
for (const row of lastResult) { const tr = document.createElement('tr'); const dateCell = document.createElement('td'); dateCell.textContent = row.date; const countCell = document.createElement('td'); countCell.textContent = `${row.count} 件`; const amountCell = document.createElement('td'); amountCell.className = 'num'; amountCell.textContent = `${row.amount.toLocaleString()} 円`; tr.append(dateCell, countCell, amountCell); rows.append(tr); }
totalCell.textContent = `${total.toLocaleString()} 円`;
// 読み取れなかった行は必ず見せる。黙って減らすのが一番こわい。 for (const line of problems) { const item = document.createElement('li'); item.textContent = `読み取れませんでした: ${line}`; problemsList.append(item); }}
function download() { if (lastResult.length === 0) return;
const header = '日付,件数,金額\n'; const body = lastResult.map((row) => `${row.date},${row.count},${row.amount}`).join('\n');
// Blob はブラウザの中だけで作られるファイルのようなもの。 // サーバーに送ってから受け取るのではなく、この場で作ってこの場で保存する。 const blob = new Blob([header + body], { type: 'text/csv;charset=utf-8' }); const url = URL.createObjectURL(blob);
const link = document.createElement('a'); link.href = url; link.download = '交通費集計.csv'; link.click();
URL.revokeObjectURL(url);}
runButton.addEventListener('click', run);downloadButton.addEventListener('click', download);
run();/* 入力 → 処理 → 出力 の 3 段を横に並べる。 */:root { color-scheme: light;}
body { margin: 0; padding: 24px 16px 48px; font-family: system-ui, -apple-system, 'Segoe UI', sans-serif; line-height: 1.7; color: #1f2933; background: #f7f8fa;}
h1 { margin: 0 0 4px; font-size: 1.4rem;}
.lead { margin: 0 0 24px; font-size: 0.85rem; color: #616e7c;}
.pipeline { display: flex; flex-wrap: wrap; gap: 20px; align-items: flex-start;}
.stage { flex: 1 1 280px; min-width: 0; padding: 16px; background: #fff; border: 1px solid #d8dee6; border-radius: 10px;}
h2 { display: flex; gap: 8px; align-items: center; margin: 0 0 6px; font-size: 0.95rem;}
.badge { padding: 2px 8px; font-size: 0.7rem; font-weight: 600; color: #fff; background: #2b6cb0; border-radius: 999px;}
.hint { margin: 0 0 8px; font-size: 0.78rem; color: #616e7c;}
.hint code { padding: 1px 4px; background: #eef1f5; border-radius: 3px;}
textarea { display: block; width: 100%; box-sizing: border-box; padding: 8px; font-family: ui-monospace, Consolas, monospace; font-size: 0.78rem; line-height: 1.7; background: #fbfcfd; border: 1px solid #cbd2d9; border-radius: 6px; resize: vertical;}
button { margin-top: 12px; padding: 8px 20px; font-family: inherit; font-size: 0.85rem; color: #fff; background: #2b6cb0; border: none; border-radius: 6px; cursor: pointer;}
button.secondary { color: #2b6cb0; background: #fff; border: 1px solid #2b6cb0;}
/* 処理の手順。実際に何件さばいたかを右に出して、動いている実感が出るようにする。 */.steps { margin: 0; padding-left: 20px; font-size: 0.82rem;}
.steps li { padding: 4px 0;}
.detail { margin-left: 8px; padding: 1px 7px; font-size: 0.72rem; color: #2f855a; background: #e6f6ec; border-radius: 999px;}
.result { width: 100%; font-size: 0.82rem; border-collapse: collapse;}
.result th,.result td { padding: 5px 8px; text-align: left; border-bottom: 1px solid #e3e8ee;}
.result thead th { font-size: 0.72rem; color: #616e7c;}
.result tfoot th { font-size: 0.85rem; border-top: 2px solid #cbd2d9; border-bottom: none;}
.num { text-align: right;}
.problems { margin: 10px 0 0; padding-left: 18px; font-size: 0.75rem; color: #a4303f;}