mirror of
https://github.com/nushell/nushell
synced 2025-01-26 11:55:20 +00:00
Add no-infer flag to 'from xlsx' command
Signed-off-by: Alex Kattathra Johnson <alex.kattathra.johnson@gmail.com>
This commit is contained in:
parent
0d3f76ddef
commit
f48f56bb5c
1 changed files with 24 additions and 16 deletions
|
@ -17,6 +17,7 @@ impl Command for FromXlsx {
|
|||
Signature::build("from xlsx")
|
||||
.input_output_types(vec![(Type::Binary, Type::table())])
|
||||
.allow_variants_without_examples(true)
|
||||
.switch("no-infer", "no field type inferencing", None)
|
||||
.named(
|
||||
"sheets",
|
||||
SyntaxShape::List(Box::new(SyntaxShape::String)),
|
||||
|
@ -47,8 +48,10 @@ impl Command for FromXlsx {
|
|||
vec![]
|
||||
};
|
||||
|
||||
let no_infer = call.has_flag(engine_state, stack, "no-infer")?;
|
||||
|
||||
let metadata = input.metadata().map(|md| md.with_content_type(None));
|
||||
from_xlsx(input, head, sel_sheets).map(|pd| pd.set_metadata(metadata))
|
||||
from_xlsx(input, head, sel_sheets, no_infer).map(|pd| pd.set_metadata(metadata))
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -114,6 +117,7 @@ fn from_xlsx(
|
|||
input: PipelineData,
|
||||
head: Span,
|
||||
sel_sheets: Vec<String>,
|
||||
no_infer: bool,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let span = input.span();
|
||||
let bytes = collect_binary(input, head)?;
|
||||
|
@ -146,7 +150,10 @@ fn from_xlsx(
|
|||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, cell)| {
|
||||
let value = match cell {
|
||||
let value = if no_infer {
|
||||
Value::string(cell.as_string().unwrap_or_default(), head)
|
||||
} else {
|
||||
match cell {
|
||||
Data::Empty => Value::nothing(head),
|
||||
Data::String(s) => Value::string(s, head),
|
||||
Data::Float(f) => Value::float(*f, head),
|
||||
|
@ -161,6 +168,7 @@ fn from_xlsx(
|
|||
.map(|d| Value::date(d, head))
|
||||
.unwrap_or(Value::nothing(head)),
|
||||
_ => Value::nothing(head),
|
||||
}
|
||||
};
|
||||
|
||||
(format!("column{i}"), value)
|
||||
|
|
Loading…
Reference in a new issue