table-data-extract v0.0.2
table-data-extract
table-data-extract
is a TypeScript package that provides a utility function for extracting data from Excel files (.xlsx
or .xls
format). It allows you to extract specific columns or all columns from a given Excel file.
Installation
You can install the package using npm:
npm install table-data-extract
Usage
Import the function
import { extractColumnsFromXLSX, ExtractOptions } from 'table-data-extract';
Define the options
Create an ExtractOptions object with the required properties:
const options: ExtractOptions = {
filePath: 'path/to/your/file.xlsx', // Path to the Excel file
columnsToExtract: ['EMAIL_ADDRESS', 'STUDENT_CODE'], // Columns to extract (case-insensitive)
skipRow: 2 // Optional: Row number to start extracting data from (default: 0)
};
filePath
(required): Path to the Excel file you want to extract data from.columnsToExtract
(required): Array of column names to extract.skipRow
(optional): Row number to start extracting data from. Defaults to 0 if not provided.
Call the function
const data = extractColumnsFromXLSX(options);
console.log(data);
The extractColumnsFromXLSX function returns a Promise that resolves to a 2D array of any values, where each sub-array represents a row, and each element in the sub-array represents the extracted column value for that row. The function also removes the uploaded file after processing.
Examples
Extract specific columns
import { extractColumnsFromXLSX, ExtractOptions } from 'table-data-extract';
const options: ExtractOptions = {
filePath: 'path/to/your/file.xlsx',
columnsToExtract: ['Email', 'Name', 'Age'],
skipRow: 3 // Assuming the data starts from row 4
};
const data = await extractColumnsFromXLSX(options);
console.log(data);
// Output: [['john@example.com', 'John Doe', 25], ['jane@example.com', 'Jane Smith', 30], ...]
Error Handling
The extractColumnsFromXLSX function throws errors in the following cases:
- If one or more columns specified in columnsToExtract are not found in the header row.
Additionally, it logs any errors that occur during the extraction process to the console. Make sure to handle these errors appropriately in your code.
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
License
This project is licensed under the MIT License.