|
import * as XLSX from 'xlsx'; |
|
|
|
export const formatGeminiResponseToExcel = (response: string) => { |
|
try { |
|
|
|
const lines = response.split('\n').filter(line => line.trim()); |
|
|
|
|
|
const data = lines.map(line => { |
|
|
|
const cleanLine = line.replace(/^[-*•\d.]+\s*/, ''); |
|
|
|
|
|
const values = cleanLine.split(/[,|\t]+/).map(col => col.trim()); |
|
|
|
|
|
return values; |
|
}); |
|
|
|
|
|
const wb = XLSX.utils.book_new(); |
|
|
|
|
|
const ws = XLSX.utils.aoa_to_sheet(data); |
|
|
|
|
|
const colWidths = data.reduce((widths: number[], row) => { |
|
row.forEach((cell, index) => { |
|
const length = cell?.toString().length || 0; |
|
widths[index] = Math.max(widths[index] || 0, length); |
|
}); |
|
return widths; |
|
}, []); |
|
|
|
ws['!cols'] = colWidths.map(width => ({ width: width + 2 })); |
|
|
|
|
|
XLSX.utils.book_append_sheet(wb, ws, 'Gemini Response'); |
|
|
|
|
|
XLSX.writeFile(wb, 'gemini-response.xlsx'); |
|
} catch (error) { |
|
console.error('Error formatting Excel:', error); |
|
throw new Error('Failed to format response as Excel'); |
|
} |
|
}; |