@mixd-id/web-scaffold 0.1.240411103 → 0.1.240411104

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mixd-id/web-scaffold",
3
3
  "private": false,
4
- "version": "0.1.240411103",
4
+ "version": "0.1.240411104",
5
5
  "scripts": {
6
6
  "dev": "vite serve",
7
7
  "build": "vite build",
@@ -5,8 +5,8 @@ const glob = require("glob");
5
5
  const Exceljs = require("exceljs")
6
6
  const { saveBuffer, unflatten } = require('./helpers.js')
7
7
 
8
- const getCellText = (cell) => {
9
- if(!cell) return ''
8
+ const getCellText = (cell, image) => {
9
+ if(!cell) return image?.buffer ?? ''
10
10
 
11
11
  let value = ''
12
12
  if(cell.formulaType === 1){
@@ -22,10 +22,40 @@ const getCellText = (cell) => {
22
22
  value = cell.value
23
23
  }
24
24
 
25
+ // Image anchored on the cell, return its raw buffer unless the cell holds a value
26
+ if(image?.buffer && (value === null || value === undefined || value === ''))
27
+ value = image.buffer
28
+
25
29
  return value
26
30
 
27
31
  }
28
32
 
33
+ const cellKey = (row, col) => row + ',' + col
34
+
35
+ // Maps the cell an image is anchored on to its media entry ({ buffer, extension, name }).
36
+ // Only the top left anchor counts, an image spilling over its neighbours stays on one cell.
37
+ const getWorksheetImages = (workbook, worksheet) => {
38
+
39
+ const images = new Map()
40
+
41
+ for(const item of worksheet.getImages() ?? []){
42
+
43
+ const tl = item.range?.tl
44
+ if(!tl) continue
45
+
46
+ const media = workbook.getImage(parseInt(item.imageId))
47
+ if(!media?.buffer) continue
48
+
49
+ // Anchors are 0 based, cell addresses are 1 based
50
+ const key = cellKey(tl.nativeRow + 1, tl.nativeCol + 1)
51
+
52
+ if(!images.has(key))
53
+ images.set(key, media)
54
+ }
55
+
56
+ return images
57
+ }
58
+
29
59
  const analyseRequest = async(req) => {
30
60
 
31
61
  const file = req.files[0]
@@ -143,6 +173,8 @@ const importRequest = async(req) => {
143
173
  for(const worksheet of workbook.worksheets){
144
174
  const doc = docs.find(_ => _.name === worksheet.name)
145
175
 
176
+ const cellImages = getWorksheetImages(workbook, worksheet)
177
+
146
178
  let rows = []
147
179
  const row = worksheet.getRow(2)
148
180
  let isInfoRow = false
@@ -174,44 +206,55 @@ const importRequest = async(req) => {
174
206
  }))
175
207
  }
176
208
 
209
+ // Rows holding nothing but an image are not visited by eachRow, collect them too
210
+ const rowNumbers = new Set()
177
211
  worksheet.eachRow((row, index) => {
178
- if(index >= rowStart){
179
- const obj = {}
180
-
181
- // Check if cells in row 2 and 3 is notes in italic
182
- if(index <= 3){
183
- let isItalic = false
184
- for(let idx in keys){
185
- const key = keys[idx]
186
- if(key.value && columnAddress[key.value]){
187
- const cell = row.getCell(columnAddress[key.value])
188
- if(cell.font && cell.font.italic){
189
- isItalic = true
190
- break
191
- }
212
+ if(index >= rowStart)
213
+ rowNumbers.add(index)
214
+ })
215
+ for(const key of cellImages.keys()){
216
+ const index = parseInt(key.split(',')[0])
217
+ if(index >= rowStart)
218
+ rowNumbers.add(index)
219
+ }
220
+
221
+ for(const index of [...rowNumbers].sort((a, b) => a - b)){
222
+ const row = worksheet.getRow(index)
223
+ const obj = {}
224
+
225
+ // Check if cells in row 2 and 3 is notes in italic
226
+ if(index <= 3){
227
+ let isItalic = false
228
+ for(let idx in keys){
229
+ const key = keys[idx]
230
+ if(key.value && columnAddress[key.value]){
231
+ const cell = row.getCell(columnAddress[key.value])
232
+ if(cell.font && cell.font.italic){
233
+ isItalic = true
234
+ break
192
235
  }
193
236
  }
194
-
195
- if(isItalic)
196
- return
197
237
  }
198
238
 
199
- if(keys){
200
- for(let idx in keys){
201
- const key = keys[idx]
202
- if(key.value && columnAddress[key.value]){
203
- const cell = row.getCell(columnAddress[key.value])
204
- obj[key.key] = getCellText(cell)
205
- }
239
+ if(isItalic)
240
+ continue
241
+ }
242
+
243
+ if(keys){
244
+ for(let idx in keys){
245
+ const key = keys[idx]
246
+ if(key.value && columnAddress[key.value]){
247
+ const cell = row.getCell(columnAddress[key.value])
248
+ obj[key.key] = getCellText(cell, cellImages.get(cellKey(index, columnAddress[key.value])))
206
249
  }
207
250
  }
208
- else{
209
- //Object.assign(obj, row)
210
- }
211
-
212
- rows.push(unflatten(obj))
213
251
  }
214
- })
252
+ else{
253
+ //Object.assign(obj, row)
254
+ }
255
+
256
+ rows.push(unflatten(obj))
257
+ }
215
258
 
216
259
  Object.assign(doc, {
217
260
  rows