コンテンツにスキップ

実際に小さなツールを作ってみる

ひな形を土台に、自分の業務用のツールへ育てていく様子を表した図

40 分あります。作ります。

前の節で Codex にまとめてもらった仕様を、 そのまま Work モードに貼ります。

最後に、この 2 行を足してください。

・index.html と main.js と style.css の3つのファイルにしてください
・最初からサンプルのデータが入っていて、開いた時点で結果が見える状態にしてください

2 行目が効きます。空っぽの画面が出てくると、動いているのかどうか分からないからです。

B. ひな形から始める(おすすめ)

Section titled “B. ひな形から始める(おすすめ)”

このページの下にあるひな形をダウンロードして、それを土台にします。

ひな形には「入力 → 処理 → 出力」の骨組みが入っていて、書き換えるのは処理の部分だけに なるよう作ってあります。

  1. ページ下部の「ひな形をダウンロード」から ZIP を落とす
  2. 解凍してできた app フォルダを、作業フォルダの中に置く
  3. Codex(Work モード)に次のように頼む
app フォルダの中にあるひな形をもとに、(作りたいもの)を作ってください。
main.js の process という関数の中だけを書き換えれば済むように作ってあります。
できるだけそこだけの変更にしてください。
・入力の形式:(    )
・やってほしい処理:(    )
・出力してほしいもの:(    )
通信は一切しないでください。

B の方が速いです。 形が決まっているので、Codex が迷いません。

座席表と同じ進め方です。全部の条件を最初に伝えないでください。

  1. まず、いちばん単純な形で動かす
  2. 動いたら、条件を 1 つ足す
  3. また動かす

一度に 10 個の条件を伝えると、どれが原因で動かないのか分からなくなります。

一度に全部頼む場合と、1つずつ足す場合の進み方の違いを表した図

動かなかったら、そのまま伝える

Section titled “動かなかったら、そのまま伝える”

エラーの文章が出たら、それをコピーしてそのまま貼ってください。 説明し直す必要はありません。

開いたら、画面に次のように出ました。直してください。
(エラーの文章を貼る)

何も表示されないときは、F12 を押して Console タブを見てください。 赤い文字が出ていたら、それをコピーして貼ります。

作っている間は、架空のデータで進めてください。

Codex はフォルダの中を読みます。作業フォルダに実データを置いてあると、 「参考にしますね」と読まれる可能性があります。

実データを入れるのは、できあがってからです。

座席表とシフト表で共通していたことを、自分のツールにも入れてください。

・処理できなかった行は、捨てずに画面に出してください
・条件を満たせたかどうかを画面に出してください

この 2 行を足すかどうかで、使えるツールかどうかが決まります。

動いたら、必ずこの 3 つを確認してください。

  1. F12 → Network タブで、通信が発生していないこと (データの行き先を確認する でやった手順)
  2. アドレスバーが file:/// で始まっていること
  3. わざと変なデータを入れても、画面が壊れないこと

3 つめは試してみてください。空っぽの入力、全角の数字、1 行だけ違う形式。 自分以外の人に渡すつもりなら、ここが効いてきます。

  • 実データを入れて、結果が正しいか確かめる
  • 同僚に渡して使ってもらう(HTML ファイルを送るだけ)
  • 条件をもう 1 つ足す

そのまま持ち帰ってください。 Codex は今日のやりとりを覚えています。 明日続きから頼めます。

作業フォルダごと保存しておけば、家でも会社でも続けられます。

「入力 → 処理 → 出力」の形だけが入ったひな形です。ダウンロードして解凍し、index.html を ダブルクリックすると次の画面が開きます。データを書き換えて「実行する」を押してみてください。

ひな形の画面。左が入力のデータと条件、右が処理の手順と結果

ひな形の中身です。Codex に書き換えてもらう process という関数は main.js にあります。

ひな形
<!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 id="title">ひな形</h1>
<p class="lead">
入力 → 処理 → 出力 の形だけが入っています。ここを土台に、自分の業務のツールへ育ててください。
</p>
<div class="layout">
<section class="panel">
<h2><span class="badge">入力</span>データ</h2>
<p class="hint" id="data-hint">1 行に 1 件。自分の業務のデータに合わせて書き換えます。</p>
<textarea id="data" rows="10" spellcheck="false"></textarea>
<h2 class="spaced"><span class="badge">入力</span>条件</h2>
<p class="hint">毎回変わる設定を書く欄です。使わないなら消してかまいません。</p>
<textarea id="rules" rows="3" spellcheck="false"></textarea>
<button id="run" type="button">実行する</button>
</section>
<section class="panel">
<h2><span class="badge">処理</span>やっていること</h2>
<ol id="steps" class="steps"></ol>
<h2 class="spaced"><span class="badge">出力</span>結果</h2>
<table class="result">
<thead id="head"></thead>
<tbody id="rows"></tbody>
</table>
<ul id="problems" class="problems"></ul>
<button id="download" type="button" class="secondary">CSV で保存</button>
</section>
</div>
</body>
</html>
// 自分の業務ツールを作るためのひな形。
//
// 「入力 → 処理 → 出力」の骨組みだけが入っている。
// このファイルごと Codex に渡して、「これをもとに〜を作って」と頼むのが速い。
//
// ブラウザだけで動く。通信は一切しない。この性質は必ず保つこと
// (Codex に頼むときも「通信しないでください」と毎回書く)。
// ---------------------------------------------------------------------------
// 最初から入れておくサンプル。自分の業務のデータに置き換える。
// ---------------------------------------------------------------------------
const sampleData = [
'10/01 営業部 備品購入 4800',
'10/03 総務部 会議室利用 0',
'10/08 営業部 名刺作成 3200',
'10/12 開発部 書籍購入 5600',
'10/15 営業部 交通費 1980',
'10/22 総務部 備品購入 2400',
].join('\n');
const sampleRules = ['並び順 金額'].join('\n');
const dataInput = document.querySelector('#data');
const rulesInput = document.querySelector('#rules');
const stepsList = document.querySelector('#steps');
const head = document.querySelector('#head');
const rows = document.querySelector('#rows');
const problemsList = document.querySelector('#problems');
const runButton = document.querySelector('#run');
const downloadButton = document.querySelector('#download');
dataInput.value = sampleData;
rulesInput.value = sampleRules;
// CSV に書き出すときに使い回すので、最後の結果を覚えておく。
let lastColumns = [];
let lastRows = [];
function readLines(textarea) {
return textarea.value
.split('\n')
.map((line) => line.trim())
.filter((line) => line !== '');
}
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);
}
// ---------------------------------------------------------------------------
// ▼ ここから下が「処理」。自分の業務に合わせて書き換えるのはこの関数だけ。
//
// いまの中身: 「日付 部署 内容 金額」の行を読んで、部署ごとに合計する。
//
// Codex に頼むときは、この関数が何をすべきかを日本語で説明する。
// 例:「process の中を、部署ごとではなく月ごとの合計を出すように変えてください」
// ---------------------------------------------------------------------------
function process(lines, rules) {
const problems = [];
const totals = new Map();
for (const line of lines) {
const parts = line.split(/\s+/);
const amount = Number(parts[parts.length - 1]);
// 読み取れない行は捨てずに集めておく。黙って減らさないのが大事。
if (parts.length < 4 || !Number.isFinite(amount)) {
problems.push(line);
continue;
}
const department = parts[1];
const current = totals.get(department) || { count: 0, amount: 0 };
current.count += 1;
current.amount += amount;
totals.set(department, current);
}
addStep('部署ごとにまとめる', `${totals.size} 部署`);
let result = [...totals.entries()].map(([department, value]) => ({
部署: department,
件数: value.count,
金額: value.amount,
}));
// 条件欄の「並び順」を見て並べ替える。条件の使い方の例として置いてある。
if (rules.includes('並び順 金額')) {
result.sort((one, other) => other.金額 - one.金額);
addStep('金額の多い順に並べる');
}
return { columns: ['部署', '件数', '金額'], rows: result, problems };
}
// ---------------------------------------------------------------------------
// ▲ 書き換えるのはここまで。下は表示と保存なので、そのままで使えることが多い。
// ---------------------------------------------------------------------------
function run() {
stepsList.textContent = '';
head.textContent = '';
rows.textContent = '';
problemsList.textContent = '';
const lines = readLines(dataInput);
addStep('データを 1 行ずつに分ける', `${lines.length} 行`);
const rules = readLines(rulesInput);
const { columns, rows: resultRows, problems } = process(lines, rules);
lastColumns = columns;
lastRows = resultRows;
const headerRow = document.createElement('tr');
for (const column of columns) {
const th = document.createElement('th');
th.textContent = column;
headerRow.append(th);
}
head.append(headerRow);
for (const row of resultRows) {
const tr = document.createElement('tr');
for (const column of columns) {
const td = document.createElement('td');
const value = row[column];
td.textContent = typeof value === 'number' ? value.toLocaleString() : String(value ?? '');
tr.append(td);
}
rows.append(tr);
}
for (const line of problems) {
const item = document.createElement('li');
item.textContent = `読み取れませんでした: ${line}`;
problemsList.append(item);
}
}
function download() {
if (lastRows.length === 0) return;
const header = lastColumns.join(',');
const body = lastRows.map((row) => lastColumns.map((column) => row[column]).join(',')).join('\n');
// ブラウザの中でファイルを組み立てて、その場で保存する。サーバーは関わらない。
const blob = new Blob([`${header}\n${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();
: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.82rem;
color: #616e7c;
}
h2 {
display: flex;
gap: 8px;
align-items: center;
margin: 0 0 6px;
font-size: 0.95rem;
color: #3e4c59;
}
.spaced {
margin-top: 20px;
}
.badge {
padding: 2px 8px;
font-size: 0.7rem;
font-weight: 600;
color: #fff;
background: #2b6cb0;
border-radius: 999px;
}
.layout {
display: flex;
flex-wrap: wrap;
gap: 20px;
align-items: flex-start;
}
.panel {
flex: 1 1 320px;
min-width: 0;
padding: 16px;
background: #fff;
border: 1px solid #d8dee6;
border-radius: 10px;
}
.hint {
margin: 0 0 8px;
font-size: 0.78rem;
color: #616e7c;
}
textarea {
display: block;
width: 100%;
box-sizing: border-box;
padding: 8px;
font-family: ui-monospace, Consolas, monospace;
font-size: 0.8rem;
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.85rem;
border-collapse: collapse;
}
.result th,
.result td {
padding: 6px 8px;
text-align: left;
border-bottom: 1px solid #e3e8ee;
}
.result thead th {
font-size: 0.72rem;
color: #616e7c;
background: #f4f6f8;
}
.problems {
margin: 10px 0 0;
padding-left: 18px;
font-size: 0.75rem;
color: #a4303f;
}
ひな形をダウンロード