xlsx-template-browser 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ export const valueToString = (value) => {
7
7
  if (!value) return ''
8
8
  if (Array.isArray(value)) return value.map(valueToString).toString()
9
9
  if (typeof value === 'object') value = JSON.stringify(value)
10
- return value.toString()
10
+ return escapeXml(value.toString())
11
11
  }
12
12
 
13
13
  /**
@@ -37,7 +37,7 @@ export const guessDataType = (value) => {
37
37
  return { cellType: 'n', value: dateToExcel(value) }
38
38
  }
39
39
  if (typeof value === 'boolean') return { cellType: 'b', value: value + 0 }
40
- return { cellType: 's', value: valueToString(value )}
40
+ return { cellType: 's', value: valueToString(value) }
41
41
  }
42
42
 
43
43
  /**
@@ -61,10 +61,14 @@ export const createSharedString = (text, template) => {
61
61
  * @param {string|number} options.row - The row reference for the cell.
62
62
  * @param {string|number} options.column - The column reference (either string or numeric) for the cell.
63
63
  * @param {Element} options.template - (Optional) An existing template to clone for the cell.
64
- * @param {string} options.cellType - The type of the cell (e.g., 's', 'n', 'b') for Excel formatting.
64
+ * @param {string} options.cellType - The type of the cell (e.g., 's', 'n', 'b', 'f') for Excel formatting.
65
65
  * @returns {Element} - The created cell element.
66
66
  */
67
67
  export const createCell = ({ value, row, column, template, cellType }) => {
68
+ // If the cell is not a string, then skip
69
+ if (!cellType && !value) {
70
+ return template
71
+ }
68
72
  // Clone initial node with the style or create a new one
69
73
  const cell = template ? template.cloneNode() : document.createElement('c')
70
74
  // Create a cell reference
@@ -111,3 +115,24 @@ export const getExcelColumnIndex = (columnName) => {
111
115
  return index
112
116
  }
113
117
 
118
+ /**
119
+ * Escapes unsafe XML characters in a given string to ensure its safe inclusion
120
+ * in XML documents, preventing parsing issues.
121
+ *
122
+ * @param {string} unsafe - The input string containing XML-unsafe characters.
123
+ * @returns {string} - A new string with XML-unsafe characters replaced by their
124
+ * corresponding HTML entities.
125
+ */
126
+
127
+ const unsafeChars = {
128
+ '<': '&lt;',
129
+ '>': '&gt;',
130
+ '&': '&amp;',
131
+ '\'': '&apos;',
132
+ '"': '&quot;'
133
+ }
134
+
135
+ export const escapeXml = (unsafe) => {
136
+ return unsafe.replace(/[<>&'"]/g, (c) => { return unsafeChars[c] })
137
+ }
138
+
package/lib/main.js CHANGED
@@ -50,6 +50,16 @@ const replace = async (template, data) => {
50
50
  // We need to replace only text values with the new ones, therefore we process shared strings first
51
51
  // Get all string items
52
52
  xml.querySelectorAll('si').forEach((si, i) => {
53
+ // Get all rich format tags
54
+ const r = si.querySelector('r')
55
+ if (r) {
56
+ const xmlString = si.innerHTML
57
+ const newString = xmlString.replace(globalAccessorRegex, s => valueToString(getByNotation(data, s.replace(/\$\{|}/g, ''))))
58
+ si.innerHTML = newString
59
+ newSharedStrings.push(si)
60
+ valuesToReplace[i] = { value: newSharedStrings.length - 1, cellType: 's' }
61
+ return false
62
+ }
53
63
  // Get all text tags
54
64
  const t = si.querySelector('t')
55
65
  if (!t) return false
@@ -69,7 +79,8 @@ const replace = async (template, data) => {
69
79
  let { accessor, table } = match.groups
70
80
  // If we have table values, we need to process them in a separate way
71
81
  const isTable = typeof table === 'string'
72
- const value = getByNotation(data, accessor)
82
+ let value = getByNotation(data, accessor)
83
+ if (!value) return false
73
84
  // Save tables for further processing
74
85
  if (isTable) {
75
86
  valuesToReplace[i] = { isTable, value: value.map(guessDataType) }
@@ -125,6 +136,12 @@ const replace = async (template, data) => {
125
136
  let newIndex = index + cellOffset
126
137
  // Get the cell value tag
127
138
  let v = c.querySelector('v') || {}
139
+ // Check if the cell contains string
140
+ let f = c.getAttribute('t')
141
+ if (!f || f !== 's') {
142
+ newCells[newIndex] = Object.assign({}, { template: c })
143
+ return false
144
+ }
128
145
  // Get the new value to replace
129
146
  let newValue = valuesToReplace[v.textContent] || {}
130
147
  if (!Array.isArray(newValue)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xlsx-template-browser",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "On-the-fly generation of .xlsx (Excel) files directly within the browser using templates constructed in Excel.",
5
5
  "main": "./lib/index",
6
6
  "files": [