Exemple #1
0
 public ExcelParamsArgumentAttribute(ExcelArgumentAttribute original)
 {
     // Just copy all the fields
     AllowReference = original.AllowReference;
     Description    = original.Description;
     Name           = original.Name;
 }
Exemple #2
0
        }                                                          // Should not be null, and elements should not be null

        public ExcelParameterRegistration(ExcelArgumentAttribute argumentAttribute)
        {
            if (argumentAttribute == null)
            {
                throw new ArgumentNullException("argumentAttribute");
            }
            ArgumentAttribute = argumentAttribute;

            CustomAttributes = new List <object>();
        }
        }                                                                        // The method this entry was originally constructed with (may be useful for transformations).

        // NOTE: 16 parameter max for Expression.GetDelegateType
        public RegistrationEntry(MethodInfo methodInfo)
        {
            MethodInfo = methodInfo;

            var paramExprs = methodInfo.GetParameters()
                             .Select(pi => Expression.Parameter(pi.ParameterType, pi.Name))
                             .ToArray();

            FunctionLambda = Expression.Lambda(Expression.Call(methodInfo, paramExprs), methodInfo.Name, paramExprs);

            // Need to make sure we have explicit
            FunctionAttribute = methodInfo.GetCustomAttribute <ExcelFunctionAttribute>();
            if (FunctionAttribute == null)
            {
                FunctionAttribute = new ExcelFunctionAttribute {
                    Name = methodInfo.Name
                }
            }
            ;
            else if (string.IsNullOrEmpty(FunctionAttribute.Name))
            {
                FunctionAttribute.Name = methodInfo.Name;
            }

            ArgumentAttributes = new List <ExcelArgumentAttribute>();
            foreach (var pi in methodInfo.GetParameters())
            {
                var argAtt = pi.GetCustomAttribute <ExcelArgumentAttribute>();
                if (argAtt == null)
                {
                    argAtt = new ExcelArgumentAttribute {
                        Name = pi.Name
                    }
                }
                ;
                else if (string.IsNullOrEmpty(argAtt.Name))
                {
                    argAtt.Name = pi.Name;
                }

                ArgumentAttributes.Add(argAtt);
            }

            // Special check for final Params argument - transform to an ExcelParamsArgumentAttribute
            // NOTE: This won't work with a custom derived attribute...
            var lastParam = methodInfo.GetParameters().LastOrDefault();

            if (lastParam != null && lastParam.GetCustomAttribute <ParamArrayAttribute>() != null)
            {
                var excelParamsAtt = new ExcelParamsArgumentAttribute(ArgumentAttributes.Last());
                ArgumentAttributes[ArgumentAttributes.Count - 1] = excelParamsAtt;
            }
        }
Exemple #4
0
        public static RegistrationResults RegisterApi(OpenApiDocument apiDefinition, bool reregister = false)
        {
            List <Delegate>       delegates      = new List <Delegate>();
            List <object>         funcAttribs    = new List <object>();
            List <List <object> > argAttribsList = new List <List <object> >();

            var functionsAdded = new List <string>();

            foreach (var path in apiDefinition.Paths)
            {
                foreach (var operation in path.Value.Operations)
                {
                    delegates.Add(CreateDelegateForOperation(path.Key, path.Value, operation.Key, operation.Value));

                    ExcelFunctionAttribute att = new ExcelFunctionAttribute();

                    att.Name = operation.Value.OperationId;

                    att.Description            = operation.Value.Description;
                    att.HelpTopic              = apiDefinition.ExternalDocs?.Url?.ToString();
                    att.SuppressOverwriteError = reregister;

                    funcAttribs.Add(att);
                    List <object> argAttribs = new List <object>();

                    foreach (var parameter in operation.Value.Parameters)
                    {
                        ExcelArgumentAttribute atta1 = new ExcelArgumentAttribute();
                        atta1.Name        = parameter.Name;
                        atta1.Description = parameter.Description;

                        argAttribs.Add(atta1);
                    }

                    argAttribsList.Add(argAttribs);

                    functionsAdded.Add(att.Name);
                }
            }

            ExcelIntegration.RegisterDelegates(delegates, funcAttribs, argAttribsList);

            var registrationResults = new RegistrationResults
            {
                FunctionsAdded = functionsAdded
            };

            return(registrationResults);
        }
Exemple #5
0
        /// <summary>
        /// Also craetes attributes from Optional / Default Value
        /// </summary>
        /// <param name="parameterInfo"></param>
        public ExcelParameterRegistration(ParameterInfo parameterInfo)
        {
            CustomAttributes = new List <object>();

            var allParameterAttributes = parameterInfo.GetCustomAttributes(true);

            foreach (var att in allParameterAttributes)
            {
                var argAtt = att as ExcelArgumentAttribute;
                if (argAtt != null)
                {
                    ArgumentAttribute = argAtt;
                    if (string.IsNullOrEmpty(ArgumentAttribute.Name))
                    {
                        ArgumentAttribute.Name = parameterInfo.Name;
                    }
                }
                else
                {
                    CustomAttributes.Add(att);
                }
            }

            // Check that the ExcelArgumentAttribute has been set
            if (ArgumentAttribute == null)
            {
                ArgumentAttribute = new ExcelArgumentAttribute {
                    Name = parameterInfo.Name
                };
            }

            // Extra processing for Optional / Default values
            // TODO: Also consider DefaultValueAttribute (which is wrong, but might be used...)
            if (parameterInfo.IsOptional && parameterInfo.DefaultValue != DBNull.Value)
            {
                Debug.Assert(CustomAttributes.OfType <OptionalAttribute>().Any());
                Debug.Assert(!CustomAttributes.OfType <DefaultParameterValueAttribute>().Any());
                CustomAttributes.Add(new DefaultParameterValueAttribute(parameterInfo.DefaultValue));
            }
        }
Exemple #6
0
    /// <summary>
    /// Registers a single function that is exposed by a manually and automatically written method.
    /// see http://www.quantsa.org/home_expose_to_excel.html
    /// </summary>
    /// <param name="method">The generated method.</param>
    /// <param name="manualMethod">The manual method.</param>
    /// <param name="isHidden">Is this function hidden.</param>
    private void AddSingleAutoFunction(MethodInfo method, MethodInfo manualMethod, bool?isHidden)
    {
        //Create the function attribute
        QuantSAExcelFunctionAttribute quantsaAttribute = manualMethod.GetCustomAttribute <QuantSAExcelFunctionAttribute>();

        if (isHidden != null)
        {
            quantsaAttribute.IsHidden = isHidden.Value;
        }
        functionAttributes.Add(quantsaAttribute.CreateExcelFunctionAttribute());

        // Create the function argument attributes
        List <object> thisArgumentAttributes = new List <object>();

        if (manualMethod.GetParameters().Length < method.GetParameters().Length)
        {
            ExcelArgumentAttribute argAttrib = new ExcelArgumentAttribute();
            argAttrib.Name        = "objectName";
            argAttrib.Description = "The name of the object to be created.";
            //Note that the above 2 strings are the same as those added in GenerateDocs, if they are changed here they should be changed there too.
            thisArgumentAttributes.Add(argAttrib);
        }
        foreach (ParameterInfo param in manualMethod.GetParameters())
        {
            var argAttrib = param.GetCustomAttribute <ExcelArgumentAttribute>();
            if (argAttrib != null)
            {
                argAttrib.Name = param.Name;
            }
            if (ExcelUtilities.InputTypeShouldHaveHelpLink(param.ParameterType))
            {
                string typeName = param.ParameterType.IsArray ? param.ParameterType.GetElementType().Name : param.ParameterType.Name;
                argAttrib.Description += "(" + typeName + ")";
            }
            thisArgumentAttributes.Add(argAttrib);
        }
        functionArgumentAttributes.Add(thisArgumentAttributes);
    }
Exemple #7
0
        public static IEnumerable <SsqExcelFunction> GetSsqExcelFunctions(SsqJson ssqJson)
        {
            List <SsqExcelFunction> ssqExcelFunctions = null;

            try
            {
                ssqExcelFunctions = new List <SsqExcelFunction>();

                foreach (KeyValuePair <string, UserDefinedFunction> udf in GetUserDefinedFunctions(ssqJson))
                {
                    UserDefinedFunction userDefinedFunction = udf.Value;
                    QueryInformation    queryInformation    = userDefinedFunction.QueryInformation;
                    QueryParameter      queryParameter      = userDefinedFunction.QueryParameter;

                    Delegate excelFunction = new Func <string, object>(wkn_isin_ticker =>
                    {
                        object value;

                        try
                        {
                            Ssq ssq = new Ssq(queryParameter,
                                              udf.Key);

                            value = ssq.TryQ(wkn_isin_ticker);
                        }
                        catch (Exception ex)
                        {
                            log.Error(ex.Message);
                            return(ExcelError.ExcelErrorGettingData);
                        }

                        return(value);
                    });

                    ExcelFunctionAttribute excelFunctionAttribute = new ExcelFunctionAttribute
                    {
                        Name        = queryInformation.Name,
                        Description = queryInformation.Description + "\n" +
                                      "Author: " + queryInformation.Author + " (" + queryInformation.AuthorEmail + ")\n" +
                                      "Version " + queryInformation.Version + " of " + queryInformation.VersionDate + ", Provider: " + queryInformation.Provider,
                        Category     = "FFE",
                        IsThreadSafe = true,
                        HelpTopic    = !String.IsNullOrEmpty(queryInformation.HelpTopic) ? queryInformation.HelpTopic : null
                    };

                    ExcelArgumentAttribute excelArgumentAttribute = new ExcelArgumentAttribute {
                        Name = queryInformation.ExcelArgNameStockIdentifier, Description = queryInformation.ExcelArgDescStockIdentifier
                    };

                    ssqExcelFunctions.Add(new SsqExcelFunction(queryInformation.Name, excelFunction, excelFunctionAttribute, excelArgumentAttribute));

                    log.Debug("Created SSQ Excel function: {@SsqExcelFunction}", queryInformation.Name);
                }
            }
            catch (Exception ex)
            {
                log.Error("Exception while creating SSQ Excel functions. {@ExceptionMessage}", ex.Message);

                throw;
            }

            return(ssqExcelFunctions);
        }
Exemple #8
0
        private static ExcelFunctionRegistration ToExcelFunctionRegistration(IScript script, string FunctionName)
        {
            var paramExprs = script
                             .Parameters
                             .Select(x => Expression.Parameter(GetExcelRegistrationTypeFor(x), x.Name))
                             .ToList();

            var methodInfo = typeof(ExcelScriptAddin).GetMethod(nameof(InternalRun), BindingFlags.Static | BindingFlags.NonPublic);

            var paramsToObj = paramExprs.Select(x => Expression.Convert(x, typeof(object))); // cast parameter to object, otherwise won't match function signature of ExcelScriptAddin.Run
            var paramsArray = Expression.NewArrayInit(typeof(object), paramsToObj.ToArray());
            var methodArgs  = new Expression[] { Expression.Constant(script, typeof(IScript)), paramsArray };

            LambdaExpression lambdaExpression = Expression.Lambda(Expression.Call(methodInfo, methodArgs), FunctionName, paramExprs);

            ExcelFunctionAttribute excelFunctionAttribute = new ExcelFunctionAttribute()
            {
                Name = FunctionName
            };

            if (!String.IsNullOrEmpty(script.Description))
            {
                excelFunctionAttribute.Description = script.Description;
            }

            IEnumerable <ExcelParameterRegistration> paramRegistrations = script
                                                                          .Parameters
                                                                          .Select((IParameter x) =>
            {
                var argumentAttribute = new ExcelArgumentAttribute()
                {
                    Name = x.Name
                };
                var parameterRegistration = new ExcelParameterRegistration(argumentAttribute);

                if (x.Type == typeof(Excel.Range) || x.Type == typeof(ExcelReference))
                {
                    argumentAttribute.AllowReference = true;
                }

                if (x.IsOptional)
                {
                    var optionalAttribute = new OptionalAttribute();
                    parameterRegistration.CustomAttributes.Add(optionalAttribute);

                    var defaultValueAttribute = new DefaultParameterValueAttribute(x.DefaultValue);
                    parameterRegistration.CustomAttributes.Add(defaultValueAttribute);
                }

                if (!String.IsNullOrEmpty(x.Description))
                {
                    argumentAttribute.Description = x.Description;
                }

                return(parameterRegistration);
            });

            var reg = new ExcelFunctionRegistration(lambdaExpression, excelFunctionAttribute, paramRegistrations);

            return(reg);
        }
Exemple #9
0
 public SsqExcelFunction(string name, Delegate func, ExcelFunctionAttribute excelFunctionAttribute, ExcelArgumentAttribute excelArgumentAttribute)
 {
     Name     = name;
     Delegate = func;
     ExcelFunctionAttribute  = excelFunctionAttribute;
     ExcelArgumentAttributes = new List <object>()
     {
         excelArgumentAttribute
     };
 }
Exemple #10
0
        private static bool registerOneFunction(string method)
        {
            var request = new RestRequest("excel-service/info/" + method, Method.GET);

            request.AddHeader("Authorization", "Bearer " + settings.token);
            var response = settings.client.Execute <Utils.QLAPIInfo>(request);

            Utils.QLAPIInfo info = response.Data;
            if (info == null)
            {
                return(false);
            }
            info.provider = "excel-service";
            var apiAttr = new ExcelFunctionAttribute
            {
                Name        = info.method,
                Category    = "BCT Excel Addin",
                Description = info.description
            };
            List <Utils.QLAPIParam> args     = info.args;
            List <object>           argAttrs = new List <object>();

            foreach (var arg in args)
            {
                var attr = new ExcelArgumentAttribute
                {
                    Name        = arg.name,
                    Description = arg.description
                };
                argAttrs.Add(attr);
            }
            Delegate func = null;

            switch (args.Count)
            {
            case 0:
                func = MakeExcelUDFArgs0(info);
                break;

            case 1:
                func = MakeExcelUDFArgs1(info);
                break;

            case 2:
                func = MakeExcelUDFArgs2(info);
                break;

            case 3:
                func = MakeExcelUDFArgs3(info);
                break;

            case 4:
                func = MakeExcelUDFArgs4(info);
                break;

            case 5:
                func = MakeExcelUDFArgs5(info);
                break;

            case 6:
                func = MakeExcelUDFArgs6(info);
                break;

            case 7:
                func = MakeExcelUDFArgs7(info);
                break;

            case 8:
                func = MakeExcelUDFArgs8(info);
                break;

            case 9:
                func = MakeExcelUDFArgs9(info);
                break;

            case 10:
                func = MakeExcelUDFArgs10(info);
                break;

            case 11:
                func = MakeExcelUDFArgs11(info);
                break;

            case 12:
                func = MakeExcelUDFArgs12(info);
                break;

            case 13:
                func = MakeExcelUDFArgs13(info);
                break;

            case 14:
                func = MakeExcelUDFArgs14(info);
                break;

            case 15:
                func = MakeExcelUDFArgs15(info);
                break;

            case 16:
                func = MakeExcelUDFArgs16(info);
                break;

            case 17:
                func = MakeExcelUDFArgs17(info);
                break;

            case 18:
                func = MakeExcelUDFArgs18(info);
                break;

            case 19:
                func = MakeExcelUDFArgs19(info);
                break;

            case 20:
                func = MakeExcelUDFArgs20(info);
                break;

            case 21:
                func = MakeExcelUDFArgs21(info);
                break;
            }
            if (func != null)
            {
                ExcelIntegration.RegisterDelegates(new List <Delegate> {
                    func
                },
                                                   new List <object> {
                    apiAttr
                },
                                                   new List <List <object> > {
                    argAttrs
                });
                return(true);
            }
            return(false);
        }