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)
{
}
}
}
We have to let the user can click the Excel function to change arguments without error pop-ups.