3

We created a new Excel formulas/functions with C#, the partial code is as below:

using ExcelDna.Integration;
using Excel = Microsoft.Office.Interop.Excel;
namespace Test_Atune
{
    public class Dataset
    {
        public static string baseurl = Constant.URL_ROOT_COMMON;
        public static string headval = Constant.HEADVAL_COMMON;
        public static Request request = new Request();

        [ExcelFunction(Category = "test", IsMacroType = true, Description = "test dataset")]
        public static object TEST_DATASET(
                    [ExcelArgument(Description = "test_code")] string test_1,
                    [ExcelArgument(Description = "YYYYMMDD")] string test_2,
                    [ExcelArgument(Description = "YYYYMMDD")] string test_3
                    )
        {
            string parstr = @"/test_dataset/?" +
                @"test_1=" + test_1 +
                @"&test_2=" + test_2 +
                @"&test_3=" + test_3;
            ExcelReference caller = (ExcelReference)XlCall.Excel(XlCall.xlfCaller);
            Excel.Range rg = caller.ToPiaRange();
            return ExcelAsyncUtil.Run("TEST_DATASET",
                new object[] { parstr },
                () => request.GetDatasetFromUrl(Constant.URL_ROOT_COMMON, parstr, headval, rg));
        }
    }

}

the function works fine, but when we click the non-blank area and click the Excel function meanwhile, we will get an error as below(I also attached the screenshot of the error as below):

Microsoft.Csharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type “bool” to “system.func”

The Whole contents in left button is as below:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type “bool” to “system.func” 在 CallSite.Target(Closure , CallSite , Object ) 在 Test_AtuneAddIn.FundIndexTree.treeView1_NodeMouseDoubleClick(Object sender, TreeNodeMouseClickEventArgs e) 在 System.Windows.Forms.TreeView.OnNodeMouseDoubleClick(TreeNodeMouseClickEventArgs e) 在 System.Windows.Forms.TreeView.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

And below code is IndexTree.cs

using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using ExcelDna.Integration;
using System.Windows.Forms;

namespace Test_Atune
{
    [ComVisible(true)]
    public partial class FundIndexTree : UserControl
    {
        public IndexTree()
        {
            InitializeComponent();
        }
        RichText richText = new RichText();
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            Excel.Application ExcelApp = (Excel.Application)ExcelDnaUtil.Application;
            //Excel.Dialog dialoddg = ExcelApp.Application.Dialogs[Excel.XlBuiltInDialog.xlDialogFunctionWizard];

            // Return when there is child node
            TreeNode node = e.Node;
            if (node.Nodes.Count != 0 || !node.IsSelected)
            {
                return;
            }
            System.Func<dynamic> a;
            // If there is no child node display the functions of pop-up dialog
            Excel.Range activeCell = ExcelApp.ActiveCell;
            if (node.Tag != null && node.Tag.ToString().Replace(" ", "").Length > 0)
            {
                if (activeCell.HasFormula)
                {
                    if (activeCell.Formula.ToString().Substring(0, activeCell.Formula.ToString().IndexOf("(")).ToUpper().Replace(" ", "") ==
                        node.Tag.ToString().Substring(0, node.Tag.ToString().IndexOf("(")).ToUpper().Replace(" ", ""))
                    {
                        a = activeCell.FunctionWizard();
                        return;

                    }
                }
                activeCell.Value2 = node.Tag.ToString();
                 activeCell.FunctionWizard();
                return;
            }
            else
            {
                MessageBox.Show("Coming soon!!");
            }



       }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.richTextBox1.Text = richText.IndicatorDesc(e.Node.Text);

        }
        private void SplitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}

enter image description here

We have to let the user can click the Excel function to change arguments without error pop-ups.

  • Maybe this question can help you out: https://stackoverflow.com/questions/2424718/how-to-know-if-a-cell-has-an-error-in-the-formula-in-c-sharp – Rahul Sharma Jan 06 '20 at 06:36
  • @RahulSharma I will try, thank you so much for sharing~ –  Jan 06 '20 at 09:20
  • Any luck in getting this one resolved? – Rahul Sharma Jan 07 '20 at 06:37
  • @RahulSharma It's a pity I don't know where it change –  Jan 07 '20 at 13:44
  • Can you show the CallStack of the Exception? – Alois Kraus Jan 11 '20 at 17:07
  • @AloisKraus I am sorry, could you please tell me more details about what you need, I don't know what CallStack is....... –  Jan 13 '20 at 01:15
  • The popup has two buttons. Click the left one and show us the contents. – Alois Kraus Jan 13 '20 at 08:40
  • @AloisKraus Hi, it's > Microsoft.Csharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type “bool” to “system.func”, I updated the question with the whole contents of the left button, thanks so much –  Jan 13 '20 at 08:53
  • You should check the code in Test_AtuneAddIn..treeView1_NodeMouseDoubleClick where you seem to try to assign variables to the wrong target type. What exactly I cannot tell without the code in this handler and the variable declarations of your class which are used in that method. – Alois Kraus Jan 14 '20 at 21:00
  • @AloisKraus Hi, thanks so so so much for following this question, thank you so much!!!!! I updated my IndexTree.cs that included `treeView1_NodeMouseDoubleClic`, wish you have a nice weekend in advance. –  Jan 17 '20 at 07:44
  • Same to you. Glad that you have found the problematic code. – Alois Kraus Jan 17 '20 at 14:25
  • @AloisKraus I still don't know how to change them Thank you all the same for the comments –  Jan 21 '20 at 06:50

0 Answers0