diff --git a/packages/@ourworldindata/core-table/src/OwidTable.ts b/packages/@ourworldindata/core-table/src/OwidTable.ts index c17d1e7b540..529aff908b1 100644 --- a/packages/@ourworldindata/core-table/src/OwidTable.ts +++ b/packages/@ourworldindata/core-table/src/OwidTable.ts @@ -606,12 +606,26 @@ export class OwidTable extends CoreTable { } // Give our users a clean CSV of each Grapher. Assumes an Owid Table with entityName. - toPrettyCsv(useShortNames: boolean = false): string { - return this.dropColumns([ - OwidTableSlugs.entityId, - OwidTableSlugs.time, - OwidTableSlugs.entityColor, - ]) + toPrettyCsv( + useShortNames: boolean = false, + activeColumnSlugs: string[] | undefined = undefined + ): string { + let table + if (activeColumnSlugs?.length) { + table = this.select([ + OwidTableSlugs.year, + OwidTableSlugs.day, + this.entityNameSlug, + ...activeColumnSlugs, + ]) + } else { + table = this.dropColumns([ + OwidTableSlugs.entityId, + OwidTableSlugs.time, + OwidTableSlugs.entityColor, + ]) + } + return table .sortBy([this.entityNameSlug]) .toCsvWithColumnNames(useShortNames) } diff --git a/packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx b/packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx index b5a45c73b77..39d896a763f 100644 --- a/packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx +++ b/packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx @@ -65,6 +65,7 @@ export interface DownloadModalManager { showAdminControls?: boolean isSocialMediaExport?: boolean isPublished?: boolean + activeColumnSlugs?: string[] } interface DownloadModalProps { @@ -432,13 +433,17 @@ interface DataDownloadContextClientSide extends DataDownloadContextBase { // Only needed for local CSV generation table: OwidTable transformedTable: OwidTable + activeColumnSlugs: string[] | undefined } const createCsvBlobLocally = async (ctx: DataDownloadContextClientSide) => { const csv = ctx.csvDownloadType === CsvDownloadType.Full - ? ctx.table.toPrettyCsv(ctx.shortColNames) - : ctx.transformedTable.toPrettyCsv(ctx.shortColNames) + ? ctx.table.toPrettyCsv(ctx.shortColNames, ctx.activeColumnSlugs) + : ctx.transformedTable.toPrettyCsv( + ctx.shortColNames, + ctx.activeColumnSlugs + ) return new Blob([csv], { type: "text/csv;charset=utf-8" }) } @@ -765,6 +770,7 @@ export const DownloadModalDataTab = (props: DownloadModalProps) => { table: props.manager.table ?? BlankOwidTable(), transformedTable: props.manager.transformedTable ?? BlankOwidTable(), + activeColumnSlugs: props.manager.activeColumnSlugs, }), [ props.manager.baseUrl, @@ -772,6 +778,7 @@ export const DownloadModalDataTab = (props: DownloadModalProps) => { props.manager.queryStr, props.manager.table, props.manager.transformedTable, + props.manager.activeColumnSlugs, ] )