2021-11-29 20:12:45 +00:00
|
|
|
import {ApiError} from 'app/common/ApiError';
|
2021-07-21 08:46:03 +00:00
|
|
|
import {createFormatter} from 'app/common/ValueFormatter';
|
|
|
|
import {ActiveDoc} from 'app/server/lib/ActiveDoc';
|
2021-09-01 21:07:53 +00:00
|
|
|
import {ExportData, exportSection, exportTable, Filter} from 'app/server/lib/Export';
|
2021-11-29 20:12:45 +00:00
|
|
|
import * as log from 'app/server/lib/log';
|
2021-07-21 08:46:03 +00:00
|
|
|
import * as bluebird from 'bluebird';
|
2021-11-29 20:12:45 +00:00
|
|
|
import * as contentDisposition from 'content-disposition';
|
2021-07-21 08:46:03 +00:00
|
|
|
import * as csv from 'csv';
|
|
|
|
import * as express from 'express';
|
2021-09-01 21:07:53 +00:00
|
|
|
|
|
|
|
export interface DownloadCSVOptions {
|
|
|
|
filename: string;
|
|
|
|
tableId: string;
|
|
|
|
viewSectionId: number | undefined;
|
|
|
|
filters: Filter[];
|
|
|
|
sortOrder: number[];
|
|
|
|
}
|
2021-07-21 08:46:03 +00:00
|
|
|
|
|
|
|
// promisify csv
|
|
|
|
bluebird.promisifyAll(csv);
|
|
|
|
|
|
|
|
/**
|
2021-09-01 21:07:53 +00:00
|
|
|
* Converts `activeDoc` to a CSV and sends the converted data through `res`.
|
|
|
|
*/
|
|
|
|
export async function downloadCSV(activeDoc: ActiveDoc, req: express.Request,
|
|
|
|
res: express.Response, options: DownloadCSVOptions) {
|
|
|
|
log.info('Generating .csv file...');
|
|
|
|
const {filename, tableId, viewSectionId, filters, sortOrder} = options;
|
2021-11-29 20:12:45 +00:00
|
|
|
const data = viewSectionId ?
|
|
|
|
await makeCSVFromViewSection(activeDoc, viewSectionId, sortOrder, filters, req) :
|
|
|
|
await makeCSVFromTable(activeDoc, tableId, req);
|
|
|
|
res.set('Content-Type', 'text/csv');
|
|
|
|
res.setHeader('Content-Disposition', contentDisposition(filename + '.csv'));
|
|
|
|
res.send(data);
|
2021-09-01 21:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a csv stream of a view section that can be transformed or parsed.
|
|
|
|
*
|
|
|
|
* See https://github.com/wdavidw/node-csv for API details.
|
2021-07-21 08:46:03 +00:00
|
|
|
*
|
|
|
|
* @param {Object} activeDoc - the activeDoc that the table being converted belongs to.
|
|
|
|
* @param {Integer} viewSectionId - id of the viewsection to export.
|
|
|
|
* @param {Integer[]} activeSortOrder (optional) - overriding sort order.
|
|
|
|
* @param {Filter[]} filters (optional) - filters defined from ui.
|
|
|
|
* @return {Promise<string>} Promise for the resulting CSV.
|
|
|
|
*/
|
2021-09-01 21:07:53 +00:00
|
|
|
export async function makeCSVFromViewSection(
|
2021-07-21 08:46:03 +00:00
|
|
|
activeDoc: ActiveDoc,
|
|
|
|
viewSectionId: number,
|
|
|
|
sortOrder: number[],
|
|
|
|
filters: Filter[],
|
|
|
|
req: express.Request) {
|
|
|
|
|
|
|
|
const data = await exportSection(activeDoc, viewSectionId, sortOrder, filters, req);
|
|
|
|
const file = convertToCsv(data);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:07:53 +00:00
|
|
|
/**
|
|
|
|
* Returns a csv stream of a table that can be transformed or parsed.
|
|
|
|
*
|
|
|
|
* @param {Object} activeDoc - the activeDoc that the table being converted belongs to.
|
|
|
|
* @param {Integer} tableId - id of the table to export.
|
|
|
|
* @return {Promise<string>} Promise for the resulting CSV.
|
|
|
|
*/
|
|
|
|
export async function makeCSVFromTable(
|
|
|
|
activeDoc: ActiveDoc,
|
|
|
|
tableId: string,
|
|
|
|
req: express.Request) {
|
|
|
|
|
|
|
|
if (!activeDoc.docData) {
|
|
|
|
throw new Error('No docData in active document');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the table to make a CSV from.
|
|
|
|
const tables = activeDoc.docData.getTable('_grist_Tables')!;
|
|
|
|
const tableRef = tables.findRow('tableId', tableId);
|
|
|
|
|
2021-11-29 20:12:45 +00:00
|
|
|
if (tableRef === 0) {
|
|
|
|
throw new ApiError(`Table ${tableId} not found.`, 404);
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:07:53 +00:00
|
|
|
const data = await exportTable(activeDoc, tableRef, req);
|
|
|
|
const file = convertToCsv(data);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2021-07-21 08:46:03 +00:00
|
|
|
function convertToCsv({
|
|
|
|
rowIds,
|
|
|
|
access,
|
2021-08-26 16:35:11 +00:00
|
|
|
columns: viewColumns,
|
|
|
|
docSettings
|
2021-07-21 08:46:03 +00:00
|
|
|
}: ExportData) {
|
|
|
|
|
|
|
|
// create formatters for columns
|
2021-08-26 16:35:11 +00:00
|
|
|
const formatters = viewColumns.map(col => createFormatter(col.type, col.widgetOptions, docSettings));
|
2021-07-21 08:46:03 +00:00
|
|
|
// Arrange the data into a row-indexed matrix, starting with column headers.
|
|
|
|
const csvMatrix = [viewColumns.map(col => col.label)];
|
|
|
|
// populate all the rows with values as strings
|
|
|
|
rowIds.forEach(row => {
|
|
|
|
csvMatrix.push(access.map((getter, c) => formatters[c].formatAny(getter(row))));
|
|
|
|
});
|
|
|
|
return csv.stringifyAsync(csvMatrix);
|
|
|
|
}
|