// Hook: AfterPacketGeneration // Replace every formula on the "Distribution" sheet with its calculated value. // // Recipients see only the result and cannot reverse-engineer the formulas. // Useful for confidential reports where the calculation logic itself is // considered IP. Other sheets are untouched. var sheet = Workbook.Worksheets["Distribution"]; if (sheet == null) { AddWarning("Sheet 'Distribution' not found - skipping formula removal."); return; } // Two things to know here: // 1. Cells is a NON-GENERIC IEnumerable, so `var cell` would bind as object and // .IsFormula / .Value would not resolve. Declare the loop variable's type. // 2. `Cell` on its own is AMBIGUOUS - the script runner pre-imports both // Aspose.Cells and Aspose.Slides, and each defines a Cell. Qualify it. // Collect first, then rewrite, so nothing is mutated mid-enumeration. var formulaCells = new List(); foreach (Aspose.Cells.Cell cell in sheet.Cells) { if (cell.IsFormula) { formulaCells.Add(cell); } } foreach (var cell in formulaCells) { cell.PutValue(cell.Value); // replace the formula with its calculated value } int converted = formulaCells.Count; Logger.LogInformation($"Converted {converted} formula(s) to static values on 'Distribution'.");