Skip to content

Commit

Permalink
featura: excel importer now reads values from all sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelPelegrina committed Apr 23, 2024
1 parent 4ef00b6 commit a29ac52
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,39 @@ <h1 class="m-2 p-2">Excel importer</h1>
<div class="container">
<h2>Instructions</h2>
<p>
1. This importer allows you to choose an excel file, see all the values of it in a table and then import those books into the database.
This importer allows you to choose an excel file, see all the values of it in a table and then import those books into the database.
</p>
<p>
2. Currently <b>supported extensions:</b>.
The importer assigns each value of a row depending on the column header. As a consequence the <b>first row of the excel file must contain</b> the following values:
</p>
<ul>
<li><b>Title</b></li>
<li><b>ISBN</b></li>
<li><b>Active</b></li>
<li><b>Price</b></li>
<li><b>Stock</b></li>
<li><b>Genre</b></li>
<li><b>Author</b></li>
<li><b>Format</b></li>
</ul>
<h2>Reminder</h2>
<p>
1. Currently <b>supported extensions:</b>.
</p>
<ul>
<li><b>.xlsx</b></li>
</ul>
<p>
3. All <b>new genres</b> used in the excel <b>will be created</b>.
2. All <b>new genres</b> used in the excel <b>will be created</b>.
</p>
<p>
4. All <b>new formats</b> used in the excel <b>will be created</b>.
3. All <b>new formats</b> used in the excel <b>will be created</b>.
</p>
<p>
5. Books are <b>identified by their ISBN</b>. This has the following consequences:</p>
4. Books are <b>identified by their ISBN</b>. This has the following consequences:</p>
<ul>
<li>If the ISBN is <b>not assigned yet</b>, the book will be <b>added</b>.</li>
<li>If the ISBN is <b>already assigned</b>, the book will be <b>updated</b>.</li>
<li>If the ISBN is <b>not assigned yet</b>, a new book will be <b>added</b>.</li>
<li>If the ISBN is <b>already assigned</b>, the existing book will be <b>updated</b>.</li>
</ul>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class BookExcelParserComponent implements AfterViewInit{

protected parsedData: Book[] = [];

protected fileReader = new FileReader();

/**
* Constructor
* @param {BookService} service - The BookService for handling book-related operations.
Expand Down Expand Up @@ -108,22 +110,16 @@ export class BookExcelParserComponent implements AfterViewInit{
const file = event.target.files[0];

if(file && requiredFileType(file, allowedParserExtensions)){
let fileReader = new FileReader();

this.isLoading = true;

fileReader.readAsBinaryString(file);
this.fileReader.readAsBinaryString(file);

fileReader.onload = (e) => {
this.fileReader.onload = (e) => {
this.parsedData = [];

this.excelSelected = true;

const workBook = XLSX.read(fileReader.result, {type:'binary'});

const sheetNames = workBook.SheetNames;

this.data = XLSX.utils.sheet_to_json(workBook.Sheets[sheetNames[0]]);
this.readExcelWorkBook();

this.dataSource.data = this.data;

Expand All @@ -141,6 +137,22 @@ export class BookExcelParserComponent implements AfterViewInit{
}
}

/**
* Reads an Excel workbook and extracts data from all its sheets into a single array.
* This method is useful for processing Excel files in web applications where data from multiple sheets needs to be combined or displayed together.
*/
private readExcelWorkBook() {
const workBook = XLSX.read(this.fileReader.result, {type:'binary'});

const sheetNames = workBook.SheetNames;

for (let index = 0; index < sheetNames.length; index++) {
const sheetName = sheetNames[index];
const sheet = workBook.Sheets[sheetName]
this.data = this.data.concat(XLSX.utils.sheet_to_json(sheet));
}
}

/**
* Parses each Excel book entry into a Book and populates the parsedData array.
*/
Expand Down

0 comments on commit a29ac52

Please sign in to comment.