locked
Dynamic Analysis Code Like Excel Formula RRS feed

  • Question

  • User-47589850 posted

    Hi all,

    I want dynamic analysis formula on asp.net .
    I have many formula on excel. For example; 
    =IF(C2<B2;"OK";"NO") I want migrate on asp.net mvc. 

    I want exactly this. The user write new analysis code on the site and save database code and run it excel formula or c# code etc.

    According to research, it is not possible to write a dynamic excel formula?
    I think this can be work in php eval and javascript eval. 

    How should I proceed?

    Saturday, June 27, 2020 1:06 PM

All replies

  • User-474980206 posted

    C# is not a dynamic language, you just write an interpreter in C#. Here is a simple sample

       https://github.com/Timu5/BasicSharp

    a more robust solution is to use clear script to host an engine like JavaScript V8

      https://github.com/microsoft/ClearScript

    Saturday, June 27, 2020 3:26 PM
  • User-47589850 posted

    Thanks for your feedback. 
    Do you think I should do this with javascript and jquery? Can you give an example?

    Saturday, June 27, 2020 6:31 PM
  • User1686398519 posted

    Hi asp.netlearning,

    According to your needs, I made an example, please refer to it.You can modify it according to your actual situation.

    <h2>Index</h2>
    C2:
    <input id="C2" />
    <br />
    B2:
    <input id="B2" />
    <br />
    <input type="hidden" id="formulahidden" />
    result:<input id="result" />
    <br />
    <p id="message"></p>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script>
        $("#C2,#B2").change(function () {
            var flag = true;
            var result = $("#formulahidden").val();
            var pattern = /([=][I][F][\(][A-Za-z][0-9][(>|<)][A-Za-z][0-9][;]["])([\s\S]*)(["][;]["])([\s\S]*)(["][\)])/;
            if (pattern.test(result)) {
                var position1 = result.indexOf("(");
                var position2 = -1;
                if (result.indexOf("<") != -1) {
                    position2 = result.indexOf("<");
                }
                if (result.indexOf(">") != -1) {
                    position2 = result.indexOf(">");
                    flag = false;
                }
                var position3 = result.indexOf(";");
                var position4 = result.indexOf("\"");
                var position5 = result.indexOf("\";\"");
                var position6 = result.lastIndexOf("\"");
                var val1 = result.substring(position1 + 1, position2);
                var val2 = result.substring(position2 + 1, position3);
                var val3 = result.substring(position4 + 1, position5);
                var val4 = result.substring(position5 + 3, position6);
                var c2val = $("#" + val1).val();
                var b2val = $("#" + val2).val();
                if (flag) {
                    c2val < b2val ? $("#result").val(val3) : $("#result").val(val4);
                } else {
                    c2val > b2val ? $("#result").val(val3) : $("#result").val(val4);
                }
            } 
        });
        $("#result").change(function () {
            $("#formulahidden").val($("#result").val());
        });
        $("#result").dblclick(function () {
            $("#result").val($("#formulahidden").val());
        });
    </script>

    Here is the result.

     

    Best Regards,

    YihuiSun

    Monday, June 29, 2020 9:47 AM
  • User-47589850 posted

    Hi YihuiSun,

    Is it too much code for a single formula? I'm finded a jquery plugin.  have you ever used this?
    https://formulajs.info/

    Wednesday, July 1, 2020 9:01 PM