From 637e7809f0644a7c62ab6fd23eb8a0e4a2a8b6ba Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Fri, 23 Oct 2026 18:39:40 +0200 Subject: [PATCH] Make HYPERLINK formula cells behave as clickable links Fixes #2230 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/calc/src/lib.rs | 2 +- crates/engine/src/cmd/insert/links.rs | 4 +-- crates/engine/src/cmd/inspect.rs | 2 +- crates/engine/src/links.rs | 39 ++++++++++++++++++++++++++- crates/engine/src/tests.rs | 15 +++++++++++ crates/ui-egui/src/grid/interact.rs | 4 +-- crates/ui-egui/src/grid/show.rs | 2 +- 7 files changed, 60 insertions(+), 8 deletions(-) diff --git a/crates/calc/src/lib.rs b/crates/calc/src/lib.rs index 794a3f97f..92aa9526c 100644 --- a/crates/calc/src/lib.rs +++ b/crates/calc/src/lib.rs @@ -12,7 +12,7 @@ mod trimrange; pub use eval::{Area, SPECIAL_FUNCTIONS, is_known_function}; pub use precision::DisplayedPrecision; -pub use recalc::{Bounded, Calc, Key, evaluate, evaluate_bounded, now_serial}; +pub use recalc::{Bounded, Calc, Key, evaluate, evaluate_bounded, evaluate_expr, now_serial}; #[cfg(test)] mod tests; diff --git a/crates/engine/src/cmd/insert/links.rs b/crates/engine/src/cmd/insert/links.rs index c90fc946a..b0b489295 100644 --- a/crates/engine/src/cmd/insert/links.rs +++ b/crates/engine/src/cmd/insert/links.rs @@ -56,10 +56,10 @@ pub(super) fn open_link(s: &mut Session, p: &Json) -> Result { None => { let sheet = target_sheet(s, p)?; let at = cell_param_on(s, p, "cell", sheet)?.unwrap_or(s.doc()?.selection.active); - let Some(h) = s.doc()?.wb.sheet(sheet).and_then(|sh| sh.hyperlinks.get(&at)) else { + let Some(h) = crate::links::cell_link(&s.doc()?.wb, sheet, at) else { return Err(EngineError::Other("There's no hyperlink in this cell.".into())); }; - h.target.clone() + h.target } }; match crate::links::classify(&target) { diff --git a/crates/engine/src/cmd/inspect.rs b/crates/engine/src/cmd/inspect.rs index e91a269fb..d6646743d 100644 --- a/crates/engine/src/cmd/inspect.rs +++ b/crates/engine/src/cmd/inspect.rs @@ -136,7 +136,7 @@ fn cell_get(s: &mut Session, p: &Json) -> Result { "style": style, "merge": sh.merge_at(c).map(|m| m.a1()), "comment": sh.comments.get(&c), - "hyperlink": sh.hyperlinks.get(&c), + "hyperlink": crate::links::cell_link(&d.wb, sheet, c), "python": cell.and_then(|x| x.py.as_deref()).map(|py| json!({ "code": py.code, "output": if py.object { "object" } else { "value" }, diff --git a/crates/engine/src/links.rs b/crates/engine/src/links.rs index 4c2318f07..9497de4ef 100644 --- a/crates/engine/src/links.rs +++ b/crates/engine/src/links.rs @@ -9,7 +9,9 @@ //! external addresses; only the host, after showing the address and getting OK from the user, //! opens one through [`crate::Session::open_confirmed_link`], which no command reaches. -use gridcraft_core::RangeRef; +use gridcraft_core::{CellRef, RangeRef}; +use gridcraft_formula::Expr; +use gridcraft_model::{Hyperlink, Workbook}; use serde_json::{Value as Json, json}; use crate::{EngineError, Result, Session, UiRequest}; @@ -86,6 +88,41 @@ pub fn classify(target: &str) -> LinkKind { } } +/// The link of a cell: the one stored with it, or, for a cell whose formula is `HYPERLINK(target, …)`, +/// the target that formula currently evaluates to. A scheme-less web address (`google.com`) is +/// opened over `https`, as Excel does. +pub fn cell_link(wb: &Workbook, sheet: usize, at: CellRef) -> Option { + let sh = wb.sheet(sheet)?; + if let Some(h) = sh.hyperlinks.get(&at) { + return Some(h.clone()); + } + let expr = sh.cell(at)?.formula.as_ref()?.expr()?; + let Expr::Call(name, args) = &expr else { return None }; + if name != "HYPERLINK" { + return None; + } + let value = gridcraft_calc::evaluate_expr(wb, sheet, at, args.first()?); + let target = value.display().trim().to_string(); + if target.is_empty() || matches!(value, gridcraft_core::Value::Error(_)) { + return None; + } + let target = if looks_like_web_address(&target) && wb.name(&target, sheet).is_none() { format!("https://{target}") } else { target }; + let external = !matches!(classify(&target), LinkKind::Internal(_)); + Some(Hyperlink { target, tooltip: None, external }) +} + +fn looks_like_web_address(t: &str) -> bool { + if t.contains(|c: char| c.is_whitespace() || matches!(c, '\\' | '!' | '#' | ':')) { + return false; + } + let host = t.split('/').next().unwrap_or(t); + let Some((_, tld)) = host.rsplit_once('.') else { return false }; + tld.len() >= 2 + && tld.chars().all(|c| c.is_ascii_alphabetic()) + && !["xlsx", "xlsm", "xlsb", "xls", "csv", "txt", "pdf", "doc", "docx"].contains(&tld.to_ascii_lowercase().as_str()) + && RangeRef::parse(t).is_none() +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 785e68f6b..7b25f7af6 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7657,3 +7657,18 @@ fn history_charges_the_storage_of_empty_comment_replies() { // 500 replies of no text are still 500 elements. assert!(crate::sheet_text_bytes(&sheet) >= 500 * std::mem::size_of::<(String, String)>()); } + +#[test] +fn hyperlink_function_makes_a_cell_link() { + let mut s = s(); + s.link_policy = crate::links::LinkPolicy::Allow; + s.execute("cell.set", json!({"cell": "A1", "input": "https://example.com/x"})).unwrap(); + s.execute("cell.set", json!({"cell": "B2", "input": "=HYPERLINK(A1,\"Go\")"})).unwrap(); + s.execute("cell.set", json!({"cell": "C3", "input": "=HYPERLINK(\"google.com\")"})).unwrap(); + s.take_ui_requests(); + s.execute("link.open", json!({"cell": "B2"})).unwrap(); + assert_eq!(s.take_ui_requests(), vec![crate::UiRequest::OpenUrl("https://example.com/x".into())]); + s.execute("link.open", json!({"cell": "C3"})).unwrap(); + assert_eq!(s.take_ui_requests(), vec![crate::UiRequest::OpenUrl("https://google.com".into())]); + assert!(s.execute("link.open", json!({"cell": "A1"})).is_err()); +} diff --git a/crates/ui-egui/src/grid/interact.rs b/crates/ui-egui/src/grid/interact.rs index f0ab7bc8e..e5fe41f53 100644 --- a/crates/ui-egui/src/grid/interact.rs +++ b/crates/ui-egui/src/grid/interact.rs @@ -528,7 +528,7 @@ fn handle_press(app: &mut SheetApp, input: &InteractionInput<'_>) { } } else if mods.command { // Ctrl+click (without Alt) on a hyperlink's text follows it instead of adding the cell to the selection. - if let Some(h) = sh.hyperlinks.get(&c).cloned() + if let Some(h) = gridcraft_engine::links::cell_link(wb, wb.active_sheet(), c) && acts && !mods.alt && p.x < geo.cell_rect(sh, c).left() + geo.cell_rect(sh, c).width() * 0.8 @@ -545,7 +545,7 @@ fn handle_press(app: &mut SheetApp, input: &InteractionInput<'_>) { let _ = app.run("cell.toggleCheckbox", json!({"cell": c.a1()})); } // A click on a hyperlink's text follows it (like Excel); elsewhere in the cell selects. - if let Some(h) = sh.hyperlinks.get(&c).cloned() + if let Some(h) = gridcraft_engine::links::cell_link(wb, wb.active_sheet(), c) && acts && !mods.any() { diff --git a/crates/ui-egui/src/grid/show.rs b/crates/ui-egui/src/grid/show.rs index 713c2d51c..13839f1b8 100644 --- a/crates/ui-egui/src/grid/show.rs +++ b/crates/ui-egui/src/grid/show.rs @@ -770,7 +770,7 @@ pub(crate) fn paint_overlays(input: OverlayInput<'_>) { ); } // Hyperlink tooltip. - if let Some(h) = hover.and_then(|c| sh.hyperlinks.get(&c)) + if let Some(h) = hover.and_then(|c| gridcraft_engine::links::cell_link(wb, si, c)) && app.editor.is_none() { let tip = h.tooltip.clone().unwrap_or_else(|| format!("{}\nClick once to follow. Click and hold to select this cell.", h.target));