UPDATE: Adds load_from_xlsx_bytes (#86)
This is usefull if we are transfering bytes, for instance over the internet
This commit is contained in:
committed by
GitHub
parent
b9bf485379
commit
40aa8bebaf
@@ -10,7 +10,7 @@ mod worksheets;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs,
|
||||
io::{BufReader, Read},
|
||||
io::{BufReader, Cursor, Read},
|
||||
};
|
||||
|
||||
use roxmltree::Node;
|
||||
@@ -113,7 +113,7 @@ fn load_xlsx_from_reader<R: Read + std::io::Seek>(
|
||||
})
|
||||
}
|
||||
|
||||
/// Imports a file from disk into an internal representation
|
||||
// Imports a file from disk into an internal representation
|
||||
fn load_from_excel(file_name: &str, locale: &str, tz: &str) -> Result<Workbook, XlsxError> {
|
||||
let file_path = std::path::Path::new(file_name);
|
||||
let file = fs::File::open(file_path)?;
|
||||
@@ -126,11 +126,26 @@ fn load_from_excel(file_name: &str, locale: &str, tz: &str) -> Result<Workbook,
|
||||
load_xlsx_from_reader(name, reader, locale, tz)
|
||||
}
|
||||
|
||||
/// Loads a [Workbook] from the bytes of an xlsx file.
|
||||
/// This is useful, for instance, when bytes are transferred over the network
|
||||
pub fn load_from_xlsx_bytes(
|
||||
bytes: &[u8],
|
||||
name: &str,
|
||||
locale: &str,
|
||||
tz: &str,
|
||||
) -> Result<Workbook, XlsxError> {
|
||||
let cursor = Cursor::new(bytes);
|
||||
let reader = BufReader::new(cursor);
|
||||
load_xlsx_from_reader(name.to_string(), reader, locale, tz)
|
||||
}
|
||||
|
||||
/// Loads a [Model] from an xlsx file
|
||||
pub fn load_from_xlsx(file_name: &str, locale: &str, tz: &str) -> Result<Model, XlsxError> {
|
||||
let workbook = load_from_excel(file_name, locale, tz)?;
|
||||
Model::from_workbook(workbook).map_err(XlsxError::Workbook)
|
||||
}
|
||||
|
||||
/// Loads a [Model] from an `ic` file (a file in the IronCalc internal representation)
|
||||
pub fn load_from_icalc(file_name: &str) -> Result<Model, XlsxError> {
|
||||
let contents = fs::read(file_name)
|
||||
.map_err(|e| XlsxError::IO(format!("Could not extract workbook name: {}", e)))?;
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use std::io::Read;
|
||||
use std::{env, fs, io};
|
||||
use uuid::Uuid;
|
||||
|
||||
use ironcalc::compare::{test_file, test_load_and_saving};
|
||||
use ironcalc::export::save_to_xlsx;
|
||||
use ironcalc::import::{load_from_icalc, load_from_xlsx};
|
||||
use ironcalc::import::{load_from_icalc, load_from_xlsx, load_from_xlsx_bytes};
|
||||
use ironcalc_base::types::{HorizontalAlignment, VerticalAlignment};
|
||||
use ironcalc_base::Model;
|
||||
|
||||
@@ -48,6 +49,16 @@ fn test_example() {
|
||||
assert_eq!(workbook, model2.workbook, "{:?}", s);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_from_xlsx_bytes() {
|
||||
let file_path = std::path::Path::new("tests/example.xlsx");
|
||||
let mut file = fs::File::open(file_path).unwrap();
|
||||
let mut bytes = Vec::new();
|
||||
file.read_to_end(&mut bytes).unwrap();
|
||||
let workbook = load_from_xlsx_bytes(&bytes, "home", "en", "UTC").unwrap();
|
||||
assert_eq!(workbook.views[&0].sheet, 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_grid() {
|
||||
let model = load_from_xlsx("tests/NoGrid.xlsx", "en", "UTC").unwrap();
|
||||
|
||||
Reference in New Issue
Block a user