Skip to content

Commit ccafb5f

Browse files
Merge pull request #26 from harishdeivanayagam/feature/cloud-alpha-reqs
fix: export csv opt
2 parents cac7111 + e0c33f6 commit ccafb5f

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/app/console/sheets/[slug]/page.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,43 @@ export default function SheetPage() {
207207

208208

209209
const handleExport = async () => {
210+
let csvContent = ""
210211

212+
if (sheet.singleSource) {
213+
// Create header row with column names
214+
csvContent = sheetColumns.map(col => `"${col.name}"`).join(",") + "\n"
215+
216+
// Add data rows
217+
for (let rowNum = 1; rowNum <= extractedMaximumRowNumber; rowNum++) {
218+
const rowData = sheetColumns.map(column => {
219+
const value = extractedSheetRows[`${rowNum}_${column.id}`]?.value || ""
220+
return `"${value.replace(/"/g, '""')}"` // Escape quotes in CSV
221+
}).join(",")
222+
csvContent += rowData + "\n"
223+
}
224+
}
225+
226+
if (!sheet.singleSource) {
227+
// Create header row with source column and other columns
228+
csvContent = `"Source",${sheetColumns.map(col => `"${col.name}"`).join(",")}\n`
229+
230+
// Add data rows for each source
231+
sheetSources.forEach(source => {
232+
const rowData = sheetColumns.map(column => {
233+
const value = columnValues[`${source.id}_${column.id}`]?.value || ""
234+
return `"${value.replace(/"/g, '""')}"` // Escape quotes in CSV
235+
})
236+
csvContent += `"${source.source.nickName}",${rowData.join(",")}\n`
237+
})
238+
}
239+
240+
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" })
241+
const url = URL.createObjectURL(blob)
242+
const link = document.createElement("a")
243+
link.href = url
244+
link.setAttribute("download", `${sheet.name}.csv`)
245+
document.body.appendChild(link)
246+
link.click()
211247
}
212248

213249
return (
@@ -234,7 +270,7 @@ export default function SheetPage() {
234270
<PiPlay />
235271
Run All
236272
</Button>
237-
<Button disabled={sheet.extractInProgress || runAlert.open}>
273+
<Button disabled={sheet.extractInProgress || runAlert.open} onClick={handleExport}>
238274
<PiDownload />
239275
Export as CSV
240276
</Button>

0 commit comments

Comments
 (0)