ronniec95 / xladd-derive

Macros to help write Excel User defined functions easily in Rust
MIT License
18 stars 1 forks source link

Function taking variable type in #5

Closed vron closed 2 years ago

vron commented 3 years ago

Hi Again,

Sorry to keep bothering you - am I correct in uderstanding that the derive function (which provide nice Result<> wrapping!) does not support any input type that holds a variable typed input array / matrix?

I.e. if i in Excel do =my_test(A1:B8)

In the underlying crate I could then define a function that would give me a single LPXLOPER12 / Variant in where i Could iterate over each matrix element and do different things depending on if they contain strings or numbers e.g. (but then without the error wrapping)

I naively tried to derive such a function using:

#[xl_func(category = "test")]
fn ra_json_object(val: Variant) -> Result<Variant, Box<dyn std::error::Error>> {

Without any luck

ronniec95 commented 3 years ago

Hi,

Right, that's by design, as Rust is generally strongly typed. However there is a work around which is sort of ok...

Take in a String or Array2 (for a range) and try a parse::<i32>()

I do have the reverse though:

fn example(name: Array2<String>) -> Result<Array2<Variant>, Box<dyn std::error::Error>> {
 // ...
 // some code before
 Ok(Array2::from_shape_vec(
        [results.len() / max_width, max_width],
        results,
    )?)
}
ronniec95 commented 3 years ago

Just thinking about it, if you want to take a variant type then you can always drop down to xladd's raw interface.

That's where I originally started but with null values and conversions, my code got really messy (lots of it/else statements) which is why I wrote the xladd-derive macro.

On my todo list is Optional arguments represented by Option so if there is a need for that, let me know

vron commented 3 years ago

Thanks for clarification,

Yes - i can drop down - but I'd have to write the wrapper to handle the results (not much work admitedly) - I just though that a Variant as input would also be reasonable (since it's safe . the non strongly typed part is protected) - but thats fine - your crate your call :-)

As for options, always nice but would not help me now - i need json input to send so was planning to effectively write a add-in function that takes two columns of "field names" in col1 and "values" in col2 that i then merge to a json string to return / send (the values in the json struct needs to be serialized diffrently depending on if they are numbers or strings or bools etc.)

However - your string workarkound should work very well - a bit unecissary to go float - string - float but not a problem for this case.

thanks

ronniec95 commented 3 years ago

Add it as a feature request - I'm always open to adding things.

vron commented 3 years ago

Another argumenr for this (or alternatve) in the high level api is to be able to acess the shape of the input - eg implementing matrix multiplication of two ranges.

ronniec95 commented 3 years ago

Yeah, so I've done all the matrix ops in xl8ml which might save you a lot of time. After some design thought it was easier to use ndarrays. If it's allowable, what is the project you are working on?

vron commented 3 years ago

Fair point on ndarray! Its an internal project im affraid so im nit allowed to share details :-( (But no not doing matrix operations it was just a easy example)

ronniec95 commented 2 years ago

Discussion complete