このコミットが含まれているのは:
2026-10-26 18:59:25 +09:00
コミット 88711d2bd8
10個のファイルの変更、183行の追加、39行の削除
+58 -2
ファイルの表示
@@ -1,11 +1,67 @@
export type CountsClipboard = { text: string; html: string }
export type CountsClipboardColumn = { label: string; children?: CountsClipboardColumn[] }
export type CountsClipboardCell = string | CountsClipboardTable
export type CountsClipboardTable = {
columns: CountsClipboardColumn[]; rows: CountsClipboardCell[][] }
const escapeHTML = (value: string): string => value.replace (/[&<>"']/g, character =>
({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[character]!)
export const countsClipboard = (rows: string[][], headers = false): CountsClipboard => {
const columnWidth = (column: CountsClipboardColumn): number =>
column.children?.reduce ((sum, child) => sum + columnWidth (child), 0) || 1
const headerDepth = (columns: CountsClipboardColumn[]): number =>
columns.length === 0 ? 0 : 1 + Math.max (0, ...columns.map (column =>
headerDepth (column.children ?? [])))
const headerRows = (columns: CountsClipboardColumn[]): string[][] => {
const width = columns.reduce ((sum, column) => sum + columnWidth (column), 0)
const rows = Array.from ({ length: headerDepth (columns) },
() => Array<string> (width).fill (''))
const place = (fields: CountsClipboardColumn[], row: number, offset: number) => {
for (const field of fields)
{
rows[row][offset] = field.label
if (field.children?.length) place (field.children, row + 1, offset)
offset += columnWidth (field)
}
}
if (columns.length) place (columns, 0, 0)
return rows
}
const dataRows = (
table: CountsClipboardTable,
columns = table.columns,
): string[][] => {
if (columns.length === 0) return []
return table.rows.flatMap (record => {
const blocks = columns.map ((column, index) => {
const value = record[index] ?? ''
return typeof value === 'string' ? [[value]]
: dataRows (value, column.children ?? value.columns)
})
const height = Math.max (1, ...blocks.map (block => block.length))
// Sibling arrays share the parent row's height; they never form a Cartesian product.
return Array.from ({ length: height }, (_, row) => columns.flatMap ((column, index) =>
Array.from ({ length: columnWidth (column) }, (_, offset) =>
blocks[index][row]?.[offset] ?? '')))
})
}
export const countsClipboard = (
source: string[][] | CountsClipboardTable,
headers = false,
): CountsClipboard => {
const headings = !Array.isArray (source) && headers ? headerRows (source.columns) : []
const rows = Array.isArray (source) ? source : [...headings, ...dataRows (source)]
const headingCount = Array.isArray (source) ? (headers ? 1 : 0) : headings.length
// Excel accepts quoted TSV fields only when delimiters or quotes require them.
// Prefix formula-like text in both representations, including leading whitespace.
const safe = rows.map (row => row.map (value => /^\s*[=+@-]/.test (value)
@@ -13,7 +69,7 @@ export const countsClipboard = (rows: string[][], headers = false): CountsClipbo
const text = safe.map (row => row.map (value => /[\t\r\n"]/.test (value)
? `"${value.replaceAll ('"', '""')}"` : value).join ('\t')).join ('\r\n')
const html = '<html><body><table>' + safe.map ((row, index) => '<tr>' + row.map (value => {
const tag = headers && index === 0 ? 'th' : 'td'
const tag = index < headingCount ? 'th' : 'td'
// Preserve time strings, leading zeroes, and user text instead of inferring dates/formulae.
const numeric = value.length <= 15 && /^(?:0|[1-9]\d*)(?:\.\d+)?$/.test (value)
const style = numeric ? 'white-space:pre-wrap' : 'white-space:pre-wrap;mso-number-format:"\\@"'