Features supported by luckyexcel
masterWhen importing Excel files, luckyexcel supports the following elements for adaptation to Luckysheet:
- Cell style
- Cell border
- Cell format (e.g., number format, date, percentage, etc.)
- Formula
repository·master·Indexed 19 days ago
https://github.com/dream-num/luckyexcelAn Excel import and export library designed to adapt .xlsx files for use with Luckysheet. It converts .xlsx files into a JSON format that Luckysheet can render, supporting cell styles, borders, formulas, and various cell formats. The library provides the transformExcelToLucky method for local files/buffers and transformExcelToLuckyByUrl for remote files.
When importing Excel files, luckyexcel supports the following elements for adaptation to Luckysheet:
Install the package using npm for use in ES modules or Node.js environments.
npm install luckyexcelYou can include luckyexcel directly in your HTML using a CDN script tag. The library will be available under the global window.LuckyExcel object.
<script src="https://cdn.jsdelivr.net/npm/luckyexcel/dist/luckyexcel.umd.js"></script>
<script>
// 'file' is the obtained xlsx file
LuckyExcel.transformExcelToLucky(
file,
function(exportJson, luckysheetfile){
// Initialize luckysheet with the converted data
luckysheet.create({
container: 'luckysheet',
data: exportJson.sheets,
title: exportJson.info.name,
userInfo: exportJson.info.name.creator
});
},
function(err){
console.error('Import failed. Is your file a valid xlsx?');
});
</script>To use Luckyexcel in a browser environment without a module bundler, include the UMD build via JSDelivr. Use LuckyExcel.transformExcelToLucky to convert an .xlsx file into a format compatible with Luckysheet.
Note: You must have Luckysheet already loaded and a container initialized to use the resulting data.
<script src="https://cdn.jsdelivr.net/npm/luckyexcel/dist/luckyexcel.umd.js"></script>
<script>
// Ensure 'file' is a valid xlsx file object
LuckyExcel.transformExcelToLucky(
file,
function(exportJson, luckysheetfile){
// Use the converted data to initialize or update Luckysheet
luckysheet.create({
container: 'luckysheet', // luckysheet is the container id
data: exportJson.sheets,
title: exportJson.info.name,
userInfo: exportJson.info.name.creator
});
},
function(err){
console.error('Import failed. Is your file a valid xlsx?', err);
}
);
</script>The LuckySheetCelldata class is a core component used to transform raw XML cell elements from an Excel file into a structured format compatible with LuckySheet. It handles the extraction of cell values, formulas, and complex styling information.
When instantiated, it automatically processes the following:
r) and column (c) indices.SharedString tables, or inlineStr types.= to the formula string).LuckySheetCelldataValue structure.inlineStr and complex text formatting (bold, italic, underline, font family, etc.) for individual characters or segments within a cell.// Note: The constructor is used internally by the parser to transform XML elements.
// It requires several dependencies from the ReadXml process.
new LuckySheetCelldata(
cell: Element,
styles: IStyleCollections,
sharedStrings: Element[],
mergeCells: Element[],
sheetFile: string,
ReadXml: ReadXml
);The core functionality of luckyexcel is the transformExcelToLucky method. It converts .xlsx files (note: .xls is not supported) into a JSON format compatible with Luckysheet.
Note: This library only supports the .xlsx format.
// Example using ES modules
import LuckyExcel from 'luckyexcel'
// 'data' should be the file content/buffer from an .xlsx file
LuckyExcel.transformExcelToLucky(data,
function(exportJson, luckysheetfile){
// exportJson contains the converted worksheet data
},
function(error){
// handle error if conversion fails
}
)Import LuckyExcel from the package and use transformExcelToLucky to process an .xlsx file. The method accepts a file object and two callbacks: a success callback providing the converted JSON and a failure callback for error handling.
import LuckyExcel from 'luckyexcel'
// 'file' is the obtained xlsx file
LuckyExcel.transformExcelToLucky(
file,
function(exportJson, luckysheetfile){
// Handle converted worksheet data here
},
function(error){
// Handle errors here
}
)The core method of Luckyexcel. It converts .xlsx files into a JSON structure that Luckysheet can consume.
Parameters:
file (File | Buffer): The source .xlsx file or buffer.successCallback (Function): Called on success. Receives (exportJson, luckysheetfile).exportJson: Contains sheets (the data) and info (metadata like name and creator).luckysheetfile: Additional file-related data.errorCallback (Function): Called on failure. Receives (error).In a Node.js environment, use require to import the library. You can pass the raw buffer/data read from the file system (using fs) directly into transformExcelToLucky.
var fs = require("fs");
var LuckyExcel = require('luckyexcel');
// Read an xlsx file
fs.readFile("House cleaning checklist.xlsx", function(err, data) {
if (err) throw err;
LuckyExcel.transformExcelToLucky(data, function(exportJson, luckysheetfile){
// Handle converted worksheet data here
});
});Use LuckyExcel.transformExcelToLuckyByUrl() to convert an Excel file located at a specific URL into Luckysheet-compatible JSON. This is useful for remote files where you don't have a local File object.
Parameters:
url: The string URL of the .xlsx file.name: The name to be associated with the file.callBack (optional): A function called upon success. It receives:files: An IuploadfileList object containing the parsed Excel data.fs: A string representing the raw Luckysheet file content.errorHandler (optional): A function called if an error occurs.LuckyExcel.transformExcelToLuckyByUrl(
'https://example.com/sample.xlsx',
'sample.xlsx',
(exportJson, luckysheetfile) => {
console.log('Parsed JSON:', exportJson);
},
(err) => {
console.error('Error:', err);
}
);Use LuckyExcel.transformExcelToLucky() to convert an .xlsx file into a JSON format compatible with Luckysheet. This method accepts a File object (typically from an <input type="file"> element) and provides the resulting data via a callback.
Parameters:
excelFile: The File object to be processed.callback (optional): A function called upon success. It receives two arguments:files: An IuploadfileList object containing the parsed Excel data (the exportJson).fs: A string representing the raw Luckysheet file content.errorHandler (optional): A function called if an error occurs during processing.LuckyExcel.transformExcelToLucky(
excelFile,
(exportJson, luckysheetfile) => {
console.log('Parsed JSON:', exportJson);
console.log('Raw string:', luckysheetfile);
},
(err) => {
console.error('Transformation failed:', err);
}
);The Parse() method is the main execution method for the conversion process. It performs the following steps:
getWorkBookInfo() to extract file metadata.getSheetsFull() to parse all worksheets and their contents.LuckyFile object into a JSON string via toJsonString().The resulting JSON string follows the ILuckyFile interface, which is the standard format required by LuckySheet to render the spreadsheet.