ツール管理へ戻るv3 のソース
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>是正処置記録検索</title>
<style>
:root {
color-scheme: light;
--ink: #1f2937;
--muted: #5b6573;
--line: #cbd2da;
--panel: #f4f6f8;
--accent: #145da0;
--accent-hover: #0e477b;
--danger: #a32626;
--success: #176b3a;
}
* { box-sizing: border-box; }
body {
margin: 0;
background: #eef1f4;
color: var(--ink);
font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
line-height: 1.55;
}
main {
width: min(100% - 32px, 980px);
margin: 28px auto;
padding: 24px;
background: #fff;
border: 1px solid var(--line);
border-radius: 10px;
box-shadow: 0 3px 14px rgba(20, 35, 50, .07);
}
h1 {
margin: 0 0 8px;
font-size: 1.65rem;
}
h2 {
margin: 0 0 12px;
font-size: 1.15rem;
}
h3 {
margin: 0 0 8px;
font-size: 1rem;
}
p { margin: 8px 0; }
.intro, .help, .status {
color: var(--muted);
font-size: .94rem;
}
fieldset {
margin: 18px 0;
padding: 16px;
border: 1px solid var(--line);
border-radius: 8px;
}
legend {
padding: 0 6px;
font-weight: 700;
}
.grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px 18px;
}
.field label.title {
display: block;
margin-bottom: 5px;
font-weight: 650;
}
input[type="date"],
input[type="text"],
input[type="file"] {
max-width: 100%;
padding: 9px 10px;
border: 1px solid #9ba6b2;
border-radius: 5px;
background: #fff;
color: var(--ink);
font: inherit;
}
input[type="text"] { width: 100%; }
input[type="date"] { width: 100%; }
.checks {
display: flex;
flex-wrap: wrap;
gap: 8px 18px;
margin-top: 5px;
}
.checks label, .radio-group label {
display: inline-flex;
align-items: center;
gap: 7px;
}
.radio-group {
display: flex;
flex-wrap: wrap;
gap: 10px 18px;
margin: 8px 0;
}
.buttons {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 16px 0;
}
button {
padding: 10px 16px;
border: 0;
border-radius: 5px;
background: var(--accent);
color: #fff;
font: inherit;
font-weight: 650;
cursor: pointer;
}
button:hover { background: var(--accent-hover); }
button.secondary {
border: 1px solid #8d98a4;
background: #fff;
color: var(--ink);
}
button.secondary:hover { background: #f0f2f4; }
button:disabled {
opacity: .55;
cursor: not-allowed;
}
#status {
min-height: 1.6em;
margin: 12px 0;
padding: 10px 12px;
border-radius: 5px;
background: var(--panel);
}
#status.error {
color: var(--danger);
background: #fff0f0;
}
#status.success {
color: var(--success);
background: #edf8f0;
}
#results { margin-top: 18px; }
.summary {
margin: 10px 0;
padding: 12px;
border: 1px solid var(--line);
border-radius: 6px;
background: var(--panel);
}
.result-card {
margin: 12px 0;
padding: 14px;
border: 1px solid var(--line);
border-radius: 7px;
overflow-wrap: anywhere;
}
.result-meta {
margin: 0 0 10px;
color: var(--muted);
font-size: .9rem;
}
.excerpt, .summary-output {
margin: 10px 0;
padding: 10px 12px;
border-left: 3px solid var(--accent);
background: #f6f8fa;
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.excerpt-label {
display: block;
margin-bottom: 4px;
font-weight: 700;
}
.summary-output {
border-left-color: #6c4aa1;
background: #f7f4fb;
}
.summary-output[hidden] { display: none; }
mark {
padding: 0 2px;
background: #fff1a8;
}
.no-results {
padding: 14px;
border: 1px dashed #aab3bd;
border-radius: 6px;
color: var(--muted);
}
@media (max-width: 640px) {
main {
width: min(100% - 18px, 980px);
margin: 9px auto;
padding: 16px;
}
.grid { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<main>
<h1>是正処置記録検索</h1>
<p class="intro">
選択したファイルをブラウザ内で検索します。ファイルや検索内容を外部へ送信・自動保存しません。
このページは外部AIサービスに接続せず、生成AIによる分析・要約は行いません。
</p>
<form id="searchForm">
<fieldset>
<legend>検索対象ファイル</legend>
<div class="field">
<label class="title" for="files">記録ファイル(複数選択可)</label>
<input id="files" type="file" accept=".csv,.txt,.xlsx,.xlsm,.xls" multiple>
<p class="help">
このページで内容を読み取れるのはCSV・TXTです。Excel形式(.xlsx/.xlsm/.xls)は
ブラウザ標準機能だけではセル内容を解析できないため、ファイル名のみ検索します。
</p>
</div>
</fieldset>
<fieldset>
<legend>対象期間・部門</legend>
<div class="grid">
<div class="field">
<label class="title" for="startDate">開始日</label>
<input id="startDate" type="date">
</div>
<div class="field">
<label class="title" for="endDate">終了日</label>
<input id="endDate" type="date">
</div>
</div>
<div class="field" style="margin-top: 12px">
<span class="title"><strong>対象部門</strong></span>
<div class="checks">
<label><input type="checkbox" name="department" value="NC加工部門" checked> NC加工部門</label>
<label><input type="checkbox" name="department" value="時計製造部門" checked> 時計製造部門</label>
</div>
</div>
<p class="help">
期間指定時はファイル名またはCSV・TXT内の日付から判定します。
対応する日付表記は YYYY-MM-DD、YYYY/MM/DD、YYYY年M月D日 です。
</p>
</fieldset>
<fieldset>
<legend>検索条件</legend>
<div class="field">
<label class="title" for="incident">検索したい不具合事象</label>
<input id="incident" type="text" placeholder="例:加工面に傷が発生">
</div>
<div class="field" style="margin-top: 12px">
<label class="title" for="keywords">追加キーワード(複数語は空白区切り)</label>
<input id="keywords" type="text" placeholder="例:異音 軸受">
</div>
<div style="margin-top: 12px">
<strong>複数キーワードの検索方式</strong>
<div class="radio-group">
<label><input type="radio" name="matchMode" value="AND" checked> AND(すべての語を含む)</label>
<label><input type="radio" name="matchMode" value="OR"> OR(いずれかの語を含む)</label>
</div>
</div>
</fieldset>
<div class="buttons">
<button type="submit">検索</button>
<button id="clearButton" class="secondary" type="button">条件をクリア</button>
</div>
</form>
<div id="status" class="status" role="status" aria-live="polite">
検索対象ファイルを選択してください。
</div>
<section id="results" aria-live="polite">
<h2>検索結果</h2>
<p class="no-results">まだ検索していません。</p>
</section>
</main>
<script>
"use strict";
const form = document.getElementById("searchForm");
const filesInput = document.getElementById("files");
const statusBox = document.getElementById("status");
const resultsBox = document.getElementById("results");
const clearButton = document.getElementById("clearButton");
function setStatus(message, type) {
statusBox.textContent = message;
statusBox.className = "status" + (type ? " " + type : "");
}
function escapeHtml(value) {
return String(value).replace(/[&<>"']/g, function (character) {
return {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
}[character];
});
}
function selectedDepartments() {
return Array.from(document.querySelectorAll('input[name="department"]:checked'))
.map(function (input) { return input.value; });
}
function normalize(text) {
return String(text || "").toLocaleLowerCase("ja");
}
function findDate(text) {
const patterns = [
/(\d{4})[-.\/](\d{1,2})[-.\/](\d{1,2})/,
/(\d{4})年(\d{1,2})月(\d{1,2})日/
];
for (const pattern of patterns) {
const match = String(text).match(pattern);
if (!match) continue;
const year = Number(match[1]);
const month = Number(match[2]);
const day = Number(match[3]);
const date = new Date(year, month - 1, day);
if (
date.getFullYear() === year &&
date.getMonth() === month - 1 &&
date.getDate() === day
) {
return year + "-" + String(month).padStart(2, "0") + "-" +
String(day).padStart(2, "0");
}
}
return null;
}
function getDepartment(filename) {
const departments = ["NC加工部門", "時計製造部門"];
return departments.find(function (name) {
return filename.includes(name);
}) || "部門不明";
}
function wordMatch(text, terms, mode) {
if (!terms.length) return true;
const normalizedText = normalize(text);
const results = terms.map(function (term) {
return normalizedText.includes(normalize(term));
});
return mode === "OR" ? results.some(Boolean) : results.every(Boolean);
}
function splitSentences(text) {
return String(text || "")
.replace(/\r/g, "\n")
.split(/[\n。!?!?;;]+/)
.map(function (part) { return part.trim(); })
.filter(Boolean);
}
function parseCsvLine(line) {
const cells = [];
let cell = "";
let quoted = false;
for (let i = 0; i < line.length; i++) {
const character = line[i];
if (character === '"') {
if (quoted && line[i + 1] === '"') {
cell += '"';
i++;
} else {
quoted = !quoted;
}
} else if (character === "," && !quoted) {
cells.push(cell);
cell = "";
} else {
cell += character;
}
}
cells.push(cell);
return cells;
}
function parseCsv(text) {
const lines = String(text).replace(/^\uFEFF/, "").split(/\r?\n/)
.filter(function (line) { return line.trim() !== ""; });
if (!lines.length) return { headers: [], rows: [] };
return {
headers: parseCsvLine(lines[0]),
rows: lines.slice(1).map(parseCsvLine)
};
}
function findColumn(headers, patterns) {
return headers.findIndex(function (header) {
return patterns.some(function (pattern) {
return pattern.test(String(header || "").trim());
});
});
}
function getRecordFields(file, text) {
const extension = file.name.toLowerCase().split(".").pop();
const result = {
readable: false,
cause: "",
action: "",
allText: file.name,
date: findDate(file.name)
};
if (extension === "csv") {
result.readable = true;
const parsed = parseCsv(text);
const causeIndex = findColumn(parsed.headers, [/原因/, /発生要因/, /真因/]);
const actionIndex = findColumn(parsed.headers, [/対策/, /処置/, /再発防止/]);
const dateIndex = findColumn(parsed.headers, [/日付/, /発生日/, /処置日/, /記録日/]);
const allValues = [file.name];
const causes = [];
const actions = [];
parsed.rows.forEach(function (row) {
allValues.push(row.join(" "));
if (causeIndex >= 0 && row[causeIndex]) causes.push(row[causeIndex]);
if (actionIndex >= 0 && row[actionIndex]) actions.push(row[actionIndex]);
if (!result.date && dateIndex >= 0 && row[dateIndex]) {
result.date = findDate(row[dateIndex]);
}
});
result.cause = causes.join("\n");
result.action = actions.join("\n");
result.allText = allValues.join("\n");
return result;
}
if (extension === "txt") {
result.readable = true;
result.allText = file.name + "\n" + text;
result.date = result.date || findDate(text);
const causes = [];
const actions = [];
let currentSection = "";
text.split(/\r?\n/).forEach(function (line) {
const trimmed = line.trim();
if (!trimmed) return;
if (/原因|発生要因|真因/.test(trimmed)) {
currentSection = "cause";
causes.push(trimmed.replace(/^(不具合の)?原因[::\s]*/, ""));
} else if (/対策|処置|再発防止/.test(trimmed)) {
currentSection = "action";
actions.push(trimmed.replace(/^(不具合に対する)?対策[::\s]*/, ""));
} else if (currentSection === "cause") {
causes.push(trimmed);
} else if (currentSection === "action") {
actions.push(trimmed);
}
});
result.cause = causes.join("\n");
result.action = actions.join("\n");
}
return result;
}
function highlight(text, terms) {
const value = String(text || "");
if (!terms.length) return escapeHtml(value);
const sorted = terms.slice().sort(function (a, b) {
return b.length - a.length;
});
const escapedTerms = sorted.map(function (term) {
return term.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
});
const pattern = new RegExp("(" + escapedTerms.join("|") + ")", "giu");
return value.split(pattern).map(function (part) {
const matched = sorted.some(function (term) {
return normalize(part) === normalize(term);
});
return matched ? "<mark>" + escapeHtml(part) + "</mark>" : escapeHtml(part);
}).join("");
}
function relevantExcerpts(text, terms) {
const sentences = splitSentences(text);
if (!sentences.length) return [];
if (!terms.length) return sentences.slice(0, 5);
return sentences.filter(function (sentence) {
return terms.some(function (term) {
return normalize(sentence).includes(normalize(term));
});
}).slice(0, 5);
}
function makeExcerptBox(label, text, terms, readable) {
const box = document.createElement("div");
box.className = "excerpt";
const heading = document.createElement("span");
heading.className = "excerpt-label";
heading.textContent = label;
box.appendChild(heading);
const excerpts = relevantExcerpts(text, terms);
if (excerpts.length) {
excerpts.forEach(function (excerpt) {
const line = document.createElement("div");
line.innerHTML = highlight(excerpt, terms);
box.appendChild(line);
});
} else {
const message = document.createElement("span");
message.textContent = readable
? "キーワードに一致する文章は見つかりませんでした。"
: "ファイル内容を読み取れないため抽出できません。";
box.appendChild(message);
}
return box;
}
function makeLocalSummary(record, terms) {
const causes = relevantExcerpts(record.cause, terms);
const actions = relevantExcerpts(record.action, terms);
const parts = [];
if (causes.length) {
parts.push("原因:" + causes.join("。"));
}
if (actions.length) {
parts.push("対策:" + actions.join("。"));
}
if (!record.readable) {
return "このファイル形式の内容は読み取れないため、要約できません。";
}
if (!parts.length) {
return "検索語に一致する原因・対策の文章が見つからず、整理できる情報がありません。";
}
return parts.join("\n\n") +
"\n\n※これはAIによる要約ではなく、検索語に一致した文章を項目別に並べたものです。";
}
function renderResults(records, terms) {
resultsBox.innerHTML = "<h2>検索結果</h2>";
const summary = document.createElement("div");
summary.className = "summary";
summary.textContent = "該当件数:" + records.length + " 件";
resultsBox.appendChild(summary);
if (!records.length) {
const empty = document.createElement("p");
empty.className = "no-results";
empty.textContent = "条件に一致する記録はありません。";
resultsBox.appendChild(empty);
return;
}
records.forEach(function (record, index) {
const card = document.createElement("article");
card.className = "result-card";
const title = document.createElement("h3");
title.innerHTML = highlight(record.name, terms);
card.appendChild(title);
const meta = document.createElement("p");
meta.className = "result-meta";
meta.textContent =
"部門:" + record.department +
" / 日付:" + (record.date || "見つかりません") +
" / ファイル内容を読み取れる形式:" + (record.readable ? "はい" : "いいえ");
card.appendChild(meta);
card.appendChild(makeExcerptBox("不具合の原因", record.cause, terms, record.readable));
card.appendChild(makeExcerptBox("不具合に対する対策", record.action, terms, record.readable));
const summarizeButton = document.createElement("button");
summarizeButton.type = "button";
summarizeButton.className = "summary-button";
summarizeButton.textContent = "AIによる要約";
summarizeButton.setAttribute("aria-expanded", "false");
summarizeButton.setAttribute("aria-controls", "summary-" + index);
const output = document.createElement("div");
output.id = "summary-" + index;
output.className = "summary-output";
output.hidden = true;
output.setAttribute("role", "region");
output.setAttribute("aria-label", "要約表示欄");
summarizeButton.addEventListener("click", function () {
const willShow = output.hidden;
if (willShow) {
output.textContent = makeLocalSummary(record, terms);
output.hidden = false;
summarizeButton.setAttribute("aria-expanded", "true");
} else {
output.hidden = true;
summarizeButton.setAttribute("aria-expanded", "false");
}
});
card.appendChild(summarizeButton);
card.appendChild(output);
const note = document.createElement("p");
note.className = "help";
note.textContent =
"ボタン押下時に表示するのは、検索語に一致した文章を原因・対策別に並べたものです。" +
"生成AIによる分析や文章の言い換えは行いません。";
card.appendChild(note);
resultsBox.appendChild(card);
});
}
form.addEventListener("submit", async function (event) {
event.preventDefault();
const files = Array.from(filesInput.files || []);
if (!files.length) {
setStatus("検索対象のファイルを選択してください。", "error");
return;
}
const startDate = document.getElementById("startDate").value;
const endDate = document.getElementById("endDate").value;
if (startDate && endDate && startDate > endDate) {
setStatus("開始日は終了日以前の日付を指定してください。", "error");
return;
}
const departments = selectedDepartments();
if (!departments.length) {
setStatus("対象部門を1つ以上選択してください。", "error");
return;
}
const incident = document.getElementById("incident").value.trim();
const additional = document.getElementById("keywords").value
.trim().split(/\s+/).filter(Boolean);
const terms = [];
if (incident) terms.push(incident);
additional.forEach(function (word) {
if (!terms.some(function (existing) {
return normalize(existing) === normalize(word);
})) {
terms.push(word);
}
});
const mode = document.querySelector('input[name="matchMode"]:checked').value;
const records = [];
setStatus("選択したファイルをブラウザ内で確認しています。", "");
clearButton.disabled = true;
try {
for (const file of files) {
const department = getDepartment(file.name);
if (!departments.includes(department)) continue;
const extension = file.name.toLowerCase().split(".").pop();
let text = "";
if (extension === "csv" || extension === "txt") {
text = await file.text();
}
const fields = getRecordFields(file, text);
const date = fields.date || findDate(file.name);
if ((startDate || endDate) && !date) continue;
if (startDate && date < startDate) continue;
if (endDate && date > endDate) continue;
const searchable = fields.readable
? fields.allText + "\n" + fields.cause + "\n" + fields.action
: file.name;
if (!wordMatch(searchable, terms, mode)) continue;
records.push({
name: file.name,
department: department,
date: date,
readable: fields.readable,
cause: fields.cause,
action: fields.action
});
}
renderResults(records, terms);
setStatus(
"検索完了:" + records.length +
" 件。CSV・TXTでは原因・対策文章を抽出します。Excel内部の解析と生成AIによる要約は行いません。",
"success"
);
} catch (error) {
setStatus(
"ファイルを読み取れませんでした。CSV/TXTの文字コードや内容を確認してください。",
"error"
);
} finally {
clearButton.disabled = false;
}
});
clearButton.addEventListener("click", function () {
form.reset();
resultsBox.innerHTML =
"<h2>検索結果</h2><p class=\"no-results\">まだ検索していません。</p>";
setStatus("検索条件をクリアしました。", "");
});
</script>
</body>
</html>