docs.sheetjs.com/docz/static/cli/sheet2csv.rs

61 lines
2.5 KiB
Rust
Raw Permalink Normal View History

2023-05-23 06:28:14 +00:00
/* run code, get result as a Rust String */
fn eval_code(scope: &mut v8::HandleScope, code: &str) -> std::string::String {
2024-05-29 05:10:39 +00:00
let source = v8::String::new(scope, code).unwrap();
2023-05-23 06:28:14 +00:00
let script = v8::Script::compile(scope, source, None).unwrap();
let result = script.run(scope).unwrap();
return result.to_string(scope).unwrap().to_rust_string_lossy(scope);
}
2024-05-29 05:10:39 +00:00
/* assuming JS code returns an ArrayBuffer, copy result to a Vec<u8> */
fn eval_code_ab(scope: &mut v8::HandleScope, code: &str) -> Vec<u8> {
let source = v8::String::new(scope, code).unwrap();
let script = v8::Script::compile(scope, source, None).unwrap();
let result: v8::Local<v8::ArrayBuffer> = script.run(scope).unwrap().try_into().unwrap();
unsafe { return std::slice::from_raw_parts_mut(result.data().unwrap().cast::<u8>().as_ptr(), result.byte_length()).to_vec(); }
}
2023-05-23 06:28:14 +00:00
fn main() {
/* parse arguments */
let mut iter = std::env::args();
let path: String = iter.nth(1).expect("must specify a file name");
2024-05-29 05:10:39 +00:00
let sheetname: String = match iter.next() {
2023-05-23 06:28:14 +00:00
Some(v) => format!("'{}'", v),
None => "wb.SheetNames[0]".to_string()
};
/* initialize */
let platform = v8::new_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();
/* read snapshot */
let startup_data: Vec<u8> = include_bytes!("./snapshot.bin").to_vec();
let params = v8::Isolate::create_params().snapshot_blob(startup_data);
let isolate = &mut v8::Isolate::new(params);
let handle_scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(handle_scope);
let context_scope = &mut v8::ContextScope::new(handle_scope, context);
/* read file */
2024-05-29 05:10:39 +00:00
{
let data: Vec<u8> = std::fs::read(path.clone()).unwrap();
let back: v8::UniqueRef<v8::BackingStore> = v8::ArrayBuffer::new_backing_store_from_vec(data);
let shared = back.make_shared();
let ab: v8::Local<v8::ArrayBuffer> = v8::ArrayBuffer::with_backing_store(context_scope, &shared);
let key = v8::String::new(context_scope, "buf").unwrap();
context.global(context_scope).set(context_scope, key.into(), ab.into());
}
2023-05-23 06:28:14 +00:00
/* print CSV of specified sheet */
2024-05-29 05:10:39 +00:00
{
let result = eval_code(context_scope, &format!("var wb = XLSX.read(buf, {{dense: true}}); XLSX.utils.sheet_to_csv(wb.Sheets[{}])", sheetname));
println!("{}", result);
}
/* write sheetjsw.numbers */
{
let result = eval_code_ab(context_scope, "XLSX.write(wb, {type:'array', bookType:'numbers', numbers: XLSX_ZAHL_PAYLOAD})");
std::fs::write("sheetjsw.numbers", result).unwrap();
}
2023-05-23 06:28:14 +00:00
}