Skip to content

Commit 24162a4

Browse files
author
DESKTOP-U434MT0\hiro
committed
no message
1 parent ffe4863 commit 24162a4

File tree

7 files changed

+90
-19
lines changed

7 files changed

+90
-19
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "umya-spreadsheet"
3-
version = "1.0.1"
3+
version = "1.0.2"
44
authors = ["MathNya <umya.net.info@gmail.com>"]
55
repository = "https://github.com/MathNya/umya-spreadsheet"
66
keywords = ["excel", "spreadsheet", "xlsx", "reader", "writer"]

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@
1212
Please use [Gitter](https://gitter.im/MathNya/umya-spreadsheet) for brief chats.
1313

1414
## New feature
15+
### ver 1.0.2
16+
#### **Image data is now easier to acquire.**
17+
```rust
18+
// get image data.
19+
let img = book.get_sheet_by_name("Sheet1").unwrap().get_image("M17").unwrap();
20+
// or
21+
let img_list = book.get_sheet_by_name("Sheet1").unwrap().get_image_collection();
22+
23+
// get Coordinate.
24+
assert_eq!(img.get_coordinate(), "M17");
25+
assert_eq!(img.get_col(), &12);
26+
assert_eq!(img.get_row(), &16);
27+
28+
// get file name.
29+
assert_eq!(img.get_image_name(), "image1.png");
30+
31+
// get binary data.
32+
dbg!(img.get_image_data());
33+
34+
// get base64 data.
35+
dbg!(img.get_image_data_base64());
36+
```
1537
### ver 1.0.0
1638
#### **remove deprecated functions**
1739
#### **new function move_range**

src/structs/cell.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ impl Cell {
118118
self
119119
}
120120

121+
pub(crate) fn set_value_str<S: Into<String>>(&mut self, value: S) -> &mut Self {
122+
self.cell_value.set_value_str(value);
123+
self
124+
}
125+
121126
pub fn set_value_bool(&mut self, value: bool) -> &mut Self {
122127
self.cell_value.set_value_bool(value);
123128
self
@@ -322,7 +327,7 @@ impl Cell {
322327
}
323328
b"v" => {
324329
if type_value == "str" {
325-
self.set_value_crate(string_value.clone());
330+
self.set_value_str(string_value.clone());
326331
}
327332
if type_value == "s" {
328333
let index = string_value.parse::<usize>().unwrap();
@@ -375,7 +380,11 @@ impl Cell {
375380
let mut attributes: Vec<(&str, &str)> = Vec::new();
376381
let coordinate = self.coordinate.get_coordinate();
377382
attributes.push(("r", &coordinate));
378-
if self.get_data_type_crate() == "s" || self.get_data_type_crate() == "b" {
383+
if self.get_data_type_crate() == "s"
384+
|| self.get_data_type_crate() == "b"
385+
|| self.get_data_type_crate() == "str"
386+
|| self.get_data_type_crate() == "e"
387+
{
379388
attributes.push(("t", self.get_data_type_crate()));
380389
}
381390
let xf_index_str: String;

src/structs/cell_raw_value.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::fmt;
55
#[derive(Clone, Debug, PartialEq, PartialOrd)]
66
pub enum CellRawValue {
77
String(String),
8+
Str(String),
89
RichText(RichText),
910
Lazy(String),
1011
Numeric(f64),
@@ -17,6 +18,7 @@ impl fmt::Display for CellRawValue {
1718
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1819
match self {
1920
Self::String(v) => write!(f, "{}", v),
21+
Self::Str(v) => write!(f, "{}", v),
2022
Self::RichText(v) => write!(f, "{}", v.get_text()),
2123
Self::Numeric(v) => write!(f, "{}", &v),
2224
Self::Bool(v) => write!(f, "{}", if v == &true { "TRUE" } else { "FALSE" }),
@@ -33,16 +35,19 @@ impl CellRawValue {
3335
pub fn get_data_type(&self) -> &str {
3436
match self {
3537
Self::String(_) => "s",
38+
Self::Str(_) => "str",
3639
Self::RichText(_) => "s",
3740
Self::Numeric(_) => "n",
3841
Self::Bool(_) => "b",
42+
Self::Error => "e",
3943
_ => "",
4044
}
4145
}
4246

4347
pub(crate) fn get_text(&self) -> Option<Text> {
4448
match self {
4549
Self::String(_) | // _
50+
Self::Str(_) | // _
4651
Self::Numeric(_) | // _
4752
Self::Bool(_) => {
4853
let mut text = Text::default();

src/structs/cell_value.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ impl CellValue {
2121
}
2222

2323
pub(crate) fn get_data_type_crate(&self) -> &str {
24-
match &self.formula {
25-
Some(_) => {
26-
return "f";
27-
}
28-
None => {}
29-
}
3024
self.raw_value.get_data_type()
3125
}
3226

@@ -98,6 +92,11 @@ impl CellValue {
9892
self
9993
}
10094

95+
pub(crate) fn set_value_str<S: Into<String>>(&mut self, value: S) -> &mut Self {
96+
self.raw_value = CellRawValue::Str(value.into());
97+
self
98+
}
99+
101100
pub fn set_value_bool(&mut self, value: bool) -> &mut Self {
102101
self.raw_value = CellRawValue::Bool(value);
103102
self.remove_formula();

src/structs/image.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use base64::{engine::general_purpose::STANDARD, Engine as _};
12
use image::GenericImageView;
23
use quick_xml::Writer;
34
use std::fs;
@@ -152,15 +153,27 @@ impl Image {
152153
fs::write(path, self.get_media_object().get_image_data()).unwrap();
153154
}
154155

156+
pub fn get_image_name(&self) -> &str {
157+
self.get_media_object().get_image_name()
158+
}
159+
160+
pub fn get_image_data(&self) -> &Vec<u8> {
161+
self.get_media_object().get_image_data()
162+
}
163+
164+
pub fn get_image_data_base64(&self) -> String {
165+
STANDARD.encode(self.get_image_data())
166+
}
167+
155168
pub fn get_coordinate(&self) -> String {
156169
self.get_from_marker_type().get_coordinate()
157170
}
158171

159-
pub(crate) fn get_col(&self) -> &u32 {
172+
pub fn get_col(&self) -> &u32 {
160173
self.get_from_marker_type().get_col()
161174
}
162175

163-
pub(crate) fn get_row(&self) -> &u32 {
176+
pub fn get_row(&self) -> &u32 {
164177
self.get_from_marker_type().get_row()
165178
}
166179

tests/integration_test.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,14 +1181,37 @@ fn issue_72() {
11811181
}
11821182

11831183
#[test]
1184-
fn issue_128() {
1185-
let xlsx_path = std::path::Path::new("./tests/test_files/wb_with_shared_strings.xlsx");
1184+
fn issue_129() {
1185+
let path = std::path::Path::new("./tests/test_files/aaa.xlsx");
1186+
let book = umya_spreadsheet::reader::xlsx::read(path).unwrap();
1187+
let img = book
1188+
.get_sheet_by_name("Sheet1")
1189+
.unwrap()
1190+
.get_image("M17")
1191+
.unwrap();
1192+
//dbg!(img.get_one_cell_anchor().is_some());
1193+
//dbg!(img.get_two_cell_anchor().is_some());
1194+
assert_eq!(img.get_image_name(), "image1.png");
1195+
assert_eq!(img.get_coordinate(), "M17");
1196+
assert_eq!(img.get_col(), &12);
1197+
assert_eq!(img.get_row(), &16);
1198+
1199+
let path = std::path::Path::new("./tests/result_files/issue_129.xlsx");
1200+
let _ = umya_spreadsheet::writer::xlsx::write(&book, path);
1201+
}
11861202

1187-
let book = umya_spreadsheet::reader::xlsx::read(xlsx_path).unwrap();
1203+
#[test]
1204+
fn wb_with_shared_strings() {
1205+
let path = std::path::Path::new("./tests/test_files/wb_with_shared_strings.xlsx");
1206+
let book = umya_spreadsheet::reader::xlsx::read(path).unwrap();
11881207
let sheet = book.get_sheet_by_name("Sheet To Read From").unwrap();
1189-
assert_eq!(sheet.get_formatted_value("A2"), "11");
1190-
assert_eq!(sheet.get_formatted_value("A3"), "22");
1191-
assert_eq!(sheet.get_formatted_value("A4"), "ABCdef");
1192-
assert_eq!(sheet.get_formatted_value("A5"), "ABCdef");
1193-
assert_eq!(sheet.get_formatted_value("A6"), "ABCdef");
1208+
1209+
assert_eq!(sheet.get_cell("A2").unwrap().get_value(), "11");
1210+
assert_eq!(sheet.get_cell("A3").unwrap().get_value(), "22");
1211+
assert_eq!(sheet.get_cell("A4").unwrap().get_value(), "ABCdef");
1212+
assert_eq!(sheet.get_cell("A5").unwrap().get_value(), "ABCdef");
1213+
assert_eq!(sheet.get_cell("A6").unwrap().get_value(), "ABCdef");
1214+
1215+
let path = std::path::Path::new("./tests/result_files/wb_with_shared_strings.xlsx");
1216+
let _ = umya_spreadsheet::writer::xlsx::write(&book, path);
11941217
}

0 commit comments

Comments
 (0)