focalboard/webapp/src/csvExporter.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-10-15 21:22:38 +02:00
import { BoardView } from "./blocks/boardView"
2020-10-08 18:21:27 +02:00
import { BoardTree } from "./boardTree"
import { OctoUtils } from "./octoUtils"
import { Utils } from "./utils"
class CsvExporter {
static exportTableCsv(boardTree: BoardTree, view?: BoardView) {
const { activeView } = boardTree
const viewToExport = view ?? activeView
const rows = CsvExporter.generateTableArray(boardTree, view)
let csvContent = "data:text/csv;charset=utf-8,"
rows.forEach((row) => {
const encodedRow = row.join(",")
csvContent += encodedRow + "\r\n"
})
const filename = `${Utils.sanitizeFilename(viewToExport.title)}.csv`
const encodedUri = encodeURI(csvContent)
const link = document.createElement("a")
link.style.display = "none"
link.setAttribute("href", encodedUri)
link.setAttribute("download", filename)
document.body.appendChild(link) // FireFox support
link.click()
// TODO: Remove or reuse link
}
private static generateTableArray(boardTree: BoardTree, view?: BoardView): string[][] {
const { board, cards, activeView } = boardTree
const viewToExport = view ?? activeView
const rows: string[][] = []
const visibleProperties = board.cardProperties.filter(template => viewToExport.visiblePropertyIds.includes(template.id))
{
// Header row
const row: string[] = []
visibleProperties.forEach(template => {
row.push(template.name)
})
rows.push(row)
}
cards.forEach(card => {
const row: string[] = []
visibleProperties.forEach(template => {
2020-10-14 01:49:29 +02:00
const propertyValue = card.properties[template.id]
const displayValue = OctoUtils.propertyDisplayValue(card, propertyValue, template) || ""
2020-10-08 18:21:27 +02:00
if (template.type === "number") {
2020-10-14 01:49:29 +02:00
const numericValue = propertyValue ? Number(propertyValue).toString() : undefined
2020-10-08 18:21:27 +02:00
row.push(numericValue)
} else {
// Export as string
row.push(`"${displayValue}"`)
}
})
rows.push(row)
})
return rows
}
}
export { CsvExporter }