Conditional formatting: name the calling command in style errors and title the edit path (#1822)

This commit is contained in:
alosslessdev
2026-11-03 23:07:09 -05:00
parent a8e5fdcb75
commit 68d54cc1b6
20 changed files with 41 additions and 5 deletions
+17 -3
View File
@@ -1502,9 +1502,9 @@ fn is_string_literal(v: &str) -> bool {
/// written as `format_range` takes it (`"#RRGGBB"`, `{theme, tint}`, `"auto"`) and `fill` may be a
/// colour alone (a solid fill) or `"none"` (no fill). An unknown key is an error rather than
/// dropped: dropped, it left a rule with an empty format that matches and shows nothing.
fn cf_style_json(st: &Json) -> Result<Json> {
fn cf_style_json(st: &Json, cmd: &str) -> Result<Json> {
const KEYS: [&str; 6] = ["font", "fill", "borders", "align", "num_fmt", "protection"];
let err = |m: String| bad("home.conditionalFormat", m);
let err = |m: String| bad(cmd, m);
let Some(obj) = st.as_object() else { return Err(err("`style` must be an object, e.g. {\"fill\": \"#FFC7CE\"}".into())) };
let mut st = obj.clone();
if let Some(k) = st.keys().find(|k| !KEYS.contains(&k.as_str())) {
@@ -1604,7 +1604,7 @@ fn cf_style(rule: &Json, cmd: &str) -> Result<Style> {
let g = |k: &str| rule.get(k).and_then(Json::as_str).map(str::to_string);
match rule.get("style") {
Some(st) if !st.is_null() => {
let st = cf_style_json(st)?;
let st = cf_style_json(st, cmd)?;
let base = serde_json::to_value(Style::default()).map_err(|e| EngineError::Other(e.to_string()))?;
Ok(serde_json::from_value(super::format::merge_json(base, &st)).map_err(|e| bad(cmd, e.to_string()))?)
}
@@ -2173,6 +2173,20 @@ mod tests {
assert_eq!(formula, "A1>2", "the failed edit leaves the rule alone");
}
#[test]
fn manage_rules_names_itself_in_a_style_error() {
let mut s = Session::new();
s.new_workbook();
s.execute("home.conditionalFormat", json!({"range": "A1:A3", "rule": {"type": "colorScale", "colors": ["#112233", "#778899"]}})).unwrap();
// A style shared by both commands must name the one that was called.
let rule = json!({"type": "cellIs", "operator": "greater", "value": 1, "style": {"nope": 1}});
let err = s.execute("home.manageRules", json!({"edit": 0, "rule": rule.clone()})).unwrap_err().to_string();
assert!(err.contains("home.manageRules"), "{err}");
assert!(!err.contains("home.conditionalFormat"), "{err}");
let err = s.execute("home.conditionalFormat", json!({"range": "A1:A3", "rule": rule})).unwrap_err().to_string();
assert!(err.contains("home.conditionalFormat"), "{err}");
}
#[test]
fn manage_rules_names_itself_in_a_stop_count_error() {
let mut s = Session::new();
@@ -42,8 +42,12 @@ pub(super) fn manage_rules(app: &mut SheetApp, ui: &mut egui::Ui) {
/// scale (`edit` and `rule` in the params).
pub(super) fn new_rule_dialog(app: &mut SheetApp, params: &Json) -> Dialog {
let theme = app.session.active().map(|d| d.wb.theme.clone()).unwrap_or_default();
let mut d = Dialog::custom("newFormattingRule", "New Formatting Rule", new_rule_defaults(params, &theme));
d.command = Some(if params.get("edit").is_some() { "home.manageRules" } else { "home.conditionalFormat" });
// Excel titles the Manage Rules path "Edit Formatting Rule": OK replaces the rule rather than
// adding one, and the window should say so.
let editing = params.get("edit").is_some();
let title = if editing { "Edit Formatting Rule" } else { "New Formatting Rule" };
let mut d = Dialog::custom("newFormattingRule", title, new_rule_defaults(params, &theme));
d.command = Some(if editing { "home.manageRules" } else { "home.conditionalFormat" });
// The sheet and selection the rule was opened on: Excel's "Applies to" stays put
// even if the selection moves or another sheet is shown before OK.
let (sheet, applies_to) = app
+1
View File
@@ -562,6 +562,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Zoom Out" => "Oddálit",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Nové pravidlo formátování",
"Edit Formatting Rule" => "Upravit pravidlo formátování",
"Format all cells based on their values" => "Formátovat všechny buňky podle jejich hodnot",
"Format Style:" => "Styl formátu:",
"2-Color Scale" => "Dvoubarevná škála",
+1
View File
@@ -501,6 +501,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Dialogfeld erweitern",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Neue Formatierungsregel",
"Edit Formatting Rule" => "Formatierungsregel bearbeiten",
"Select a Rule Type" => "Regeltyp auswählen",
"Edit the Rule Description" => "Regelbeschreibung bearbeiten",
"Format all cells based on their values" => "Alle Zellen basierend auf ihren Werten formatieren",
+1
View File
@@ -998,6 +998,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Ανάπτυξη παραθύρου διαλόγου",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Νέος κανόνας μορφοποίησης",
"Edit Formatting Rule" => "Επεξεργασία κανόνα μορφοποίησης",
"Select a Rule Type" => "Επιλογή τύπου κανόνα",
"Edit the Rule Description" => "Επεξεργασία περιγραφής κανόνα",
"Format all cells based on their values" => "Μορφοποίηση όλων των κελιών με βάση τις τιμές τους",
+1
View File
@@ -507,6 +507,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Expandir diálogo",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Nueva regla de formato",
"Edit Formatting Rule" => "Editar regla de formato",
"Select a Rule Type" => "Seleccionar un tipo de regla",
"Edit the Rule Description" => "Editar la descripción de la regla",
"Format all cells based on their values" => "Dar formato a todas las celdas según sus valores",
+1
View File
@@ -469,6 +469,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Développer la boîte de dialogue",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Nouvelle règle de mise en forme",
"Edit Formatting Rule" => "Modifier la règle de mise en forme",
"Select a Rule Type" => "Sélectionner un type de règle",
"Edit the Rule Description" => "Modifier la description de la règle",
"Format all cells based on their values" => "Mettre en forme toutes les cellules en fonction de leurs valeurs",
+1
View File
@@ -1002,6 +1002,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Espandi finestra di dialogo",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Nuova regola di formattazione",
"Edit Formatting Rule" => "Modifica regola di formattazione",
"Select a Rule Type" => "Seleziona un tipo di regola",
"Edit the Rule Description" => "Modifica la descrizione della regola",
"Format all cells based on their values" => "Formatta tutte le celle in base ai loro valori",
+1
View File
@@ -1005,6 +1005,7 @@ pub(super) const DIALOGS: &[(&str, &str)] = &[
("Collapse dialog", "ダイアログを折りたたむ"),
("Expand dialog", "ダイアログを展開する"),
("New Formatting Rule", "新しい書式ルール"),
("Edit Formatting Rule", "書式ルールの編集"),
("Select a Rule Type", "ルールの種類を選択"),
("Edit the Rule Description", "ルールの説明を編集"),
("Format all cells based on their values", "値に基づいてすべてのセルを書式設定"),
+1
View File
@@ -475,6 +475,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "대화 상자 확장",
// Conditional formatting: colour scales.
"New Formatting Rule" => "새 서식 규칙",
"Edit Formatting Rule" => "서식 규칙 편집",
"Select a Rule Type" => "규칙 유형 선택",
"Edit the Rule Description" => "규칙 설명 편집",
"Format all cells based on their values" => "해당 값에 따라 모든 셀의 서식 지정",
+1
View File
@@ -544,6 +544,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Dialoogvenster uitvouwen",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Nieuwe opmaakregel",
"Edit Formatting Rule" => "Opmaakregel bewerken",
"Select a Rule Type" => "Een regeltype selecteren",
"Edit the Rule Description" => "De beschrijving van de regel bewerken",
"Format all cells based on their values" => "Alle cellen opmaken op basis van hun waarden",
+1
View File
@@ -992,6 +992,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Rozwiń okno dialogowe",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Nowa reguła formatowania",
"Edit Formatting Rule" => "Edytuj regułę formatowania",
"Select a Rule Type" => "Wybierz typ reguły",
"Edit the Rule Description" => "Edytuj opis reguły",
"Format all cells based on their values" => "Formatuj wszystkie komórki na podstawie ich wartości",
+1
View File
@@ -1008,6 +1008,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Expandir caixa de diálogo",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Nova Regra de Formatação",
"Edit Formatting Rule" => "Editar Regra de Formatação",
"Select a Rule Type" => "Selecionar um Tipo de Regra",
"Edit the Rule Description" => "Editar a Descrição da Regra",
"Format all cells based on their values" => "Formatar todas as células com base em seus valores",
+1
View File
@@ -1054,6 +1054,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Extindere casetă de dialog",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Regulă de formatare nouă",
"Edit Formatting Rule" => "Editați regula de formatare",
"Select a Rule Type" => "Selectați un tip de regulă",
"Edit the Rule Description" => "Editați descrierea regulii",
"Format all cells based on their values" => "Formatați toate celulele în funcție de valorile lor",
+1
View File
@@ -477,6 +477,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Развернуть диалоговое окно",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Новое правило форматирования",
"Edit Formatting Rule" => "Изменение правила форматирования",
"Select a Rule Type" => "Выбор типа правила",
"Edit the Rule Description" => "Изменение описания правила",
"Format all cells based on their values" => "Форматировать все ячейки на основе их значений",
+1
View File
@@ -464,6 +464,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Proširi dijalog",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Novo pravilo oblikovanja",
"Edit Formatting Rule" => "Izmeni pravilo oblikovanja",
"Select a Rule Type" => "Izaberi tip pravila",
"Edit the Rule Description" => "Uredi opis pravila",
"Format all cells based on their values" => "Oblikuj sve ćelije na osnovu njihovih vrednosti",
+1
View File
@@ -434,6 +434,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "İletişim kutusunu genişlet",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Yeni Biçimlendirme Kuralı",
"Edit Formatting Rule" => "Biçimlendirme Kuralını Düzenle",
"Select a Rule Type" => "Kural Türü Seçin",
"Edit the Rule Description" => "Kural Açıklamasını Düzenle",
"Format all cells based on their values" => "Tüm hücreleri değerlerine göre biçimlendir",
+1
View File
@@ -996,6 +996,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Розгорнути діалогове вікно",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Нове правило форматування",
"Edit Formatting Rule" => "Змінити правило форматування",
"Select a Rule Type" => "Виберіть тип правила",
"Edit the Rule Description" => "Редагувати опис правила",
"Format all cells based on their values" => "Форматувати всі клітинки за їхніми значеннями",
+1
View File
@@ -436,6 +436,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "Mở rộng hộp thoại",
// Conditional formatting: colour scales.
"New Formatting Rule" => "Quy tắc định dạng mới",
"Edit Formatting Rule" => "Chỉnh sửa quy tắc định dạng",
"Select a Rule Type" => "Chọn loại quy tắc",
"Edit the Rule Description" => "Sửa mô tả quy tắc",
"Format all cells based on their values" => "Định dạng tất cả ô dựa trên giá trị của chúng",
+1
View File
@@ -467,6 +467,7 @@ pub(super) fn translate(text: &str) -> Option<&'static str> {
"Expand dialog" => "展开对话框",
// Conditional formatting: colour scales.
"New Formatting Rule" => "新建格式规则",
"Edit Formatting Rule" => "编辑格式规则",
"Select a Rule Type" => "选择规则类型",
"Edit the Rule Description" => "编辑规则说明",
"Format all cells based on their values" => "基于各自值设置所有单元格的格式",