Excel 脚本:'We found a problem with some content in ’filename.xlsx’.'
Exceljs: 'We found a problem with some content in ’filename.xlsx’.'
我正在尝试使用 Exceljs 以 Table 格式呈现 excel 文件,但在打开文件之前收到此警告:
We found a problem with some content in ’test.xlsx’. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
如果我单击“是”,它 'recovers' 文件和一切都是完美的,但我总是在打开之前收到警告。
这只会在我执行多个选项卡时发生,因为一个选项卡可以正常工作。
import Excel from 'exceljs'
const tabs = {
'FIRST TAB': [
{ URL: 'https://google.com', FOO: 10 },
{ URL: 'https://apple.com', FOO: 12.5 }
],
'SECOND TAB': [
{ URL: 'https://google.com', FOO: 10 },
{ URL: 'https://apple.com', FOO: 22.5 }
]
}
;(async () => {
const workbook = new Excel.Workbook()
let worksheet = {}
for (const [label, tab] of Object.entries(tabs)) {
worksheet = workbook.addWorksheet(label)
worksheet.state = 'visible'
const columns = Object.keys(tab[0]).map((items) => ({
name: items,
filterButton: true
}))
const rows = tab.map((entry) => Object.values(entry))
workbook.getWorksheet(label).addTable({
name: label,
ref: 'A1',
headerRow: true,
columns,
rows
})
}
// Write to excel
await workbook.xlsx.writeFile(`test.xlsx`)
})()
问题是由 table 名称中的 space 引起的。
修复它的一种方法是用下划线替换 space,这实际上是 Excel 在 'fixes' 文件时所做的。
workbook.getWorksheet(label).addTable({
name: label.replace(' ', '_'),
ref: 'A1',
headerRow: true,
columns,
rows
})
我正在尝试使用 Exceljs 以 Table 格式呈现 excel 文件,但在打开文件之前收到此警告:
We found a problem with some content in ’test.xlsx’. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
如果我单击“是”,它 'recovers' 文件和一切都是完美的,但我总是在打开之前收到警告。
这只会在我执行多个选项卡时发生,因为一个选项卡可以正常工作。
import Excel from 'exceljs'
const tabs = {
'FIRST TAB': [
{ URL: 'https://google.com', FOO: 10 },
{ URL: 'https://apple.com', FOO: 12.5 }
],
'SECOND TAB': [
{ URL: 'https://google.com', FOO: 10 },
{ URL: 'https://apple.com', FOO: 22.5 }
]
}
;(async () => {
const workbook = new Excel.Workbook()
let worksheet = {}
for (const [label, tab] of Object.entries(tabs)) {
worksheet = workbook.addWorksheet(label)
worksheet.state = 'visible'
const columns = Object.keys(tab[0]).map((items) => ({
name: items,
filterButton: true
}))
const rows = tab.map((entry) => Object.values(entry))
workbook.getWorksheet(label).addTable({
name: label,
ref: 'A1',
headerRow: true,
columns,
rows
})
}
// Write to excel
await workbook.xlsx.writeFile(`test.xlsx`)
})()
问题是由 table 名称中的 space 引起的。
修复它的一种方法是用下划线替换 space,这实际上是 Excel 在 'fixes' 文件时所做的。
workbook.getWorksheet(label).addTable({
name: label.replace(' ', '_'),
ref: 'A1',
headerRow: true,
columns,
rows
})