[javascript] bootstrap-table export csv 문제
bootstrap-table.com을 통해 grid 생성 후
csv로 export를 하는데 data가 undefined 일 경우 '- 로 데이터가 나오는 문제가 확인되었다
Bootstrap Table · An extended table to the integration with some of the most widely used CSS frameworks. (Supports Bootstrap, S
Bootstrap Table An extended table to the integration with some of the most widely used CSS frameworks. (Supports Bootstrap, Semantic UI, Bulma, Material Design, Foundation) Currently1.18.1
bootstrap-table.com
data가 undefined의 경우는 bootstrap-table에서 - 로 바꿔서 보여주는데
formatter를 통해 - 로 변경했을 경우도 '- 로 csv에서 노출되고 있었다
bootstrap-table 의 export는 tableExport.jquery.plugin 을 사용하고 있고 exportOption을 그대로 차용한다
해서 해당 코드를 확인하게 되었는데
// defaults option
...
preventInjection: true, // Prepend a single quote to cell strings that start with =,+,- or @ to prevent formula injection
...
preventInjection 옵션에서 발견한 하이픈..
해당 코드를 좀더 확인해보니 preventInjection 함수에서 - 가 '- 로 변경되는 것을 확인할 수 있었다
...
if (dataString instanceof Date)
result = defaults.csvEnclosure + dataString.toLocaleString() + defaults.csvEnclosure;
else {
result = preventInjection(csvValue);
result = replaceAll(result, defaults.csvEnclosure, defaults.csvEnclosure + defaults.csvEnclosure);
if (result.indexOf(defaults.csvSeparator) >= 0 || /[\r\n ]/g.test(result))
result = defaults.csvEnclosure + result + defaults.csvEnclosure;
}
...
function preventInjection (str) {
if (str.length > 0 && defaults.preventInjection === true) {
var chars = '=+-@';
if (chars.indexOf(str.charAt(0)) >= 0)
return ('\'' + str);
}
return str;
}
...
preventInjectin 옵션을 false로 주면 해결
csv의 경우 colspan, rowspan이 불가능하여 excel로 다운로드 추가해보았는데 파일이 정상적으로 다운로드 되지 않아 디버깅해보니 아래와 같은 메세지가 노출되었다
saveAs is not a function
FileSaver.js 추가하여 해결
github.com/eligrey/FileSaver.js/issues/253