FIX: Allow headings to have the same label (#46)

Fixes an issue where multiple headings with the same value would remove
columns from displaying.
This commit is contained in:
Penar Musaraj
2023-02-16 21:16:48 -05:00
committed by GitHub
parent d754e7a2ec
commit b45ffe48b7
6 changed files with 77 additions and 39 deletions
@@ -2,26 +2,23 @@
/**
* Generate markdown table from an array of objects
* Inspired by https://github.com/Ygilany/array-to-table
*
* @see {@link https://github.com/Ygilany/array-to-table|GitHub}:
*
* @param {Array} array Array of objects
* @param {String} columns Optional, table column names, otherwise taken from the keys of the first object
* @param {Array} array Array of objects
* @param {Array} columns Column headings
* @param {String} colPrefix Table column prefix
*
* @return {String} Markdown table
*/
export function arrayToTable(array, columns) {
export function arrayToTable(array, cols, colPrefix = "col") {
var table = "";
// Generate column list
var cols = columns ? columns.split(",") : Object.keys(array[0]);
// Generate table headers
table += "|";
table += cols.join(" | ");
table += "|\r\n|";
// Generate table header seperator
// Generate table header separator
table += cols
.map(function () {
return "---";
@@ -32,15 +29,17 @@ export function arrayToTable(array, columns) {
// Generate table body
array.forEach(function (item) {
table += "|";
table +=
cols
.map(function (key) {
return String(item[key] || "");
.map(function (_key, index) {
return String(item[`${colPrefix}${index}`] || "");
})
.join(" | ") + "|\r\n";
});
// Return table
console.log(table);
return table;
}
@@ -5,6 +5,7 @@ import {
findTableRegex,
tokenRange,
} from "../discourse-table-builder/lib/utilities";
import Component from "@glimmer/component";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
@@ -12,6 +13,7 @@ import I18n from "I18n";
import { schedule } from "@ember/runloop";
import { tracked } from "@glimmer/tracking";
import { localeMapping } from "../discourse-table-builder/lib/locale-mapping";
export default class SpreadsheetEditor extends Component {
@tracked showEditReason = false;
@tracked loading = null;
@@ -244,10 +246,13 @@ export default class SpreadsheetEditor extends Component {
data.forEach((row) => {
const result = {};
headers.forEach((key, index) => (result[key] = row[index]));
headers.forEach((_key, index) => {
const columnKey = `col${index}`;
return (result[columnKey] = row[index]);
});
table.push(result);
});
return arrayToTable(table);
return arrayToTable(table, headers);
}
}