pdfs
This commit is contained in:
101
test-excel.js
Normal file
101
test-excel.js
Normal file
@@ -0,0 +1,101 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { execSync } = require('child_process');
|
||||
|
||||
const EXCEL_SOURCE_FILES = [
|
||||
path.join(process.cwd(), 'data/source/high-voltage.xlsx'),
|
||||
path.join(process.cwd(), 'data/source/medium-voltage-KM.xlsx'),
|
||||
path.join(process.cwd(), 'data/source/low-voltage-KM.xlsx'),
|
||||
path.join(process.cwd(), 'data/source/solar-cables.xlsx'),
|
||||
];
|
||||
|
||||
function normalizeExcelKey(value) {
|
||||
return String(value || '')
|
||||
.toUpperCase()
|
||||
.replace(/-\d+$/g, '')
|
||||
.replace(/[^A-Z0-9]+/g, '');
|
||||
}
|
||||
|
||||
function loadExcelRows(filePath) {
|
||||
const out = execSync(`npx -y xlsx-cli -j "${filePath}"`, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
||||
const trimmed = out.trim();
|
||||
const jsonStart = trimmed.indexOf('[');
|
||||
if (jsonStart < 0) return [];
|
||||
const jsonText = trimmed.slice(jsonStart);
|
||||
try {
|
||||
return JSON.parse(jsonText);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function getExcelIndex() {
|
||||
const idx = new Map();
|
||||
for (const file of EXCEL_SOURCE_FILES) {
|
||||
if (!fs.existsSync(file)) continue;
|
||||
const rows = loadExcelRows(file);
|
||||
console.log(`[excel] loaded ${rows.length} rows from ${path.relative(process.cwd(), file)}`);
|
||||
|
||||
const unitsRow = rows.find(r => r && r['Part Number'] === 'Units') || null;
|
||||
const units = {};
|
||||
if (unitsRow) {
|
||||
for (const [k, v] of Object.entries(unitsRow)) {
|
||||
if (k === 'Part Number') continue;
|
||||
const unit = String(v ?? '').trim();
|
||||
if (unit) units[k] = unit;
|
||||
}
|
||||
}
|
||||
|
||||
for (const r of rows) {
|
||||
const pn = r?.['Part Number'];
|
||||
if (!pn || pn === 'Units') continue;
|
||||
const key = normalizeExcelKey(String(pn));
|
||||
if (!key) continue;
|
||||
const cur = idx.get(key);
|
||||
if (!cur) {
|
||||
idx.set(key, { rows: [r], units });
|
||||
} else {
|
||||
cur.rows.push(r);
|
||||
if (Object.keys(cur.units).length < Object.keys(units).length) cur.units = units;
|
||||
}
|
||||
}
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
|
||||
const idx = getExcelIndex();
|
||||
console.log('\n=== Excel Index Keys ===');
|
||||
for (const [key, match] of idx.entries()) {
|
||||
console.log(`Key: ${key} | Rows: ${match.rows.length}`);
|
||||
if (match.rows.length > 0) {
|
||||
console.log(' Sample columns:', Object.keys(match.rows[0]).slice(0, 10).join(', '));
|
||||
}
|
||||
}
|
||||
|
||||
// Test specific products
|
||||
const products = JSON.parse(fs.readFileSync('data/processed/products.json', 'utf8'));
|
||||
const testProducts = ['na2xsfl2y-3', 'h1z2z2-k', 'na2xsfl2y', 'h1z2z2k'];
|
||||
|
||||
console.log('\n=== Product Lookup Tests ===');
|
||||
testProducts.forEach(slug => {
|
||||
const product = products.find(p => p.slug === slug);
|
||||
if (product) {
|
||||
const candidates = [
|
||||
product.name,
|
||||
product.slug ? product.slug.replace(/-\d+$/g, '') : '',
|
||||
product.sku,
|
||||
product.translationKey,
|
||||
].filter(Boolean);
|
||||
|
||||
const keys = candidates.map(c => normalizeExcelKey(c));
|
||||
const matches = keys.map(k => idx.get(k)).filter(Boolean);
|
||||
|
||||
console.log(`\nProduct: ${slug} (${product.name})`);
|
||||
console.log(` Candidates: ${candidates.join(', ')}`);
|
||||
console.log(` Keys: ${keys.join(', ')}`);
|
||||
console.log(` Excel matches: ${matches.length}`);
|
||||
if (matches.length > 0) {
|
||||
console.log(` Rows found: ${matches[0].rows.length}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user