diff --git a/scripts/generateXlsx.js b/scripts/generateXlsx.js index 8167650..70315b2 100644 --- a/scripts/generateXlsx.js +++ b/scripts/generateXlsx.js @@ -106,30 +106,45 @@ function addSheet(workbook, sheetName, data) { /** * Generates a multi-sheet XLSX file from the nested real estate data. - * @param {Array} data - The nested data from merged_data.json. + * @param {Array} mergedData - The nested data from merged_data.json. + * @param {Array} companiesData - The data from companies.json. * @param {string} filePath - The path to save the XLSX file. */ -async function generateXlsx(data, filePath) { - if (!data || data.length === 0) { +async function generateXlsx(mergedData, companiesData, filePath) { + if ((!mergedData || mergedData.length === 0) && (!companiesData || companiesData.length === 0)) { console.log(`没有数据可生成 XLSX 文件 (${filePath})。`); return; } - console.log('正在将数据处理成多个工作表...'); - const { projects, licenses, buildings } = flattenData(data); - console.log(`- 项目: ${projects.length} 条`); - console.log(`- 许可证: ${licenses.length} 条`); - console.log(`- 楼幢: ${buildings.length} 条`); - const workbook = new ExcelJS.Workbook(); workbook.creator = 'Gemini Assistant'; workbook.created = new Date(); workbook.modified = new Date(); console.log('正在创建 Excel 工作表...'); - addSheet(workbook, '项目', projects); - addSheet(workbook, '预售许可证', licenses); - addSheet(workbook, '楼幢', buildings); + + if (mergedData && mergedData.length > 0) { + console.log('正在将项目和许可证数据处理成多个工作表...'); + const { projects, licenses, buildings } = flattenData(mergedData); + console.log(`- 项目: ${projects.length} 条`); + console.log(`- 许可证: ${licenses.length} 条`); + console.log(`- 楼幢: ${buildings.length} 条`); + + addSheet(workbook, '项目', projects); + addSheet(workbook, '预售许可证', licenses); + addSheet(workbook, '楼幢', buildings); + } else { + console.log('- 注意: merged_data.json 没有数据,跳过项目和许可证工作表创建。'); + } + + if (companiesData && companiesData.length > 0) { + console.log('正在添加公司数据工作表...'); + console.log(`- 企业: ${companiesData.length} 条`); + addSheet(workbook, '企业', companiesData); + } else { + console.log('- 注意: companies.json 没有数据,跳过企业工作表创建。'); + } + await workbook.xlsx.writeFile(filePath); console.log(`✅ 成功生成多工作表 Excel 文件: ${filePath}`); @@ -140,23 +155,41 @@ async function generateXlsx(data, filePath) { */ async function run() { console.log('🚀 开始生成 Excel 分析文件...'); - const dataPath = path.join(process.cwd(), 'data', 'merged_data.json'); + const mergedDataPath = path.join(process.cwd(), 'data', 'merged_data.json'); + const companiesDataPath = path.join(process.cwd(), 'data', 'companies.json'); const outputPath = path.join(process.cwd(), '普宁房产数据分析.xlsx'); + let mergedData = []; + let companiesData = []; + try { - await fs.access(dataPath); - console.log(`读取数据源: ${dataPath}`); - const jsonData = JSON.parse(await fs.readFile(dataPath, 'utf-8')); - await generateXlsx(jsonData, outputPath); + await fs.access(mergedDataPath); + console.log(`读取数据源: ${mergedDataPath}`); + mergedData = JSON.parse(await fs.readFile(mergedDataPath, 'utf-8')); } catch (error) { if (error.code === 'ENOENT') { - console.error(`❌ 错误: 未找到数据文件 '${path.basename(dataPath)}'。`); - console.error('请先运行 `npm start` 来生成完整的数据文件。'); + console.log(`- 注意: 未找到数据文件 '${path.basename(mergedDataPath)}'。`); } else { - console.error('❌ 生成 XLSX 文件时发生错误:', error); + console.error(`❌ 读取 ${path.basename(mergedDataPath)} 时发生错误:`, error.message); + process.exit(1); } - process.exit(1); } + + try { + await fs.access(companiesDataPath); + console.log(`读取数据源: ${companiesDataPath}`); + companiesData = JSON.parse(await fs.readFile(companiesDataPath, 'utf-8')); + } catch (error) { + if (error.code === 'ENOENT') { + console.log(`- 注意: 未找到数据文件 '${path.basename(companiesDataPath)}'。`); + } else { + console.error(`❌ 读取 ${path.basename(companiesDataPath)} 时发生错误:`, error.message); + process.exit(1); + } + } + + // Pass both datasets to generateXlsx + await generateXlsx(mergedData, companiesData, outputPath); } -run(); +run(); \ No newline at end of file