Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it } from 'vitest'
import { toWorkbook } from '@/components/material/useMaterialViewHelper.js'

describe('toWorkBook', () => {
it('replaces invalid characters', () => {
const sheets = createSheets('s?h*e[]e/t:1:')

const workbook = toWorkbook(sheets)

expect(workbook.SheetNames).toEqual(['sheet1'])
})

it('shortens sheets names with more than 31 characters', () => {
const sheetName = 'a'.repeat(32)
const sheets = createSheets(sheetName)

const workbook = toWorkbook(sheets)

expect(workbook.SheetNames).toEqual([sheetName.slice(0, 31)])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are plus ou moin testing that slice does what it does, so maybe use 'a'.repeat(31)?

})

it('does not overwrite sheets when shortening them makes them not unique anymore', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tripped over this test name because I assumed overwrite means changing the sheet name, but you mean it doesn't replace the first one with the same (resulting) name. Maybe "does not drop sheets ..."?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not dropping, its replacing the pointer in the map. I would call that overwriting, but i am open for better formulations.

const sheets = [
...createSheets('a'.repeat(32)),
...createSheets('a'.repeat(33)),
...createSheets('a'.repeat(34)),
]
const workbook = toWorkbook(sheets)

expect(Object.entries(workbook.Sheets)).toHaveLength(sheets.length)
expect(workbook.SheetNames.filter((name) => name.length > 31)).toEqual([])
})

it('does not overwrite sheets when they have the same name', () => {
const sheets = [...createSheets('sheet1'), ...createSheets('sheet1')]
const workbook = toWorkbook(sheets)

expect(Object.entries(workbook.Sheets)).toHaveLength(sheets.length)
})
})

const data = [['row1']]

function createSheets(sheetName) {
return [
{
sheetName: sheetName,
data,
},
]
}
32 changes: 25 additions & 7 deletions frontend/src/components/material/useMaterialViewHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ async function getSheets(camp, collection, materialList) {
)
}

function randomString(len) {
const p = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
return [...Array(len)].reduce((a) => a + p[~~(Math.random() * p.length)], '')
}

/**
* @param sheets {array} array of {sheetName: string, data: array}
* @returns {object} an XLSX workbook
*/
export function toWorkbook(sheets) {
const workbook = XLSX.utils.book_new()
sheets.forEach(({ sheetName, data }) => {
const worksheet = XLSX.utils.aoa_to_sheet(data)
const validSheetName = sheetName.replaceAll(/[?*[\]/\\:]/g, '')
let slicedSheetName = validSheetName.slice(0, 31)
if (workbook.SheetNames.includes(slicedSheetName)) {
slicedSheetName = validSheetName.slice(0, 28) + randomString(3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using a random string here?
Wouldn't a simple increment be more understandable?

Like:

MeinLangerPeriodeNam001
MeinLangerPeriodeNam002
...

}
workbook.SheetNames.push(slicedSheetName)
workbook.Sheets[slicedSheetName] = worksheet
})
return workbook
}

/**
* @param {object} camp
* @param {{value: {period: object, materialItems: {items: array}}[]}} collection
Expand All @@ -76,14 +100,8 @@ function downloadMaterialList(camp, collection, materialList) {
return async () => {
await camp.activities().$loadItems()

const workbook = XLSX.utils.book_new()
const sheets = await getSheets(camp, collection.value, materialList)
sheets.forEach(({ sheetName, data }) => {
const worksheet = XLSX.utils.aoa_to_sheet(data)
const validSheetName = sheetName.replaceAll(/[?*[\]/\\:]/g, '')
workbook.SheetNames.push(validSheetName)
workbook.Sheets[validSheetName] = worksheet
})
const workbook = toWorkbook(sheets)
XLSX.writeFile(workbook, generateFilename(camp, materialList))
}
}
Expand Down
Loading