MathNya / umya-spreadsheet

A pure rust library for reading and writing spreadsheet files
MIT License
239 stars 41 forks source link

How to read pictures while reading all data #129

Open jamninetyfive opened 10 months ago

jamninetyfive commented 10 months ago

when i read data i get empty string

    for row in 1..max_col_and_row.0+1 {
        let mut row_vec: Vec<String> = Vec::new();
        for col in 1..max_col_and_row.1+1 {
            let cell = book
                .get_sheet_by_name(sheetname)
                .expect(&format!("Unable to open sheet: {}", sheetname))
                .get_value((row, col));
            row_vec.push(cell);
        }
        container.push(row_vec);
    }

or can I get cellType like stream, string, number....

What I want to get all data including image data and convert it to base64 and return it for use.

jamninetyfive commented 10 months ago

also hope support read buffer and filepath both them.

MathNya commented 10 months ago

@jamninetyfive Thank you for contacting us. The image data is managed in a different location than the cell. You can get it with the following code. (I did not expect this kind of usage, so it is not a smart code.)

    let img = book.get_sheet_by_name("Sheet1").unwrap().get_image("M17").unwrap();
    match img.get_two_cell_anchor() {
        Some(anchor) => match anchor.get_picture() {
            Some(v) => {
                dbg!(v.get_blip_fill().get_blip().get_image().get_image_name());
                dbg!(v.get_blip_fill().get_blip().get_image().get_image_data());
            }
            None => {}
        },
        None => {}
    }
    match img.get_one_cell_anchor() {
        Some(anchor) => match anchor.get_picture() {
            Some(v) => {
                dbg!(v.get_blip_fill().get_blip().get_image().get_image_name());
                dbg!(v.get_blip_fill().get_blip().get_image().get_image_data());
            }
            None => {}
        },
        None => {}
    }

Image objects in a sheet can also be retrieved in the following ways

let img_list = book.get_sheet_by_name("Sheet1").unwrap().get_image_collection();
jamninetyfive commented 10 months ago

Firstly, thank you for your great work in this library.

Using the first method, I still have a question that I am traversing the query col and row, and I don't know when to get images.

Secondly, in the second method, I can indeed obtain all the images, but I cannot obtain their original positions, such as their rows and columns.

Thank you again for responding to my question. Or do you have any suggestions for me.

This is my need.

  1. I will turn it into a wasm module.
  2. Upload Excel on the web to obtain the stream.
  3. Parse it to the wasm module to obtain all data, including images.
  4. I need them to ensure that each row of data is on an object.
MathNya commented 10 months ago

@jamninetyfive You can now retrieve the position.

    let img_list = book.get_sheet_by_name("Sheet1").unwrap().get_image_collection();
    for img in img_list {
        dbg!(img.get_coordinate());
    }
jamninetyfive commented 10 months ago

Thank you for your patient reply. Can read buffer/blob be supported?

MathNya commented 10 months ago

@jamninetyfive Is buffer/blob different from get_image_data()? If possible, a PR would be appreciated.

jamninetyfive commented 10 months ago

Thank you for your reply What I mean is to pass the Excel buffer for reading, not the path I will try PR, if I complete it, so far I am not good at rust, I am only good at JS

MathNya commented 9 months ago

Fixed to make it easier to retrieve Image.

// get image data.
let img = book.get_sheet_by_name("Sheet1").unwrap().get_image("M17").unwrap();
// or
let img_list = book.get_sheet_by_name("Sheet1").unwrap().get_image_collection();

// get Coordinate.
assert_eq!(img.get_coordinate(), "M17");
assert_eq!(img.get_col(), &12);
assert_eq!(img.get_row(), &16);

// get file name.
assert_eq!(img.get_image_name(), "image1.png");

// get binary data.
dbg!(img.get_image_data());

// get base64 data.
dbg!(img.get_image_data_base64());
schungx commented 7 months ago

What if there are multiple images in a particular cell? What does get_image return then?

Also, any way to get the width/height and/or the lower-right corner of the image? That's because an image may be larger than a single cell, so get_col and get_row only return the upper-left corner...

EDIT: Sorry, I now realize there are more API's that get multiple images and single-cell/two-cell ones. I'll experiment with them first. Sorry for the disruption.