Skip to content

Commit

Permalink
1551 new cell reference functions (stevencohn#1579)
Browse files Browse the repository at this point in the history
* Add cell, tablecols, tablerows functions
stevencohn#1551

* math function

* new calculator
stevencohn#1551

* trasnlate error messages

* translations

* translations
  • Loading branch information
stevencohn authored and weissm committed Sep 23, 2024
1 parent f19be4c commit 1e96af1
Show file tree
Hide file tree
Showing 30 changed files with 1,820 additions and 1,757 deletions.
12 changes: 3 additions & 9 deletions OneMore/Commands/Tables/AddFormulaCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,17 @@ public override async Task Execute(params object[] args)
return;
}

using var dialog = new FormulaDialog();
using var dialog = new FormulaDialog(table);

// display selected cell names
if (cells.Count == 1)
{
dialog.SetCellNames(cells[0].Coordinates);
dialog.SetResultRow(cells[0].RowNum);
}
else
{
dialog.SetCellNames(
$"{cells[0].Coordinates} - {cells[cells.Count - 1].Coordinates}");

if (range == TableSelectionRange.Rows)
{
dialog.SetResultRow(cells[cells.Count - 1].RowNum);
}
}

var cell = cells[0];
Expand Down Expand Up @@ -144,7 +138,7 @@ public override async Task Execute(params object[] args)
}


private void StoreFormula(
private static void StoreFormula(
Table table, IEnumerable<TableCell> cells,
string expression, FormulaFormat format, int dplaces,
TableSelectionRange rangeType, string tagIndex)
Expand All @@ -162,7 +156,7 @@ private void StoreFormula(
return;
}

var regex = new Regex(Processor.OffsetPattern);
var regex = new Regex(Processor.AddressPattern);

int offset = 0;
foreach (var cell in cells)
Expand Down
3 changes: 3 additions & 0 deletions OneMore/Commands/Tables/FormulaDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 39 additions & 20 deletions OneMore/Commands/Tables/FormulaDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
namespace River.OneMoreAddIn.Commands
{
using River.OneMoreAddIn.Commands.Tables.Formulas;
using River.OneMoreAddIn.Models;
using System;
using System.Text.RegularExpressions;
using System.Linq;
using Resx = Properties.Resources;


internal partial class FormulaDialog : UI.MoreForm
{
private readonly int helpHeight;
private readonly Calculator calculator;
private int resultRow;
private readonly Table table;


public FormulaDialog()
Expand Down Expand Up @@ -50,7 +51,21 @@ public FormulaDialog()
formatBox.SelectedIndex = 0;

calculator = new Calculator();
calculator.ProcessSymbol += ResolveSymbol;
calculator.GetCellValue += GetCellValue;
}


public FormulaDialog(Table table)
: this()
{
this.table = table;

calculator.SetVariable("tablecols", table.ColumnCount);
calculator.SetVariable("tablerows", table.RowCount);

var cell = table.GetSelectedCells(out _).First();
calculator.SetVariable("col", cell.ColNum);
calculator.SetVariable("row", cell.RowNum);
}


Expand Down Expand Up @@ -81,12 +96,6 @@ public void SetCellNames(string names)
}


public void SetResultRow(int row)
{
resultRow = row;
}


public bool Tagged
{
get => tagBox.Checked;
Expand All @@ -102,38 +111,48 @@ private void ChangedFormula(object sender, EventArgs e)
{
try
{
calculator.Execute(formula, indexOffset: resultRow);
var result = calculator.Compute(formula);
validStatusLabel.ForeColor = manager.GetColor("ControlText");
validStatusLabel.Text = Resx.word_OK;

validStatusLabel.Text = $"{Resx.word_OK} ({result})";
tooltip.SetToolTip(validStatusLabel, string.Empty);

okButton.Enabled = true;
}
catch
catch (Exception exc)
{
validStatusLabel.ForeColor = manager.GetColor("ErrorText");
validStatusLabel.Text = Resx.FormulaDialog_status_Invalid;

tooltip.SetToolTip(validStatusLabel, exc.Message);

okButton.Enabled = false;
}
}
else
{
validStatusLabel.ForeColor = manager.GetColor("ControlText");
validStatusLabel.Text = Resx.FormulaDialog_status_Empty;
tooltip.SetToolTip(validStatusLabel, string.Empty);
okButton.Enabled = false;
}
}


private void ResolveSymbol(object sender, SymbolEventArgs e)
private void GetCellValue(object sender, GetCellValueEventArgs e)
{
if (Regex.Match(e.Name, Processor.OffsetPattern).Success)
var cell = table.GetCell(e.Name.ToUpper());
if (cell is null)
{
e.SetResult(1.0);
e.Status = SymbolStatus.OK;
}
else
{
e.Status = SymbolStatus.UndefinedSymbol;
e.Value = string.Empty;
return;
}

e.Value = cell.GetText().Trim()
.Replace(AddIn.Culture.NumberFormat.CurrencySymbol, string.Empty)
.Replace(AddIn.Culture.NumberFormat.PercentSymbol, string.Empty);

logger.Verbose($"FormulaDialog.GetCellValue({e.Name}) = [{e.Value}]");
}


Expand Down
3 changes: 3 additions & 0 deletions OneMore/Commands/Tables/FormulaDialog.resx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ Cell reference: A1, A2, ... ZZZ999
Cell range: A1:A22
Relative range in a column: A1:A-1</value>
</data>
<metadata name="tooltip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down
Loading

0 comments on commit 1e96af1

Please sign in to comment.