Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions e2e/xlsx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1095,4 +1095,36 @@ describe('xlsx dynamic arrays', () => {
expect(s.cell('K10')).toBe('6')
expect(v.errors).toEqual([])
})

test('#35: pasted into several cells, or filled as a block, every copy fills the cells under it; saved', async () => {
const v = await open()
await v.typeCell('J10', '=SEQUENCE(3)')
await shownIs(v, 'J12', '3')
// one copied cell into two: the second copy is a dynamic array formula too
await v.clickCell('J10')
await v.page.keyboard.press('ControlOrMeta+c')
await v.page.waitForTimeout(200)
await v.selectRange('L10', 'M10')
await v.page.keyboard.press('ControlOrMeta+v')
for (const col of ['L', 'M']) for (const [r, x] of [[10, '1'], [11, '2'], [12, '3']] as const) await shownIs(v, `${col}${r}`, x)
expect(await v.formula('M10')).toBe('=SEQUENCE(3)')
// the fill handle repeats a block of two cells the same way
await v.typeCell('P10', '=SEQUENCE(2)')
await v.typeCell('Q10', '=SEQUENCE(2)*10')
await v.selectRange('P10', 'Q10')
const h = (await v.page.locator('.de-fill-handle').first().boundingBox())!
const to = await v.cellPoint('T10')
await v.page.mouse.move(h.x + h.width / 2, h.y + h.height / 2)
await v.page.mouse.down()
await v.page.mouse.move(to.x, to.y, { steps: 6 })
await v.page.mouse.up()
for (const [ref, x] of [['R10', '1'], ['R11', '2'], ['S10', '10'], ['S11', '20'], ['T10', '1'], ['T11', '2']] as const) await shownIs(v, ref, x)
const { bytes } = await v.save()
const s = await xlsxSheet(bytes)
for (const [ref, x] of [['M11', '2'], ['M12', '3'], ['T11', '2']] as const) expect(s.cell(ref)).toBe(x)
const z = await unzip(new Blob([bytes]))
const xml = await z.text(z.paths.find((p) => /worksheets\/sheet1\.xml$/.test(p))!)
for (const ref of ['L10', 'M10', 'R10', 'S10', 'T10']) expect(xml).toMatch(new RegExp(`<c r="${ref}"[^>]* cm="\\d+"`))
expect(v.errors).toEqual([])
})
})
14 changes: 14 additions & 0 deletions src/core/xlsx/clip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
/** Cells as text on the clipboard: tab separated, a line per row, as spreadsheet programs write and read them. */
import { translateFormula } from './formula'
import type { ClipCell } from './types'

/**
* A copied cell repeated `dr` rows and `dc` columns further than the first copy of a block (the worker moves every
* formula by the distance of the first block): its formula moved by what is left. The rest is as copied, the flag
* of a dynamic array formula with it.
*/
export function shiftedClip(cell: ClipCell, dr: number, dc: number): ClipCell {
if (!cell || cell[3] === undefined || (!dr && !dc)) return cell
const out: NonNullable<ClipCell> = [...cell]
out[3] = translateFormula(cell[3], dr, dc)
return out
}

/** a cell written for other programs: quoted when it holds what separates cells and rows there */
export const tsvCell = (s: string) => (/["\t\n\r]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s)
Expand Down
10 changes: 3 additions & 7 deletions src/react/xlsx/XlsxView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, type CSSProperties, type MutableRefObject, type ReactNode } from 'react'
import type { BookOp, CellDetail, ClipCell, CellInput, CellOut, FindMatch, FindOptions, FmtPatch, Merge, Range, SheetOp, SheetProgress, TextRun, WorkbookMeta, XlsxClip, XlsxEditResult } from '../../core/xlsx/types'
import { parseTsv, tsvCell } from '../../core/xlsx/clip'
import { parseTsv, shiftedClip, tsvCell } from '../../core/xlsx/clip'
import { colName, MAX_ROWS } from '../../core/xlsx/workbook'
import { detectSeparator, parseCsv } from '../../core/convert'
import { closeFormula, formulaContext, isFormula, rangeText, type FormulaContext } from '../../core/xlsx/formula/assist'
Expand Down Expand Up @@ -1751,8 +1751,7 @@ function SheetGrid({ store, sheet, meta, client, edit, setEdit, hold, onSheet }:
for (let j = 0; j < cols; j++) {
const cell = clip.cells[down ? i % h : i]?.[down ? j : j % w] ?? null
const tile = down ? Math.floor(i / h) : Math.floor(j / w)
// (the worker moves every formula by the distance of the first block: the others by what is left of theirs)
line.push(cell && cell[3] !== undefined && tile ? ([cell[0], cell[1], cell[2], translateFormula(cell[3], down ? tile * h : 0, down ? 0 : tile * w)] as ClipCell) : cell)
line.push(shiftedClip(cell, down ? tile * h : 0, down ? 0 : tile * w))
}
cells.push(line)
}
Expand Down Expand Up @@ -1998,10 +1997,7 @@ function SheetGrid({ store, sheet, meta, client, edit, setEdit, hold, onSheet }:
const line: ClipCell[] = []
for (let j = 0; j < w * tiles.c; j++) {
const src = copied[i % h]?.[j % w] ?? null
const ti = Math.floor(i / h)
const tj = Math.floor(j / w)
// (the worker moves every formula by the distance of the first block: the others by what is left of theirs)
line.push(src && src[3] !== undefined && (ti || tj) ? ([src[0], src[1], src[2], translateFormula(src[3], ti * h, tj * w)] as ClipCell) : src)
line.push(shiftedClip(src, Math.floor(i / h) * h, Math.floor(j / w) * w))
}
cells.push(line)
}
Expand Down
18 changes: 18 additions & 0 deletions test/xlsx-dynamic.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** Dynamic array formulas in a workbook (spilling, #SPILL!, saving), defined names, tables and 3-D references. */
import { describe, expect, test } from 'bun:test'
import { shiftedClip } from '../src/core/xlsx/clip'
import { at, book, openBook, rows } from './xlsx-builder'

const SHEET = 'xl/worksheets/sheet1.xml'
Expand Down Expand Up @@ -118,6 +119,23 @@ describe('dynamic array formulas', () => {
expect(await shown(o, ['G1', 'G2', 'H1', 'H2'])).toEqual(['1', '2', '1', '2'])
})

test('#35: a copied block repeated by the view keeps its formulas dynamic in every copy', async () => {
const o = await openBook(await book({ sheets: [{ name: 'S', rows: rows([[5], [6], [7]]) }] }))
await o.set(0, 'C1', '=A1:A3*2')
const clip: any = await o.engine.call('xlsx.copy', [0, { r0: 0, c0: 2, r1: 0, c1: 2 }])
const cell = clip.cells[0][0]
expect(cell[4]).toBe(true)
// (as the view repeats one copied cell into E1:F1: the worker moves both by the first copy's distance)
const second = shiftedClip(cell, 0, 1)
expect(second).toEqual([cell[0], cell[1], cell[2], 'B1:B3*2', true])
expect(shiftedClip(cell, 0, 0)).toBe(cell)
expect(shiftedClip(null, 1, 1)).toBeNull()
await o.engine.call('xlsx.sheetOp', [0, { op: 'paste', range: { r0: 0, c0: 4, r1: 0, c1: 5 }, cells: [[cell, second]], from: { sheet: clip.sheet, r0: 0, c0: 2 } }])
expect(await o.formula(0, 0, 5)).toBe('=D1:D3*2')
expect(await shown(o, ['E1', 'E3', 'F1', 'F3'])).toEqual(['20', '28', '0', '0'])
expect(cellXml(await (await o.save()).text(SHEET), 'F1')).toMatch(/ cm="\d+"/)
})

test('#SPILL! of a file read: cleared when the cell in the way is', async () => {
const o = await openBook(await book({ sheets: [{ name: 'S', rows: rows([[1]]) }] }))
await o.set(0, 'B2', 'x')
Expand Down