xlsx-template-browser 0.1.5 → 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
  /**
@@ -65,12 +65,9 @@ export const createSharedString = (text, template) => {
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 formula, then skip
69
- if (cellType === 'f') {
70
- // Check precalculated value
71
- const v = value.querySelector('v')
72
- if (v) v.remove()
73
- return value
68
+ // If the cell is not a string, then skip
69
+ if (!cellType && !value) {
70
+ return template
74
71
  }
75
72
  // Clone initial node with the style or create a new one
76
73
  const cell = template ? template.cloneNode() : document.createElement('c')
@@ -118,3 +115,24 @@ export const getExcelColumnIndex = (columnName) => {
118
115
  return index
119
116
  }
120
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
@@ -126,13 +136,11 @@ const replace = async (template, data) => {
126
136
  let newIndex = index + cellOffset
127
137
  // Get the cell value tag
128
138
  let v = c.querySelector('v') || {}
129
- // Check if the cell contains formula, and skip
130
- let f = c.querySelector('f')
131
- if (f) {
132
- const value = c
133
- const cellType = 'f'
134
- newCells[newIndex] = Object.assign({}, { value, cellType }, { template: c })
135
- return false
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
136
144
  }
137
145
  // Get the new value to replace
138
146
  let newValue = valuesToReplace[v.textContent] || {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xlsx-template-browser",
3
- "version": "0.1.5",
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": [