Skip to content

Commit

Permalink
Add extended header to Excel files (#237)
Browse files Browse the repository at this point in the history
Add extended header to Excel files
  • Loading branch information
thinkh authored Nov 8, 2019
2 parents 2a5d5c3 + 9fcc19f commit 61c18f8
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/lineup/internal/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import {lazyDialogModule} from '../../dialogs';
import {randomId} from 'phovea_core/src';
import {json2xlsx} from '../../internal/xlsx';

function isDateColumn(column:Column) {
function isDateColumn(column: Column) {
return column.desc.type === 'date';
}

function getColumnName(column: Column) {
return column.label + (column.description ? '\n' + column.description : '');
}

export function exportRanking(columns: Column[], rows: IDataRow[], separator: string) {
//optionally quote not numbers
const escape = new RegExp(`["]`, 'g');
Expand All @@ -26,7 +30,7 @@ export function exportRanking(columns: Column[], rows: IDataRow[], separator: st
}

const r: string[] = [];
r.push(columns.map((d) => quote(`${d.label}${d.description ? `\n${d.description}` : ''}`)).join(separator));
r.push(columns.map((d) => quote(getColumnName(d))).join(separator));
rows.forEach((row) => {
r.push(columns.map((c) => quote(c.getExportValue(row, 'text'), c)).join(separator));
});
Expand All @@ -37,7 +41,7 @@ export function exportJSON(columns: Column[], rows: IDataRow[]) {
const converted = rows.map((row) => {
const r: any = {};
for (const col of columns) {
r[col.label] = isNumberColumn(col) ? col.getRawNumber(row) : col.getExportValue(row, 'json');
r[getColumnName(col)] = isNumberColumn(col) ? col.getRawNumber(row) : col.getExportValue(row, 'json');
}
return r;
});
Expand All @@ -48,14 +52,14 @@ export function exportxlsx(columns: Column[], rows: IDataRow[]) {
const converted = rows.map((row) => {
const r: any = {};
for (const col of columns) {
r[col.label] = isNumberColumn(col) ? col.getRawNumber(row) : col.getValue(row);
r[getColumnName(col)] = isNumberColumn(col) ? col.getRawNumber(row) : col.getValue(row);
}
return r;
});
return json2xlsx({
sheets: [{
title: 'LineUp',
columns: columns.map((d) => ({name: d.label, type: <'float'|'string' |'date'>(isNumberColumn(d) ? 'float' : isDateColumn(d)? 'date':'string')})),
columns: columns.map((d) => ({name: getColumnName(d), type: <'float' | 'string' | 'date'>(isNumberColumn(d) ? 'float' : isDateColumn(d) ? 'date' : 'string')})),
rows: converted
}]
});
Expand All @@ -82,9 +86,9 @@ function toBlob(content: string, mimeType: string) {
function convertRanking(provider: LocalDataProvider, order: number[], columns: Column[], type: ExportType, name: string) {
const rows = provider.viewRawRows(order);

const separators = {csv : ',', tsv: '\t', ssv: ';'};
const separators = {csv: ',', tsv: '\t', ssv: ';'};
let content: Promise<Blob> | Blob;
const mimeTypes = {csv : 'text/csv', tsv: 'text/tab-separated-values', ssv: 'text/csv', json: 'application/json', xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'};
const mimeTypes = {csv: 'text/csv', tsv: 'text/tab-separated-values', ssv: 'text/csv', json: 'application/json', xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'};
const mimeType = mimeTypes[type];
if (type in separators) {
content = toBlob(exportRanking(columns, rows, separators[type]), mimeType);
Expand Down Expand Up @@ -163,7 +167,7 @@ function customizeDialog(provider: LocalDataProvider): Promise<IExportData> {

const rows = data.get('rows').toString();
let order: number[];
switch(rows) {
switch (rows) {
case 'selected':
order = provider.getSelection();
break;
Expand Down

0 comments on commit 61c18f8

Please sign in to comment.