Compare commits

...

11 Commits

Author SHA1 Message Date
Nicolás Hatcher
9effd8e4b5 FIX: adds create user_model_from_bytes 2025-06-03 11:49:09 +02:00
Nicolás Hatcher
be66af8e16 FIX: Adds to_bytes in the user API 2025-06-03 11:04:12 +02:00
Nicolás Hatcher
8c5fe019b8 FIX: Add source files for alpine 2025-06-02 21:38:48 +02:00
Nicolás Hatcher
7bf36959ca FIX: Python add load/from bytes 2025-06-02 21:27:47 +02:00
Nicolás Hatcher
abaeb3284a FIX: users :) 2025-06-02 21:27:47 +02:00
Nicolás Hatcher
7635cbe1d1 FIX: bump PyO3 version 2025-06-02 21:27:47 +02:00
Nicolás Hatcher
293303b59c UPDATE: Add PyUserModel for the Hackaton 2025-06-02 21:27:47 +02:00
Nicolás Hatcher
2a809e0bd0 UPDATE: bump versions 2025-06-02 21:27:47 +02:00
Nicolás Hatcher
af49d7ad96 FIX: Download to png off by one errors 2025-06-02 21:11:18 +02:00
Nicolás Hatcher
3e015bf13a FIX: control+shitf selects area 2025-06-02 20:59:18 +02:00
Nicolás Hatcher
a5d8ee9ef0 FIX: Download all selected area
We were previously downloading only the bounds of the visible cells,
without taking into account the frozen rows/colums.

Fixes #343
2025-05-17 11:49:42 +02:00
19 changed files with 289 additions and 50 deletions

View File

@@ -117,7 +117,7 @@ jobs:
MATURIN_REPOSITORY_URL: "https://test.pypi.org/legacy/"
with:
command: upload
args: "--skip-existing **/*.whl"
args: "--skip-existing **/*.whl **/*.tar.gz"
working-directory: bindings/python
publish-pypi:
@@ -137,5 +137,5 @@ jobs:
MATURIN_REPOSITORY_URL: "https://upload.pypi.org/legacy/"
with:
command: upload
args: "--skip-existing **/*.whl"
args: "--skip-existing **/*.whl **/*.tar.gz"
working-directory: bindings/python

30
Cargo.lock generated
View File

@@ -721,11 +721,10 @@ dependencies = [
[[package]]
name = "pyo3"
version = "0.23.4"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57fe09249128b3173d092de9523eaa75136bf7ba85e0d69eca241c7939c933cc"
checksum = "f239d656363bcee73afef85277f1b281e8ac6212a1d42aa90e55b90ed43c47a4"
dependencies = [
"cfg-if",
"indoc",
"libc",
"memoffset",
@@ -739,9 +738,9 @@ dependencies = [
[[package]]
name = "pyo3-build-config"
version = "0.23.4"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd3927b5a78757a0d71aa9dff669f903b1eb64b54142a9bd9f757f8fde65fd7"
checksum = "755ea671a1c34044fa165247aaf6f419ca39caa6003aee791a0df2713d8f1b6d"
dependencies = [
"once_cell",
"target-lexicon",
@@ -749,9 +748,9 @@ dependencies = [
[[package]]
name = "pyo3-ffi"
version = "0.23.4"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dab6bb2102bd8f991e7749f130a70d05dd557613e39ed2deeee8e9ca0c4d548d"
checksum = "fc95a2e67091e44791d4ea300ff744be5293f394f1bafd9f78c080814d35956e"
dependencies = [
"libc",
"pyo3-build-config",
@@ -759,9 +758,9 @@ dependencies = [
[[package]]
name = "pyo3-macros"
version = "0.23.4"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91871864b353fd5ffcb3f91f2f703a22a9797c91b9ab497b1acac7b07ae509c7"
checksum = "a179641d1b93920829a62f15e87c0ed791b6c8db2271ba0fd7c2686090510214"
dependencies = [
"proc-macro2",
"pyo3-macros-backend",
@@ -771,9 +770,9 @@ dependencies = [
[[package]]
name = "pyo3-macros-backend"
version = "0.23.4"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43abc3b80bc20f3facd86cd3c60beed58c3e2aa26213f3cda368de39c60a27e4"
checksum = "9dff85ebcaab8c441b0e3f7ae40a6963ecea8a9f5e74f647e33fcf5ec9a1e89e"
dependencies = [
"heck",
"proc-macro2",
@@ -784,8 +783,9 @@ dependencies = [
[[package]]
name = "pyroncalc"
version = "0.5.0"
version = "0.5.6"
dependencies = [
"bitcode",
"ironcalc",
"pyo3",
"serde",
@@ -979,9 +979,9 @@ dependencies = [
[[package]]
name = "target-lexicon"
version = "0.12.16"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1"
checksum = "e502f78cdbb8ba4718f566c418c52bc729126ffd16baee5baa718cf25dd5a69a"
[[package]]
name = "thiserror"
@@ -1070,7 +1070,7 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm"
version = "0.5.0"
version = "0.5.3"
dependencies = [
"ironcalc_base",
"serde",

View File

@@ -405,6 +405,7 @@ impl Model {
},
tables: HashMap::new(),
views,
users: Vec::new(),
};
let parsed_formulas = Vec::new();
let worksheets = &workbook.worksheets;

View File

@@ -39,6 +39,14 @@ pub struct WorkbookView {
pub window_height: i64,
}
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct WebUser {
pub id: String,
pub sheet: u32,
pub row: i32,
pub column: i32,
}
/// An internal representation of an IronCalc Workbook
#[derive(Encode, Decode, Debug, PartialEq, Clone)]
pub struct Workbook {
@@ -51,6 +59,7 @@ pub struct Workbook {
pub metadata: Metadata,
pub tables: HashMap<String, Table>,
pub views: HashMap<u32, WorkbookView>,
pub users: Vec<WebUser>
}
/// A defined name. The `sheet_id` is the sheet index in case the name is local

View File

@@ -14,7 +14,7 @@ use crate::{
model::Model,
types::{
Alignment, BorderItem, CellType, Col, HorizontalAlignment, SheetProperties, SheetState,
Style, VerticalAlignment,
Style, VerticalAlignment, WebUser,
},
utils::is_valid_hex_color,
};
@@ -293,6 +293,11 @@ impl UserModel {
self.model.workbook.name = name.to_string();
}
/// Set users
pub fn set_users(&mut self, users: &[WebUser]) {
self.model.workbook.users = users.to_vec();
}
/// Undoes last change if any, places the change in the redo list and evaluates the model if needed
///
/// See also:

View File

@@ -1,6 +1,6 @@
[package]
name = "pyroncalc"
version = "0.5.0"
version = "0.5.6"
edition = "2021"
@@ -13,7 +13,8 @@ crate-type = ["cdylib"]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
xlsx = { package= "ironcalc", path = "../../xlsx", version = "0.5.0" }
pyo3 = { version = "0.23", features = ["extension-module"] }
pyo3 = { version = "0.25", features = ["extension-module"] }
bitcode = "0.6.3"
[features]

View File

@@ -1,6 +1,6 @@
[project]
name = "ironcalc"
version = "0.5.0"
version = "0.5.6"
description = "Create, edit and evaluate Excel spreadsheets"
requires-python = ">=3.10"
keywords = [

View File

@@ -2,8 +2,8 @@ use pyo3::exceptions::PyException;
use pyo3::{create_exception, prelude::*, wrap_pyfunction};
use types::{PySheetProperty, PyStyle};
use xlsx::base::types::Style;
use xlsx::base::Model;
use xlsx::base::types::{Style, Workbook};
use xlsx::base::{Model, UserModel};
use xlsx::export::{save_to_icalc, save_to_xlsx};
use xlsx::import;
@@ -14,6 +14,60 @@ use crate::types::PyCellType;
create_exception!(_ironcalc, WorkbookError, PyException);
#[pyclass]
pub struct PyUserModel {
/// The user model, which is a wrapper around the Model
pub model: UserModel,
}
#[pymethods]
impl PyUserModel {
/// Saves the user model to an xlsx file
pub fn save_to_xlsx(&self, file: &str) -> PyResult<()> {
let model = self.model.get_model();
save_to_xlsx(model, file).map_err(|e| WorkbookError::new_err(e.to_string()))
}
/// Saves the user model to file in the internal binary ic format
pub fn save_to_icalc(&self, file: &str) -> PyResult<()> {
let model = self.model.get_model();
save_to_icalc(model, file).map_err(|e| WorkbookError::new_err(e.to_string()))
}
pub fn apply_external_diffs(&mut self, external_diffs: &[u8]) -> PyResult<()> {
self.model
.apply_external_diffs(external_diffs)
.map_err(|e| WorkbookError::new_err(e.to_string()))
}
pub fn flush_send_queue(&mut self) -> Vec<u8> {
self.model.flush_send_queue()
}
pub fn set_user_input(
&mut self,
sheet: u32,
row: i32,
column: i32,
value: &str,
) -> PyResult<()> {
self.model
.set_user_input(sheet, row, column, value)
.map_err(|e| WorkbookError::new_err(e.to_string()))
}
pub fn get_formatted_cell_value(&self, sheet: u32, row: i32, column: i32) -> PyResult<String> {
self.model
.get_formatted_cell_value(sheet, row, column)
.map_err(|e| WorkbookError::new_err(e.to_string()))
}
pub fn to_bytes(&self) -> PyResult<Vec<u8>> {
let bytes = self.model.to_bytes();
Ok(bytes)
}
}
/// This is a model implementing the 'raw' API
#[pyclass]
pub struct PyModel {
@@ -32,6 +86,12 @@ impl PyModel {
save_to_icalc(&self.model, file).map_err(|e| WorkbookError::new_err(e.to_string()))
}
/// To bytes
pub fn to_bytes(&self) -> PyResult<Vec<u8>> {
let bytes = self.model.to_bytes();
Ok(bytes)
}
/// Evaluates the workbook
pub fn evaluate(&mut self) {
self.model.evaluate()
@@ -249,6 +309,15 @@ pub fn load_from_icalc(file_name: &str) -> PyResult<PyModel> {
Ok(PyModel { model })
}
#[pyfunction]
pub fn load_from_bytes(bytes: &[u8]) -> PyResult<PyModel> {
let workbook: Workbook =
bitcode::decode(bytes).map_err(|e| WorkbookError::new_err(e.to_string()))?;
let model =
Model::from_workbook(workbook).map_err(|e| WorkbookError::new_err(e.to_string()))?;
Ok(PyModel { model })
}
/// Creates an empty model
#[pyfunction]
pub fn create(name: &str, locale: &str, tz: &str) -> PyResult<PyModel> {
@@ -257,6 +326,43 @@ pub fn create(name: &str, locale: &str, tz: &str) -> PyResult<PyModel> {
Ok(PyModel { model })
}
#[pyfunction]
pub fn create_user_model(name: &str, locale: &str, tz: &str) -> PyResult<PyUserModel> {
let model = UserModel::new_empty(name, locale, tz)
.map_err(|e| WorkbookError::new_err(e.to_string()))?;
Ok(PyUserModel { model })
}
#[pyfunction]
pub fn create_user_model_from_xlsx(
file_path: &str,
locale: &str,
tz: &str,
) -> PyResult<PyUserModel> {
let model = import::load_from_xlsx(file_path, locale, tz)
.map_err(|e| WorkbookError::new_err(e.to_string()))?;
let model = UserModel::from_model(model);
Ok(PyUserModel { model })
}
#[pyfunction]
pub fn create_user_model_from_icalc(file_name: &str) -> PyResult<PyUserModel> {
let model =
import::load_from_icalc(file_name).map_err(|e| WorkbookError::new_err(e.to_string()))?;
let model = UserModel::from_model(model);
Ok(PyUserModel { model })
}
#[pyfunction]
pub fn create_user_model_from_bytes(bytes: &[u8]) -> PyResult<PyUserModel> {
let workbook: Workbook =
bitcode::decode(bytes).map_err(|e| WorkbookError::new_err(e.to_string()))?;
let model =
Model::from_workbook(workbook).map_err(|e| WorkbookError::new_err(e.to_string()))?;
let user_model = UserModel::from_model(model);
Ok(PyUserModel { model: user_model })
}
#[pyfunction]
#[allow(clippy::panic)]
pub fn test_panic() {
@@ -272,7 +378,14 @@ fn ironcalc(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(create, m)?)?;
m.add_function(wrap_pyfunction!(load_from_xlsx, m)?)?;
m.add_function(wrap_pyfunction!(load_from_icalc, m)?)?;
m.add_function(wrap_pyfunction!(load_from_bytes, m)?)?;
m.add_function(wrap_pyfunction!(test_panic, m)?)?;
// User model functions
m.add_function(wrap_pyfunction!(create_user_model, m)?)?;
m.add_function(wrap_pyfunction!(create_user_model_from_bytes, m)?)?;
m.add_function(wrap_pyfunction!(create_user_model_from_xlsx, m)?)?;
m.add_function(wrap_pyfunction!(create_user_model_from_icalc, m)?)?;
Ok(())
}

View File

@@ -6,3 +6,24 @@ def test_simple():
model.evaluate()
assert model.get_formatted_cell_value(0, 1, 1) == "3"
bytes = model.to_bytes()
model2 = ic.load_from_bytes(bytes)
assert model2.get_formatted_cell_value(0, 1, 1) == "3"
def test_simple_user():
model = ic.create_user_model("model", "en", "UTC")
model.set_user_input(0, 1, 1, "=1+2")
model.set_user_input(0, 1, 2, "=A1+3")
assert model.get_formatted_cell_value(0, 1, 1) == "3"
assert model.get_formatted_cell_value(0, 1, 2) == "6"
diffs = model.flush_send_queue()
model2 = ic.create_user_model("model", "en", "UTC")
model2.apply_external_diffs(diffs)
assert model2.get_formatted_cell_value(0, 1, 1) == "3"
assert model2.get_formatted_cell_value(0, 1, 2) == "6"

View File

@@ -1,6 +1,6 @@
[package]
name = "wasm"
version = "0.5.0"
version = "0.5.3"
authors = ["Nicolas Hatcher <nicolas@theuniverse.today>"]
description = "IronCalc Web bindings"
license = "MIT/Apache-2.0"

View File

@@ -201,6 +201,36 @@ defined_name_list_types = r"""
getDefinedNameList(): DefinedName[];
"""
set_users = r"""
/**
* @param {any} users
*/
setUsers(users: any): void;
"""
set_users_types = r"""
/**
* @param {WebUser[]} users
*/
setUsers(users: WebUser[]): void;
"""
get_users = r"""
/**
* @returns {any}
*/
getUsers(): any;
}
"""
get_users_types = r"""
/**
* @returns {WebUser[]}
*/
getUsers(): WebUser[];
}
"""
def fix_types(text):
text = text.replace(get_tokens_str, get_tokens_str_types)
text = text.replace(update_style_str, update_style_str_types)
@@ -215,6 +245,8 @@ def fix_types(text):
text = text.replace(clipboard, clipboard_types)
text = text.replace(paste_from_clipboard, paste_from_clipboard_types)
text = text.replace(defined_name_list, defined_name_list_types)
text = text.replace(set_users, set_users_types)
text = text.replace(get_users, get_users_types)
with open("types.ts") as f:
types_str = f.read()
header_types = "{}\n\n{}".format(header, types_str)

View File

@@ -6,7 +6,7 @@ use wasm_bindgen::{
use ironcalc_base::{
expressions::{lexer::util::get_tokens as tokenizer, types::Area, utils::number_to_column},
types::{CellType, Style},
types::{CellType, Style, WebUser},
BorderArea, ClipboardData, UserModel as BaseModel,
};
@@ -672,4 +672,18 @@ impl Model {
.delete_defined_name(name, scope)
.map_err(|e| to_js_error(e.to_string()))
}
#[wasm_bindgen(js_name = "setUsers")]
pub fn set_users(&mut self, users: JsValue) -> Result<(), JsError> {
let users: Vec<WebUser> =
serde_wasm_bindgen::from_value(users).map_err(|e| to_js_error(e.to_string()))?;
self.model.set_users(&users);
Ok(())
}
#[wasm_bindgen(js_name = "getUsers")]
pub fn get_users(&self) -> Result<JsValue, JsError> {
let users = self.model.get_model().workbook.users.clone();
serde_wasm_bindgen::to_value(&users).map_err(|e| to_js_error(e.to_string()))
}
}

View File

@@ -234,3 +234,10 @@ export interface DefinedName {
scope?: number;
formula: string;
}
export interface WebUser {
id: string;
sheet: number;
row: number;
column: number;
}

View File

@@ -1,16 +1,16 @@
{
"name": "@ironcalc/workbook",
"version": "0.3.2",
"version": "0.5.5",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@ironcalc/workbook",
"version": "0.3.2",
"version": "0.5.5",
"dependencies": {
"@emotion/react": "^11.14.0",
"@emotion/styled": "^11.14.0",
"@ironcalc/wasm": "file:../../bindings/wasm/pkg",
"@ironcalc/wasm": "0.5.3",
"@mui/material": "^6.4",
"@mui/system": "^6.4",
"i18next": "^23.11.1",
@@ -43,11 +43,6 @@
"react-dom": "^18.0.0 || ^19.0.0"
}
},
"../../bindings/wasm/pkg": {
"name": "@ironcalc/wasm",
"version": "0.5.0",
"license": "MIT/Apache-2.0"
},
"node_modules/@adobe/css-tools": {
"version": "4.4.2",
"resolved": "https://registry.npmjs.org/@adobe/css-tools/-/css-tools-4.4.2.tgz",
@@ -1060,8 +1055,10 @@
}
},
"node_modules/@ironcalc/wasm": {
"resolved": "../../bindings/wasm/pkg",
"link": true
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/@ironcalc/wasm/-/wasm-0.5.3.tgz",
"integrity": "sha512-ryQKR5ISkSQnnsxBYDnrAUN+GDiAQUx0MzkVpJr7VQXiymOSMZbHfpv5geum1eSJV4gw1ft69syuNolIhVZ4Hg==",
"license": "MIT/Apache-2.0"
},
"node_modules/@isaacs/cliui": {
"version": "8.0.2",

View File

@@ -1,6 +1,6 @@
{
"name": "@ironcalc/workbook",
"version": "0.3.2",
"version": "0.5.5",
"type": "module",
"main": "./dist/ironcalc.js",
"module": "./dist/ironcalc.js",
@@ -17,7 +17,7 @@
"dependencies": {
"@emotion/react": "^11.14.0",
"@emotion/styled": "^11.14.0",
"@ironcalc/wasm": "file:../../bindings/wasm/pkg",
"@ironcalc/wasm": "0.5.3",
"@mui/material": "^6.4",
"@mui/system": "^6.4",
"i18next": "^23.11.1",

View File

@@ -567,19 +567,15 @@ const Workbook = (props: { model: Model; workbookState: WorkbookState }) => {
const {
range: [rowStart, columnStart, rowEnd, columnEnd],
} = model.getSelectedView();
const { topLeftCell, bottomRightCell } =
worksheetCanvas.getVisibleCells();
const firstRow = Math.max(rowStart, topLeftCell.row);
const firstColumn = Math.max(columnStart, topLeftCell.column);
const lastRow = Math.min(rowEnd, bottomRightCell.row);
const lastColumn = Math.min(columnEnd, bottomRightCell.column);
// NB: cells outside of the displayed area are not rendered
// I think the only reasonable way to do this would be server side.
let [x, y] = worksheetCanvas.getCoordinatesByCell(
firstRow,
firstColumn,
rowStart,
columnStart,
);
const [x1, y1] = worksheetCanvas.getCoordinatesByCell(
lastRow + 1,
lastColumn + 1,
rowEnd + 1,
columnEnd + 1,
);
const width = (x1 - x) * devicePixelRatio;
const height = (y1 - y) * devicePixelRatio;

View File

@@ -236,10 +236,17 @@ const usePointer = (options: PointerSettings): PointerEvents => {
);
// we continue to select the new cell
}
if (event.shiftKey) {
// We are extending the selection
options.onAreaSelecting(cell);
options.onAreaSelected();
} else {
// We are selecting a single cell
options.onCellSelected(cell, event);
isSelecting.current = true;
worksheetWrapper.setPointerCapture(event.pointerId);
}
}
},
[options],
);

View File

@@ -19,6 +19,13 @@ import {
outlineColor,
} from "./constants";
export interface UserSelection {
userId: string;
color: string;
selection: [number, number, number, number, number]; // [sheet, rowStart, columnStart, rowEnd, columnEnd]
div: HTMLDivElement;
}
export interface CanvasSettings {
model: Model;
width: number;
@@ -1244,6 +1251,33 @@ export default class WorksheetCanvas {
editor.style.height = `${height - 1}px`;
}
private drawUsersSelection(): void {
const users = this.model.getUsers();
for (const handle of document.querySelectorAll(
".user-selection-ironcalc",
))
handle.remove();
users.forEach((user, index) => {
const { sheet, row, column } = user;
if (sheet !== this.model.getSelectedSheet()) {
return;
}
const [x, y] = this.getCoordinatesByCell(row, column);
const width = this.getColumnWidth(sheet, column);
const height = this.getRowHeight(sheet, row);
const div = document.createElement("div");
const color = getColor(index + 1);
div.className = "user-selection-ironcalc";
div.style.left = `${x}px`;
div.style.top = `${y}px`;
div.style.width = `${width}px`;
div.style.height = `${height}px`;
div.style.border = `1px solid ${color}`;
div.style.position = "absolute";
this.canvas.parentElement?.appendChild(div);
});
}
private drawCellOutline(): void {
const { cellOutline, areaOutline, cellOutlineHandle } = this;
if (this.workbookState.getEditingCell()) {
@@ -1595,6 +1629,7 @@ export default class WorksheetCanvas {
context.stroke();
this.drawCellOutline();
this.drawUsersSelection();
this.drawCellEditor();
this.drawExtendToArea();
this.drawActiveRanges(topLeftCell, bottomRightCell);

View File

@@ -110,6 +110,7 @@ fn load_xlsx_from_reader<R: Read + std::io::Seek>(
metadata,
tables,
views,
users: Vec::new(),
})
}