Skip to content

Commit

Permalink
add xlsx importer
Browse files Browse the repository at this point in the history
  • Loading branch information
olifolkerd committed Mar 23, 2024
1 parent 8eb8da4 commit 890f92e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
20 changes: 14 additions & 6 deletions dist/js/tabulator_esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5078,7 +5078,7 @@ function pdf(list, options = {}, setFileContents){
setFileContents(doc.output("arraybuffer"), "application/pdf");
}

function xlsx(list, options, setFileContents){
function xlsx$1(list, options, setFileContents){
var self = this,
sheetName = options.sheetName || "Sheet1",
workbook = XLSX.utils.book_new(),
Expand Down Expand Up @@ -5223,7 +5223,7 @@ var defaultDownloaders = {
json:json$1,
jsonLines:jsonLines,
pdf:pdf,
xlsx:xlsx,
xlsx:xlsx$1,
html:html$1,
};

Expand Down Expand Up @@ -13258,10 +13258,18 @@ function array$1 (input){
return input;
}

function xlsx(input, floop){
var workbook2 = XLSX.read(input);
var sheet = workbook2.Sheets[workbook2.SheetNames[0]];

return XLSX.utils.sheet_to_json(sheet, {});
}

var defaultImporters = {
csv:csv,
json:json,
array:array$1,
xlsx:xlsx,
};

class Import extends Module{
Expand Down Expand Up @@ -13320,11 +13328,11 @@ class Import extends Module{
return importer;
}

importFromFile(importFormat, extension){
importFromFile(importFormat, extension, importReader){
var importer = this.lookupImporter(importFormat);

if(importer){
return this.pickFile(extension)
return this.pickFile(extension, importReader)
.then(this.importData.bind(this, importer))
.then(this.structureData.bind(this))
.then(this.setData.bind(this))
Expand All @@ -13335,7 +13343,7 @@ class Import extends Module{
}
}

pickFile(extensions){
pickFile(extensions, importReader){
return new Promise((resolve, reject) => {
var input = document.createElement("input");
input.type = "file";
Expand All @@ -13345,7 +13353,7 @@ class Import extends Module{
var file = input.files[0],
reader = new FileReader();

switch(this.table.options.importReader){
switch(importReader || this.table.options.importReader){
case "buffer":
reader.readAsArrayBuffer(file);
break;
Expand Down
2 changes: 1 addition & 1 deletion dist/js/tabulator_esm.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/js/modules/Import/Import.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ export default class Import extends Module{
return importer;
}

importFromFile(importFormat, extension){
importFromFile(importFormat, extension, importReader){
var importer = this.lookupImporter(importFormat);

if(importer){
return this.pickFile(extension)
return this.pickFile(extension, importReader)
.then(this.importData.bind(this, importer))
.then(this.structureData.bind(this))
.then(this.setData.bind(this))
Expand All @@ -73,7 +73,7 @@ export default class Import extends Module{
}
}

pickFile(extensions){
pickFile(extensions, importReader){
return new Promise((resolve, reject) => {
var input = document.createElement("input");
input.type = "file";
Expand All @@ -83,7 +83,7 @@ export default class Import extends Module{
var file = input.files[0],
reader = new FileReader();

switch(this.table.options.importReader){
switch(importReader || this.table.options.importReader){
case "buffer":
reader.readAsArrayBuffer(file);
break;
Expand Down
2 changes: 2 additions & 0 deletions src/js/modules/Import/defaults/importers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import csv from './importers/csv.js';
import json from './importers/json.js';
import array from './importers/array.js';
import xlsx from './importers/xlsx.js';

export default {
csv:csv,
json:json,
array:array,
xlsx:xlsx,
};
6 changes: 6 additions & 0 deletions src/js/modules/Import/defaults/importers/xlsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function(input, floop){
var workbook2 = XLSX.read(input);
var sheet = workbook2.Sheets[workbook2.SheetNames[0]];

return XLSX.utils.sheet_to_json(sheet, {});
}

0 comments on commit 890f92e

Please sign in to comment.