76 lines
2.3 KiB
JavaScript
76 lines
2.3 KiB
JavaScript
'use strict';
|
||
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const XLSX = require('xlsx');
|
||
|
||
function normalizeText(value) {
|
||
return String(value ?? '').trim();
|
||
}
|
||
|
||
function splitSourceValues(value) {
|
||
return normalizeText(value)
|
||
.split(/[\/、,,;;]+/u)
|
||
.map((item) => item.trim())
|
||
.filter(Boolean);
|
||
}
|
||
|
||
function parseSheet(workbook, sheetName) {
|
||
const rows = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], {
|
||
header: 1,
|
||
raw: false,
|
||
defval: '',
|
||
});
|
||
const propertyMap = new Map();
|
||
let categoryName = normalizeText(sheetName);
|
||
let currentPddProperty = '';
|
||
let currentSourceProperty = '';
|
||
|
||
for (const rawRow of rows.slice(1)) {
|
||
const row = rawRow.map(normalizeText);
|
||
categoryName = row[0] || categoryName;
|
||
currentPddProperty = row[1] || currentPddProperty;
|
||
currentSourceProperty = row[3] || currentSourceProperty;
|
||
if (!currentPddProperty) continue;
|
||
|
||
const property = propertyMap.get(currentPddProperty) || {
|
||
pddProperty: currentPddProperty,
|
||
sourceProperty: currentSourceProperty || '无',
|
||
valueMappings: [],
|
||
};
|
||
if (row[1]) property.sourceProperty = currentSourceProperty || '无';
|
||
|
||
const pddValue = row[2];
|
||
const sourceValues = splitSourceValues(row[4]);
|
||
if (pddValue && sourceValues.length > 0 && !sourceValues.includes('无')) {
|
||
property.valueMappings.push({ pddValue, sourceValues });
|
||
}
|
||
propertyMap.set(currentPddProperty, property);
|
||
}
|
||
|
||
return {
|
||
categoryName,
|
||
propertyMappings: Array.from(propertyMap.values()),
|
||
};
|
||
}
|
||
|
||
function main() {
|
||
const inputPath = path.resolve(process.argv[2] || '');
|
||
const outputPath = path.resolve(
|
||
process.argv[3] || path.join(__dirname, '../src/data/pdd1688PropertyMappings.json')
|
||
);
|
||
if (!inputPath || !fs.existsSync(inputPath)) {
|
||
throw new Error('请传入存在的“拼多多-1688属性对应.xlsx”路径');
|
||
}
|
||
const workbook = XLSX.readFile(inputPath, { cellDates: true });
|
||
const output = {
|
||
sourceFile: path.basename(inputPath),
|
||
categories: workbook.SheetNames.map((sheetName) => parseSheet(workbook, sheetName)),
|
||
};
|
||
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
|
||
fs.writeFileSync(outputPath, `${JSON.stringify(output, null, 2)}\n`, 'utf8');
|
||
console.log(`Imported ${output.categories.length} category mappings into ${outputPath}`);
|
||
}
|
||
|
||
main();
|