-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
106 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,121 @@ | ||
import { IController, ResultType } from '@/types'; | ||
import { | ||
saveAs, coordinateToString, | ||
getWorkSheetKey | ||
} from '@/util'; | ||
import { saveAs, coordinateToString, getWorkSheetKey, assert } from '@/util'; | ||
|
||
function processRow(row: ResultType[]) { | ||
let finalVal = ''; | ||
for (let j = 0;j < row.length;j++) { | ||
const t = row[j] ?? ''; | ||
let innerValue = ''; | ||
if (t === 0) { | ||
innerValue = t.toString(); | ||
} | ||
if (t) { | ||
innerValue = t.toString(); | ||
} | ||
let result = innerValue.replace(/"/g, '""'); | ||
if (result.search(/("|,|\n)/g) >= 0) { | ||
result = `"${result}"`; | ||
function processItem(value: any) { | ||
const type = typeof value; | ||
if (type === 'string') { | ||
return value; | ||
} else if (type === 'bigint') { | ||
return '' + value; | ||
} else if (type === 'number') { | ||
return '' + value; | ||
} else if (type === 'boolean') { | ||
return value ? 'TRUE' : 'FALSE'; | ||
} else if (value instanceof Date) { | ||
return '' + value.getTime(); | ||
} else if (type === 'object' && value !== null) { | ||
return JSON.stringify(value); | ||
} else { | ||
return ''; | ||
} | ||
} | ||
|
||
function processRow(row: any[]) { | ||
const delimiter = ','; | ||
const quote = '"'; | ||
const escape_formulas = false; | ||
const quoted_string = false; | ||
const escape = '"'; | ||
const quoted = false; | ||
const record_delimiter = '\n'; | ||
|
||
let csvRecord = ''; | ||
for (let j = 0; j < row.length; j++) { | ||
const field = row[j]; | ||
let value = processItem(field); | ||
if ('' === value) { | ||
csvRecord += value; | ||
} else if (value) { | ||
assert( | ||
typeof value === 'string', | ||
`Formatter must return a string, null or undefined, got ${JSON.stringify( | ||
value, | ||
)}`, | ||
); | ||
const containsdelimiter = | ||
delimiter.length && value.indexOf(delimiter) >= 0; | ||
const containsQuote = value.indexOf(quote) >= 0; | ||
const containsEscape = value.indexOf(escape) >= 0 && escape !== quote; | ||
const containsRecordDelimiter = value.indexOf(record_delimiter) >= 0; | ||
const quotedString = quoted_string && typeof field === 'string'; | ||
if (escape_formulas) { | ||
switch (value[0]) { | ||
case '=': | ||
case '+': | ||
case '-': | ||
case '@': | ||
case '\t': | ||
case '\r': | ||
case '\uFF1D': // Unicode '=' | ||
case '\uFF0B': // Unicode '+' | ||
case '\uFF0D': // Unicode '-' | ||
case '\uFF20': // Unicode '@' | ||
value = `'${value}`; | ||
break; | ||
} | ||
} | ||
const shouldQuote = | ||
containsQuote === true || | ||
containsdelimiter || | ||
containsRecordDelimiter || | ||
quoted || | ||
quotedString; | ||
if (shouldQuote === true && containsEscape === true) { | ||
const regexp = | ||
escape === '\\' | ||
? // @ts-ignore | ||
Check failure on line 76 in src/containers/Excel/exportCSV.ts
|
||
new RegExp(escape + escape, 'g') | ||
: new RegExp(escape, 'g'); | ||
value = value.replace(regexp, escape + escape); | ||
} | ||
if (containsQuote === true) { | ||
const regexp = new RegExp(quote, 'g'); | ||
value = value.replace(regexp, escape + quote); | ||
} | ||
if (shouldQuote === true) { | ||
value = quote + value + quote; | ||
} | ||
csvRecord += value; | ||
} | ||
if (j > 0) { | ||
finalVal += ','; | ||
if (j !== row.length - 1) { | ||
csvRecord += delimiter; | ||
} | ||
finalVal += result; | ||
} | ||
return `${finalVal}\n`; | ||
return csvRecord; | ||
} | ||
export function exportToCsv(fileName: string, controller: IController) { | ||
const sheetData = controller.toJSON()[getWorkSheetKey(controller.getCurrentSheetId())]; | ||
let csvFile = ''; | ||
const sheetData = | ||
controller.toJSON()[getWorkSheetKey(controller.getCurrentSheetId())]; | ||
const csvList: string[] = []; | ||
const sheetInfo = controller.getSheetInfo(controller.getCurrentSheetId()); | ||
if (sheetData) { | ||
for (let row = 0;row < sheetInfo.rowCount;row++) { | ||
for (let row = 0; row < sheetInfo.rowCount; row++) { | ||
const list: ResultType[] = []; | ||
for (let col = 0;col < sheetInfo.colCount;col++) { | ||
for (let col = 0; col < sheetInfo.colCount; col++) { | ||
const key = coordinateToString(row, col); | ||
const value = sheetData[key]; | ||
if (!value) { | ||
list.push(''); | ||
continue; | ||
} | ||
const t = value.formula ?? value.value; | ||
const t = value.formula || value.value; | ||
list.push(t); | ||
} | ||
csvFile += processRow(list); | ||
csvList.push(processRow(list)); | ||
} | ||
} | ||
const blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' }); | ||
const blob = new Blob([csvList.join('\n')], { | ||
type: 'text/csv;charset=utf-8;', | ||
}); | ||
saveAs(blob, fileName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters