Fix: CSV import parsing logic to correctly handle commas in quoted fields
This commit is contained in:
parent
350c720a7e
commit
d65603b566
1 changed files with 31 additions and 2 deletions
33
js/app.js
33
js/app.js
|
|
@ -989,7 +989,31 @@ async function parseCSV(csvText, api, onProgress) {
|
||||||
const lines = csvText.trim().split('\n');
|
const lines = csvText.trim().split('\n');
|
||||||
if (lines.length < 2) return [];
|
if (lines.length < 2) return [];
|
||||||
|
|
||||||
const headers = lines[0].split(',').map(h => h.replace(/"/g, '').trim());
|
// Robust CSV line parser that respects quotes
|
||||||
|
const parseLine = (text) => {
|
||||||
|
const values = [];
|
||||||
|
let current = '';
|
||||||
|
let inQuote = false;
|
||||||
|
|
||||||
|
for (let i = 0; i < text.length; i++) {
|
||||||
|
const char = text[i];
|
||||||
|
|
||||||
|
if (char === '"') {
|
||||||
|
inQuote = !inQuote;
|
||||||
|
} else if (char === ',' && !inQuote) {
|
||||||
|
values.push(current);
|
||||||
|
current = '';
|
||||||
|
} else {
|
||||||
|
current += char;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
values.push(current);
|
||||||
|
|
||||||
|
// Clean up quotes: remove surrounding quotes and unescape double quotes if any
|
||||||
|
return values.map(v => v.trim().replace(/^"|"$/g, '').replace(/""/g, '"').trim());
|
||||||
|
};
|
||||||
|
|
||||||
|
const headers = parseLine(lines[0]);
|
||||||
const rows = lines.slice(1);
|
const rows = lines.slice(1);
|
||||||
|
|
||||||
const tracks = [];
|
const tracks = [];
|
||||||
|
|
@ -998,13 +1022,18 @@ async function parseCSV(csvText, api, onProgress) {
|
||||||
|
|
||||||
for (let i = 0; i < rows.length; i++) {
|
for (let i = 0; i < rows.length; i++) {
|
||||||
const row = rows[i];
|
const row = rows[i];
|
||||||
const values = row.split(',').map(v => v.replace(/"/g, '').trim());
|
if (!row.trim()) continue; // Skip empty lines
|
||||||
|
|
||||||
|
const values = parseLine(row);
|
||||||
|
|
||||||
if (values.length >= headers.length) {
|
if (values.length >= headers.length) {
|
||||||
let trackTitle = '';
|
let trackTitle = '';
|
||||||
let artistNames = '';
|
let artistNames = '';
|
||||||
|
|
||||||
headers.forEach((header, index) => {
|
headers.forEach((header, index) => {
|
||||||
const value = values[index];
|
const value = values[index];
|
||||||
|
if (!value) return;
|
||||||
|
|
||||||
switch (header.toLowerCase()) {
|
switch (header.toLowerCase()) {
|
||||||
case 'track name':
|
case 'track name':
|
||||||
case 'title':
|
case 'title':
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue