umya-spreadsheet icon indicating copy to clipboard operation
umya-spreadsheet copied to clipboard

How to read pictures while reading all data

Open Yunnathai opened this issue 2 years ago • 9 comments

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.

Yunnathai avatar Aug 30 '23 08:08 Yunnathai

also hope support read buffer and filepath both them.

Yunnathai avatar Aug 30 '23 08:08 Yunnathai

@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();

MathNya avatar Aug 30 '23 09:08 MathNya

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.

Yunnathai avatar Aug 30 '23 09:08 Yunnathai

@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());
    }

MathNya avatar Aug 30 '23 15:08 MathNya

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

Yunnathai avatar Aug 31 '23 01:08 Yunnathai

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

MathNya avatar Sep 01 '23 09:09 MathNya

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

Yunnathai avatar Sep 01 '23 14:09 Yunnathai

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());

MathNya avatar Sep 15 '23 07:09 MathNya

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.

schungx avatar Dec 04 '23 06:12 schungx