MCPcopy Create free account
hub / github.com/Datakitpage/Datakit / exportQueryAsCSV

Function exportQueryAsCSV

frontend/src/utils/queryExport.ts:21–50  ·  view source on GitHub ↗
(
  results: any[][] = [],
  columns: string[] = [],
  filename: string = 'Query_Results'
)

Source from the content-addressed store, hash-verified

19 * Export query results as CSV
20 */
21export const exportQueryAsCSV = (
22 results: any[][] = [],
23 columns: string[] = [],
24 filename: string = 'Query_Results'
25): void => {
26 try {
27 if (results.length === 0 || columns.length === 0) {
28 throw new Error('No data to export');
29 }
30
31 // Create CSV content
32 const csvContent = [
33 // Header row
34 columns.map(col => `"${col.replace(/"/g, '""')}"`).join(','),
35 // Data rows
36 ...results.map(row =>
37 row.map(cell => {
38 const cellValue = cell === null || cell === undefined ? '' : String(cell);
39 return `"${cellValue.replace(/"/g, '""')}"`;
40 }).join(',')
41 )
42 ].join('\n');
43
44 downloadText(csvContent, filename, 'csv');
45
46 } catch (error) {
47 console.error('Failed to export CSV:', error);
48 throw new Error('CSV export failed. Please try again.');
49 }
50};
51
52/**
53 * Export query results as JSON

Callers 1

exportQueryFunction · 0.85

Calls 1

downloadTextFunction · 0.90

Tested by

no test coverage detected