As the title states, I am trying to export a data table created in a web app to excel carrying the format. The main issue is that multiple values selected from a datalist create a string in excel (ex. 10, 11, 12 in app converts to 101112 in excel).
The code below was used before, and separated the values in excel, but it would not allow the html multiple datalist to display correctly. (ex. Instead of display the full list it would only display 10,11,12 etc., in the drop down after selecting 10, instead of a name and value originally in the list.)
//For Selecting Multiple Events
document.addEventListener("DOMContentLoaded", function () {
const separator = ',';
for (const input of document.getElementsByTagName("input")) {
if (!input.multiple) {
continue;
}
if (input.list instanceof HTMLDataListElement) {
const optionsValues = Array.from(input.list.options).map(opt => opt.value);
let valueCount = input.value.split(separator).length;
input.addEventListener("input", () => {
const currentValueCount = input.value.split(separator).length;
if (valueCount !== currentValueCount) {
const lsIndex = input.value.lastIndexOf(separator);
const str = lsIndex !== -1 ? input.value.substr(0, lsIndex) + separator : "";
filldatalist(input, optionsValues, str);
valueCount = currentValueCount;
}
});
}
}
function filldatalist(input, optionValues, optionPrefix) {
const list = input.list;
if (list && optionValues.length > 0) {
list.innerHTML = "";
const usedOptions = optionPrefix.split(separator).map(value => value.trim());
for (const optionsValue of optionValues) {
if (usedOptions.indexOf(optionsValue) < 0) {
const option = document.createElement("option");
option.value = optionPrefix + optionsValue;
list.append(option);
}
}
}
}
});
This is how the App should process the data during selection. Desired selection within App
This is how the App should display the data. Desired display in App datatable
This is how the data displays in Excel Values not separated by commas
This is the result using the code above. Undesired selection within the App