Skip to content

Commit 963432e

Browse files
committed
nob list fixed with prefix
1 parent 8521820 commit 963432e

File tree

1 file changed

+24
-47
lines changed

1 file changed

+24
-47
lines changed

src/index.js

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -148,49 +148,33 @@ async function injectFormulasIntoSheet(workbook, data, config) {
148148
if (!primaryKey) throw new Error("Config must specify primaryKey")
149149
const allGroups = [primaryKey, ...lookupKeys, ...dependentKeys].map(k => Array.isArray(k) ? k : [k])
150150
const canonicalKeys = allGroups.map(g => g[0]) // always first alias = canonical key
151-
const arrayLengths = {}
152-
data.forEach((row) => {
153-
Object.keys(row).forEach((k) => {
154-
if (Array.isArray(row[k])) {
155-
const norm = k.toLowerCase()
156-
arrayLengths[norm] = Math.max(arrayLengths[norm] || 0, row[k].length)
157-
}
158-
})
159-
})
160-
// --- Build expanded headers ---
161-
let expandedKeys = []
162-
canonicalKeys.forEach((key) => {
163-
if (arrayLengths[key]) {
164-
for (let i = 0; i < arrayLengths[key]; i++) {
165-
expandedKeys.push(i === 0 ? key : `${key}_${i + 1}`)
166-
}
167-
} else {
168-
expandedKeys.push(key)
169-
}
170-
})
171151
hiddenSheet.getRows(1, hiddenSheet.rowCount).forEach(r => {
172152
r.eachCell(c => { c.value = null })
173153
})
174-
hiddenSheet.getRow(1).values = expandedKeys
175-
data.forEach((row, i) => {
176-
const baseRow = {}
177-
Object.keys(row).forEach(k => {
178-
baseRow[k.toLowerCase()] = row[k]
179-
})
180-
const rowValues = []
181-
canonicalKeys.forEach((key) => {
154+
hiddenSheet.getRow(1).values = canonicalKeys
155+
let currentRow = 2
156+
data.forEach((row) => {
157+
const baseRow = {}
158+
Object.keys(row).forEach(k => {
159+
baseRow[k.toLowerCase()] = row[k]
160+
})
161+
const maxArrayLength = Math.max(
162+
...canonicalKeys.map(key => Array.isArray(baseRow[key]) ? baseRow[key].length : 1)
163+
)
164+
for (let i = 0; i < maxArrayLength; i++) {
165+
const rowValues = canonicalKeys.map(key => {
182166
const val = baseRow[key]
183167
if (Array.isArray(val)) {
184-
for (let j = 0; j < arrayLengths[key]; j++) {
185-
rowValues.push(val[j] || "")
186-
}
168+
return val[i] || ""
187169
} else {
188-
rowValues.push(val || "")
170+
return i === 0 ? val || "" : ""
189171
}
190172
})
191-
hiddenSheet.getRow(i + 2).values = rowValues
173+
hiddenSheet.getRow(currentRow).values = rowValues
174+
currentRow++
175+
}
192176
})
193-
const lastRow = data.length + 1
177+
const lastRow = currentRow -1
194178
const primaryRange = `Lookups!$A$2:$A$${lastRow}`
195179

196180
const findCol = (aliases) => {
@@ -217,27 +201,19 @@ async function injectFormulasIntoSheet(workbook, data, config) {
217201
const colPrimary = findCol(primaryKey)
218202
if (!colPrimary) throw new Error(`Primary key column ${primaryKey} not found in sheet`)
219203

220-
const maxRow = Math.max(sheet.rowCount, 200)
221-
for (let row = 2; row <= maxRow; row++) {
204+
const row = 2
222205
const primaryCell = sheet.getRow(row).getCell(colPrimary)
223-
//primary dropdown
224-
primaryCell.dataValidation = {
225-
type: 'list',
226-
allowBlank: true,
227-
formulae: [primaryRange],
228-
}
229206
//Dependent dropdowns
230207
for (const depGroup of dependentKeys) {
231208
const colDep = findCol(depGroup)
232209
if (!colDep) continue
233210
const depKey = Array.isArray(depGroup) ? depGroup[0] : depGroup
234211
const depCell = sheet.getRow(row).getCell(colDep)
212+
const depColLetter = String.fromCharCode(65 + canonicalKeys.indexOf(depKey))
235213
depCell.dataValidation = {
236214
type: 'list',
237215
allowBlank: true,
238-
formulae: [`OFFSET(Lookups!$${String.fromCharCode(65 + canonicalKeys.indexOf(depKey))}$2,MATCH(${primaryCell.address},Lookups!$A$2:$A$${lastRow},0)-1,0,1,
239-
COUNTA(OFFSET(Lookups!$${String.fromCharCode(65 + canonicalKeys.indexOf(depKey))}$2,MATCH(${primaryCell.address},Lookups!$A$2:$A$${lastRow},0)-1,0,1,50)))`
240-
.replace(/\s+/g, ' ')],
216+
formulae: [`Lookups!$${depColLetter}$2:$${depColLetter}$${lastRow}`],
241217
}
242218
}
243219
//Lookup autofill
@@ -246,13 +222,14 @@ async function injectFormulasIntoSheet(workbook, data, config) {
246222
if (!colLookup) continue
247223
const lookupKey = Array.isArray(lookupGroup) ? lookupGroup[0] : lookupGroup
248224
const lookupCell = sheet.getRow(row).getCell(colLookup)
225+
const lookupColIndex = canonicalKeys.indexOf(lookupKey) + 1
249226
lookupCell.value = {
250-
formula: `IF(${primaryCell.address}="","",VLOOKUP(${primaryCell.address},Lookups!$A$2:$Z$${lastRow},${canonicalKeys.indexOf(lookupKey) + 1},FALSE))`
227+
formula: `IF(${primaryCell.address}="","",VLOOKUP(${primaryCell.address},Lookups!$A$2:$${String.fromCharCode(64 + canonicalKeys.length)}$${lastRow},${lookupColIndex},FALSE))`
251228
}
252229
}
253230

254231
}
255-
}
232+
256233

257234
const convertJsonToExcel = (jsonData) => {
258235
const workbook = xlsx.utils.book_new()

0 commit comments

Comments
 (0)