Skip to content

Commit

Permalink
Import/export xls files (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivansg44 authored Jun 11, 2020
1 parent e7a7d50 commit f02aada
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion main.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div class="dropdown-menu">
<span class="dropdown-item" id="new-dropdown-item">New</span>
<label for="open-file-input" class="dropdown-item mb-0" id="open-dropdown-item">Open</label>
<input type="file" accept=".xlsx,.tsv,.csv" class="form-control-file" id="open-file-input">
<input type="file" accept=".xlsx,.xls,.tsv,.csv" class="form-control-file" id="open-file-input">
<span class="dropdown-item" data-toggle="modal" data-target="#save-as-modal">Save As...</span>
</div>
</div>
Expand Down Expand Up @@ -97,6 +97,7 @@
<div class="input-group-append">
<select class="form-control" id="file-ext-save-as-select">
<option value="xlsx" selected>.xlsx</option>
<option value="xls">.xls</option>
<option value="tsv">.tsv</option>
<option value="csv">.csv</option>
</select>
Expand Down
6 changes: 4 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ const exportFile = (matrix, baseName, ext, xlsx) => {
xlsx.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
if (ext === 'xlsx') {
xlsx.writeFile(workbook, `${baseName}.xlsx`);
} else if (ext === 'xls') {
xlsx.writeFile(workbook, `${baseName}.xls`);
} else if (ext === 'tsv') {
xlsx.writeFile(workbook, `${baseName}.tsv`, {bookType: 'csv', FS: '\t'});
} else if (ext === 'csv') {
Expand All @@ -311,7 +313,7 @@ const exportFile = (matrix, baseName, ext, xlsx) => {

/**
* Read local file opened by user.
* Only reads `xlsx`, `csv` and `tsv` files.
* Only reads `xlsx`, `xlsx`, `csv` and `tsv` files.
* @param {File} file User file.
* @param {Object} xlsx SheetJS variable.
* @return {Promise<Array<Array<String>>>} Matrix populated by user's file data.
Expand Down Expand Up @@ -627,7 +629,7 @@ $(document).ready(() => {
$fileInput.change(() => {
const file = $fileInput[0].files[0];
const ext = file.name.split('.').pop();
const acceptedExts = ['xlsx', 'tsv', 'csv'];
const acceptedExts = ['xlsx', 'xls', 'tsv', 'csv'];
if (!acceptedExts.includes(ext)) {
const errMsg = `Only ${acceptedExts.join(', ')} files are supported`;
$('#open-err-msg').text(errMsg);
Expand Down

0 comments on commit f02aada

Please sign in to comment.