Skip to content

Commit 8674cfd

Browse files
authored
Merge pull request #5 from divamtech/ishika-excel-category-fixes
nob list fixed with prefix
2 parents 8521820 + f3ef6d1 commit 8674cfd

File tree

1 file changed

+84
-90
lines changed

1 file changed

+84
-90
lines changed

src/index.js

Lines changed: 84 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -138,119 +138,113 @@ const uploadToAWS = async (config, excelData) => {
138138
}
139139

140140
async function injectFormulasIntoSheet(workbook, data, config) {
141-
const sheetName = config.dependentSheet
142-
const sheet = workbook.getWorksheet(sheetName) || workbook.worksheets[0]
143-
if (!sheet) throw new Error('Target sheet not found')
144141
const lookupSheet = config.lookupSheet
145142
const hiddenSheet = workbook.getWorksheet(lookupSheet) || workbook.addWorksheet(lookupSheet)
146143
hiddenSheet.state = 'veryHidden'
147-
const { primaryKey, dependentKeys = [], lookupKeys = [] } = config
148-
if (!primaryKey) throw new Error("Config must specify primaryKey")
149-
const allGroups = [primaryKey, ...lookupKeys, ...dependentKeys].map(k => Array.isArray(k) ? k : [k])
150-
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-
})
171-
hiddenSheet.getRows(1, hiddenSheet.rowCount).forEach(r => {
172-
r.eachCell(c => { c.value = null })
173-
})
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) => {
182-
const val = baseRow[key]
183-
if (Array.isArray(val)) {
184-
for (let j = 0; j < arrayLengths[key]; j++) {
185-
rowValues.push(val[j] || "")
186-
}
187-
} else {
188-
rowValues.push(val || "")
189-
}
190-
})
191-
hiddenSheet.getRow(i + 2).values = rowValues
192-
})
193-
const lastRow = data.length + 1
194-
const primaryRange = `Lookups!$A$2:$A$${lastRow}`
195144

196-
const findCol = (aliases) => {
145+
const headerKeys = [
146+
...new Set(
147+
data.flatMap(row => Object.keys(row))
148+
)
149+
];
150+
151+
// Column-wise data insertion into hidden sheet
152+
headerKeys.forEach((key, idx) => {
153+
const values = data.flatMap(row => row[key] ?? []);
154+
values.forEach((val, rowIndex) => {
155+
hiddenSheet.getRow(rowIndex + 2).getCell(idx + 1).value = val ?? "";
156+
});
157+
hiddenSheet.getRow(1).getCell(idx + 1).value = key;
158+
});
159+
const findCol = (sheet, aliases) => {
197160
const headerRow = sheet.getRow(1)
198161
const lookupNames = Array.isArray(aliases) ? aliases : [aliases]
199162

200163
for (let col = 1; col <= sheet.columnCount; col++) {
201164
const val = headerRow.getCell(col)?.value
202-
const text = typeof val === 'object'
203-
? (val?.richText?.map(rt => rt.text).join('') || val?.result || '')
204-
: (val || '')
205-
const normalized = String(text).trim().toLowerCase()
165+
let text = '';
166+
167+
if (typeof val === 'object') {
168+
if (Array.isArray(val.richText)) {
169+
text = val.richText.map(rt => rt.text).join('');
170+
} else if (val.result) {
171+
text = val.result;
172+
}
173+
} else if (val !== null && val !== undefined) {
174+
text = val;
175+
}
176+
177+
const normalized = String(text || '').trim().toLowerCase();
206178

207179
for (const alias of lookupNames) {
208-
const normAlias = alias.trim().toLowerCase()
180+
if (!alias) continue;
181+
const normAlias = String(alias).trim().toLowerCase()
209182
if (normalized === normAlias) return col
210-
if (normalized.replace(/\s+/g, "_") === normAlias) return col // "Country Code" -> country_code
211-
if (normalized.replace(/[^a-z0-9]/gi, "") === normAlias.replace(/[^a-z0-9]/gi, "")) return col // remove *, etc.
183+
if (normalized.replace(/\s+/g, "_") === normAlias) return col
184+
if (normalized.replace(/[^a-z0-9]/gi, "") === normAlias.replace(/[^a-z0-9]/gi, "")) return col
212185
}
213186
}
214187
return null
215188
}
216-
// --- Primary key col ---
217-
const colPrimary = findCol(primaryKey)
218-
if (!colPrimary) throw new Error(`Primary key column ${primaryKey} not found in sheet`)
219-
220-
const maxRow = Math.max(sheet.rowCount, 200)
221-
for (let row = 2; row <= maxRow; row++) {
222-
const primaryCell = sheet.getRow(row).getCell(colPrimary)
223-
//primary dropdown
224-
primaryCell.dataValidation = {
225-
type: 'list',
226-
allowBlank: true,
227-
formulae: [primaryRange],
189+
for (const sheetConfig of config.dependentSheet) {
190+
const sheet = workbook.getWorksheet(sheetConfig.name);
191+
if (!sheet) throw new Error(`Sheet ${sheetConfig.name} not found`);
192+
193+
const lastRow = hiddenSheet.lastRow.number;
194+
195+
//Primary key dropdown (if exists)
196+
let primaryCol = null;
197+
let primaryCell = null;
198+
199+
if (sheetConfig.primaryKey) {
200+
primaryCol = findCol(sheet, sheetConfig.primaryKey);
201+
if (!primaryCol) throw new Error(`Primary key not found in ${sheetConfig.name}`);
202+
primaryCell = sheet.getRow(2).getCell(primaryCol);
203+
204+
const lookupCol = findCol(hiddenSheet, sheetConfig.primaryKey);
205+
if (!lookupCol) throw new Error(`Primary key not found in Lookups sheet`);
206+
const lookupColLetter = String.fromCharCode(64 + lookupCol);
207+
208+
primaryCell.dataValidation = {
209+
type: "list",
210+
allowBlank: true,
211+
formulae: [`${hiddenSheet.name}!$${lookupColLetter}$2:$${lookupColLetter}$${lastRow}`],
212+
};
228213
}
229214
//Dependent dropdowns
230-
for (const depGroup of dependentKeys) {
231-
const colDep = findCol(depGroup)
232-
if (!colDep) continue
233-
const depKey = Array.isArray(depGroup) ? depGroup[0] : depGroup
234-
const depCell = sheet.getRow(row).getCell(colDep)
215+
for (const depGroup of sheetConfig.dependentKeys || []) {
216+
const depCol = findCol(sheet, depGroup);
217+
if (!depCol) continue;
218+
219+
const depCell = sheet.getRow(2).getCell(depCol);
220+
221+
const lookupCol = findCol(hiddenSheet, depGroup);
222+
if (!lookupCol) continue;
223+
const lookupColLetter = String.fromCharCode(64 + lookupCol);
224+
235225
depCell.dataValidation = {
236-
type: 'list',
226+
type: "list",
237227
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, ' ')],
241-
}
228+
formulae: [`${hiddenSheet.name}!$${lookupColLetter}$2:$${lookupColLetter}$${lastRow}`],
229+
};
242230
}
243-
//Lookup autofill
244-
for (const lookupGroup of lookupKeys) {
245-
const colLookup = findCol(lookupGroup)
246-
if (!colLookup) continue
247-
const lookupKey = Array.isArray(lookupGroup) ? lookupGroup[0] : lookupGroup
248-
const lookupCell = sheet.getRow(row).getCell(colLookup)
249-
lookupCell.value = {
250-
formula: `IF(${primaryCell.address}="","",VLOOKUP(${primaryCell.address},Lookups!$A$2:$Z$${lastRow},${canonicalKeys.indexOf(lookupKey) + 1},FALSE))`
231+
232+
//Lookup autofill (only if primary key exists)
233+
if (primaryCol && sheetConfig.lookupKeys) {
234+
for (const lookupGroup of sheetConfig.lookupKeys) {
235+
const lookupCol = findCol(sheet, lookupGroup);
236+
if (!lookupCol) continue;
237+
const lookupKey = Array.isArray(lookupGroup) ? lookupGroup[0] : lookupGroup;
238+
const lookupColIndex = headerKeys.indexOf(lookupKey) + 1;
239+
const lookupCell = sheet.getRow(2).getCell(lookupCol);
240+
241+
lookupCell.value = {
242+
formula: `IF(${primaryCell.address}="","",VLOOKUP(${primaryCell.address},${hiddenSheet.name}!$A$2:$${String.fromCharCode(
243+
64 + headerKeys.length
244+
)}$${lastRow},${lookupColIndex},FALSE))`,
245+
};
251246
}
252247
}
253-
254248
}
255249
}
256250

0 commit comments

Comments
 (0)