InvalidOperationException "Stack empty." on Save with nested MAP/LAMBDA formula

Posted by: yyoshimura on 29 September 2026, 2:25 am EST

  • Posted 29 September 2026, 2:25 am EST

    Hi,

    We have a workbook with dynamic array formulas using MAP/REDUCE, LAMBDA and INDIRECT, and when it is saved using the library, System.InvalidOperationException “Stack empty.” is thrown. Microsoft Excel calculates and saves the same workbook without any problem.

    In the workbook, each SumXX sheet sums the same cell on the sheets listed in C13:C1x with a formula like this:

    =SUM(MAP(INDIRECT($D$13),LAMBDA(x,INDIRECT(“'”&x&“'!”&ADDRESS(ROW(),COLUMN())))))

    Some of the listed sheets are also SumXX sheets with the same formula (nested up to 3 levels). Smaller workbooks with only one or two nested sheets do not reproduce the problem. Calling Workbook.Calculate() before Save() does not throw.

    Please see attachment for reproduction (open the workbook and save it). We confirmed this with DS.Documents.Excel 8.1.5, 9.0.3 and 9.2.1 (.NET 8, Windows).

    Stack trace:
    System.InvalidOperationException: Stack empty.
       at System.Collections.Generic.Stack 1.ThrowForEmptyStack()
       at System.Collections.Generic.Stack 1.Pop()
       at GrapeCity.Documents.Excel.xj.i(mu a)
       at GrapeCity.Documents.Excel.xj.a(mu a)
       at GrapeCity.Documents.Excel.xj.os(mu a)
       at GrapeCity.Documents.Excel.Functions.gnt.bi(mu a, lk b)
       at GrapeCity.Documents.Excel.xj.os(mu a)
       ...
       at GrapeCity.Documents.Excel.Workbook.Save(String fileName)

    Please help to resolve this issue. Thank you.

    Regards

    GcExcelTest_Lambda.zip

  • Posted 29 September 2026, 6:50 am EST - Updated 29 September 2026, 11:29 pm EST

    Hello,

    Thank you for providing the reproducible sample. We were able to reproduce the behavior on our end.

    We are getting in touch with our development team for further investigation. We will update you as soon as we hear back from them. [Internal Tracking ID: DSX-14838]

    In the meantime, to resolve this, you can try

    1. Call Calculate() before saving, as you already mentioned
    2. Use Cached Values, that is, open the file with DoNotRecalculateAfterOpened flag set to true

    We have modified the sample with the second implementation for your reference. Please take a look at it, and if you face any issues, please let us know.

    Best regards,

    Vivek Singh

    GcExcelTest.zip

  • Posted 29 September 2026, 11:30 pm EST

    Hello,

    Thank you for your patience while we discussed the issue with the development team.

    They have confirmed the issue as a bug related to the LAMBDA function, which will be fixed in the 9.2.3 hotfix release. Rest assured, we are monitoring this closely, and we will update you as soon as the release becomes public.

    Best regards,

    Vivek Singh

  • Posted 30 September 2026, 5:26 am EST

    Hi Vivek,

    Thank you for the quick response and the modified sample.

    Unfortunately, the two workarounds cannot be applied to our application. They work only while nothing is written to the workbook between Open and Save. Our application writes values into cells and reads formula results before saving (we already open with

    DoNotRecalculateAfterOpened = true
    ). As soon as one cell value is set, the same “Stack empty.” exception is thrown on the next read of a formula cell or on Save, even when
    Calculate()
    was called before.

    I extended your sample to show this (attached, cases A1 to B1; LambdaTest.xlsx is unchanged). The results are the same with 8.1.5 and 9.2.1.

    We found another workaround, which is case C1 in the sample, and it passed our tests:

    workbook.EnableCalculation = false;   // stays off all the time
    // ... write values ...
    workbook.EnableCalculation = true;    // on only during Calculate()
    workbook.Calculate();
    workbook.EnableCalculation = false;
    // ... read formula values / Save ...
    

    We plan to use this until the fix is released. Could you please confirm that this is a valid way to use the library, and let us know if there is any side effect we should be aware of?

    Thank you.

    Regards

    GcExcelTest_Lambda_v2.zip

  • Posted 30 September 2026, 8:35 pm EST - Updated 30 September 2026, 8:38 pm EST

    Hello Vivek,

    Thank you for the update, and for confirming the cause with the development team.

    We understand that this is a bug related to the LAMBDA function and that it will be fixed in the 9.2.3 hotfix release. We will discuss this with our team and wait for the release.

    We would appreciate it if you could let us know once it becomes publicly available.

    Best regards

  • Posted 1 October 2026, 12:36 am EST

    Hi Yukinori,

    Based on the behavior you demonstrated, using EnableCalculation = false during updates and reads, then enabling it only while performing the calculation, is a reasonable workaround for this workflow until the fix is available.

    Please ensure that you call the Calculate method after all relevant input changes and before reading formula results or saving. After calculation completes, you can disable calculation and read the updated formula results. If you then change relevant inputs while calculation remains disabled, formula reads will continue to return the results from the last calculation until you enable calculation and calculate again.

    We plan to release version 9.2.3 by October 30, 2026. In the meantime, you can continue using the workaround.

    Please let us know if you encounter any issues with this workaround.

    Best Regards,

    Chirag

  • Posted 2 October 2026, 1:18 am EST

    Hi Chirag,

    Thank you for your reply.

    We have tested the workaround and found no other issues. We will use this solution temporarily until the fix is released.

    Thank you also for letting us know the release date for version 9.2.3. We will keep in touch.

    Best regards

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels