Skip to content

A local-first personal finance app

[AI] feat(budget analysis report): export csv feature (#8163)

tabedzki6519bbecb6f7
diff --git a/packages/desktop-client/package.json b/packages/desktop-client/package.json
index 3851f0f348..9238deed20 100644
--- a/packages/desktop-client/package.json
+++ b/packages/desktop-client/package.json
@@ -162,6 +162,7 @@
"babel-plugin-react-compiler": "^1.0.0",
"cmdk": "^1.1.1",
"cross-env": "^10.1.0",
+ "csv-stringify": "^6.7.0",
"date-fns": "^4.4.0",
"downshift": "9.3.6",
"es-toolkit": "^1.47.1",
diff --git a/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx b/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx
index eca613f27f..ee983dcf81 100644
--- a/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx
+++ b/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx
@@ -6,10 +6,12 @@ import { AlignedText } from '@actual-app/components/aligned-text';
import { Block } from '@actual-app/components/block';
import { Button } from '@actual-app/components/button';
import { useResponsive } from '@actual-app/components/hooks/useResponsive';
+import { SvgDownload } from '@actual-app/components/icons/v1';
import { Menu } from '@actual-app/components/menu';
import { Paragraph } from '@actual-app/components/paragraph';
import { Popover } from '@actual-app/components/popover';
import { theme } from '@actual-app/components/theme';
+import { Tooltip } from '@actual-app/components/tooltip';
import { View } from '@actual-app/components/view';
import { send } from '@actual-app/core/platform/client/connection';
import * as monthUtils from '@actual-app/core/shared/months';
@@ -30,6 +32,7 @@ import { BudgetAnalysisGraph } from '#components/reports/graphs/BudgetAnalysisGr
import { Header } from '#components/reports/Header';
import { LoadingIndicator } from '#components/reports/LoadingIndicator';
import { calculateTimeRange } from '#components/reports/reportRanges';
+import { buildBudgetAnalysisCsv } from '#components/reports/spreadsheets/budget-analysis-export';
import { createBudgetAnalysisSpreadsheet } from '#components/reports/spreadsheets/budget-analysis-spreadsheet';
import { useReport } from '#components/reports/useReport';
import { fromDateRepr } from '#components/reports/util';
@@ -326,6 +329,19 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {
);
}
+ const onExportCsv = () => {
+ if (!data) return;
+ const csv = buildBudgetAnalysisCsv(data.intervalData);
+ const reportName = (widget?.meta?.name || t('Budget Analysis'))
+ .replace(/[^a-z0-9]/gi, '-')
+ .toLowerCase();
+ void window.Actual.saveFile(
+ csv,
+ `${reportName}-${start}-${end}.csv`,
+ t('Export budget analysis'),
+ );
+ };
+
if (!data || !allMonths) {
return <LoadingIndicator />;
}
@@ -413,6 +429,16 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {
}
/>
+ <Tooltip content={t('Export as CSV')}>
+ <Button
+ variant="bare"
+ onPress={onExportCsv}
+ aria-label={t('Export as CSV')}
+ >
+ <SvgDownload style={{ width: 16, height: 16 }} />
+ </Button>
+ </Tooltip>
+
{widget && (
<Button variant="primary" onPress={onSaveWidget}>
<Trans>Save widget</Trans>
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-export.test.ts b/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-export.test.ts
new file mode 100644
index 0000000000..ddea124531
--- /dev/null
+++ b/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-export.test.ts
@@ -0,0 +1,70 @@
+import { describe, expect, it } from 'vitest';
+
+import { buildBudgetAnalysisCsv } from './budget-analysis-export';
+
+describe('buildBudgetAnalysisCsv', () => {
+ const rows = [
+ {
+ date: '2024-01',
+ budgeted: 100000,
+ spent: -75000,
+ balance: 25000,
+ overspendingAdjustment: 0,
+ },
+ {
+ date: '2024-02',
+ budgeted: 100000,
+ spent: -110000,
+ balance: 15000,
+ overspendingAdjustment: 10000,
+ },
+ ];
+
+ const lines = (csv: string) => csv.trimEnd().split('\n');
+
+ it('produces a header row and one data row per interval', () => {
+ expect(lines(buildBudgetAnalysisCsv(rows))).toHaveLength(3);
+ });
+
+ it('header contains the expected column names', () => {
+ expect(lines(buildBudgetAnalysisCsv(rows))[0]).toBe(
+ 'Month,Budgeted,Spent,Overspending Adjustment,Balance',
+ );
+ });
+
+ it('converts integer amounts to decimal strings', () => {
+ // budgeted 100000 → 1000, spent -75000 → -750, overspending 0 → 0, balance 25000 → 250
+ expect(lines(buildBudgetAnalysisCsv(rows))[1]).toBe(
+ '2024-01,1000,-750,0,250',
+ );
+ });
+
+ it('handles negative spent values correctly', () => {
+ // spent -110000 → -1100
+ const fields = lines(buildBudgetAnalysisCsv(rows))[2].split(',');
+ expect(fields[2]).toBe('-1100');
+ });
+
+ it('returns only the header row for empty input', () => {
+ const result = lines(buildBudgetAnalysisCsv([]));
+ expect(result).toHaveLength(1);
+ expect(result[0]).toBe(
+ 'Month,Budgeted,Spent,Overspending Adjustment,Balance',
+ );
+ });
+
+ it('escapes fields containing commas with double quotes', () => {
+ const specialRows = [
+ {
+ date: '2024,01',
+ budgeted: 0,
+ spent: 0,
+ balance: 0,
+ overspendingAdjustment: 0,
+ },
+ ];
+ expect(
+ lines(buildBudgetAnalysisCsv(specialRows))[1].startsWith('"2024,01"'),
+ ).toBe(true);
+ });
+});
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-export.ts b/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-export.ts
new file mode 100644
index 0000000000..a1ecc19924
--- /dev/null
+++ b/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-export.ts
@@ -0,0 +1,44 @@
+import { integerToAmount } from '@actual-app/core/shared/util';
+// Use the browser ESM build: the default `csv-stringify/sync` entry pulls in
+// the Node build (which imports `stream`), breaking the lazy-loaded reports
+// chunk in the browser bundle. The browser ESM build is self-contained.
+import { stringify as csvStringify } from 'csv-stringify/browser/esm/sync';
+import { t } from 'i18next';
+
+type IntervalRow = {
+ date: string;
+ budgeted: number;
+ spent: number;
+ balance: number;
+ overspendingAdjustment: number;
+};
+
+const FORMULA_TRIGGERS = /^[=+\-@\t\r]/;
+
+export function buildBudgetAnalysisCsv(rows: IntervalRow[]): string {
+ const month = t('Month');
+ const budgeted = t('Budgeted');
+ const spent = t('Spent');
+ const overspendingAdjustment = t('Overspending Adjustment');
+ const balance = t('Balance');
+
+ const columns = [month, budgeted, spent, overspendingAdjustment, balance];
+
+ return csvStringify(
+ rows.map(row => [
+ row.date,
+ integerToAmount(row.budgeted),
+ integerToAmount(row.spent),
+ integerToAmount(row.overspendingAdjustment),
+ integerToAmount(row.balance),
+ ]),
+ {
+ header: true,
+ columns,
+ cast: {
+ string: (value: string) =>
+ FORMULA_TRIGGERS.test(value) ? "'" + value : value,
+ },
+ },
+ );
+}
diff --git a/upcoming-release-notes/ai-feat-budget-analysis-report-export-csv-feature.md b/upcoming-release-notes/ai-feat-budget-analysis-report-export-csv-feature.md
new file mode 100644
index 0000000000..0b9b68754e
--- /dev/null
+++ b/upcoming-release-notes/ai-feat-budget-analysis-report-export-csv-feature.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [tabedzki]
+---
+
+Budget Analysis reports can now be exported as CSV
diff --git a/yarn.lock b/yarn.lock
index d234dd6e14..b8859ae878 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -254,6 +254,7 @@ __metadata:
babel-plugin-react-compiler: "npm:^1.0.0"
cmdk: "npm:^1.1.1"
cross-env: "npm:^10.1.0"
+ csv-stringify: "npm:^6.7.0"
date-fns: "npm:^4.4.0"
downshift: "npm:9.3.6"
es-toolkit: "npm:^1.47.1"