diff --git a/ExcelGenericUDF/ExcelGenericUDF.csproj b/ExcelGenericUDF/ExcelGenericUDF.csproj index ad6c2b5..14e0b9b 100644 --- a/ExcelGenericUDF/ExcelGenericUDF.csproj +++ b/ExcelGenericUDF/ExcelGenericUDF.csproj @@ -43,6 +43,11 @@ True False + + True + ..\packages\Microsoft.Office.Interop.Excel.15.0.4795.1000\lib\net20\Microsoft.Office.Interop.Excel.dll + True + diff --git a/ExcelGenericUDF/Udf.cs b/ExcelGenericUDF/Udf.cs index d798aa9..50b912d 100644 --- a/ExcelGenericUDF/Udf.cs +++ b/ExcelGenericUDF/Udf.cs @@ -7,6 +7,8 @@ using ExcelDna.Integration; using ExcelDna.IntelliSense; +using Excel = Microsoft.Office.Interop.Excel; + namespace ExcelGenericUDF { public class Udf : IExcelAddIn @@ -21,7 +23,7 @@ public void AutoClose() } - [ExcelFunction(Description = "Data una matrice con tipico, potenza e lunghezza del cavo restituisce la sezione")] + [ExcelFunction(Description = "Data una matrice con tipico, potenza e lunghezza del cavo restituisce la sezione", Name ="Claudio.CalcolaCavi")] public static string ClaudioCalcolaCavi( [ExcelArgument(Description = "Inserire una matrice dove nella prima colonna è indicato il tipico, nella seconda la potenza (kW) e nelle restanti righe di intestazione le varie lunghezze del cavo", Name = "Matrice Cavi")] object[,] matriceCavi, [ExcelArgument(Description = "Inserire il tipico desiderato già presente nella matrice", Name = "Tipico")] string tipico, @@ -60,5 +62,90 @@ public static string ClaudioCalcolaCavi( return e.ToString(); } } + + [ExcelFunction(Description = "Dato il numero della scheda ne restituisce il nome",Name = "Claudio.Scheda.Nome")] + public static object ClaudioNomeScheda( + [ExcelArgument(Name = "Numero Scheda", Description = "Inserire il numero della scheda")] int i) + { + try + { + Excel.Application application = (Excel.Application)ExcelDnaUtil.Application; + Excel.Workbook workbook = application.ActiveWorkbook; + Excel.Worksheet worksheets = workbook.Worksheets[i]; + + return worksheets.Name; + } + catch (Exception e) + { + return e.ToString(); + } + } + + [ExcelFunction(Description = "Dato il numero della scheda ne modifica il nome", Name ="Claudio.Scheda.Rinomia")] + public static object ClaudioRinomicaScheda( + [ExcelArgument(Name ="Numero Scheda", Description ="Inserire il numero della scheda")] int i, + [ExcelArgument(Name ="Nome", Description ="Inserire il nuovo nome della scheda")] string nome) + { + try + { + Excel.Application application = (Excel.Application)ExcelDnaUtil.Application; + Excel.Workbook workbook = application.ActiveWorkbook; + Excel.Worksheet worksheets = workbook.Worksheets[i]; + + worksheets.Name = nome; + + return worksheets.Name; + } + catch (Exception e) + { + return e.ToString(); + } + } + [ExcelFunction(Name ="Claudio.Stringa.Spazia", Description ="Data una stringa ed un pattern ne spazia il contenuto")] + public static string ClaudioSepara( + [ExcelArgument(Name ="Stringa Iniziale", Description ="")] string nome, + [ExcelArgument(Name = "Pattern", Description = "Indica con quanti caratteri separatori riempiere la sottostringa. es. 10-10-5")] string Pattern, + [ExcelArgument(Name = "Carattere Separatore", Description = "Indica con quale carattere la stringa viene separata")] string Separatore, + [ExcelArgument(Name = "Carattere di Riempimento", Description = "Indica con quale carattere riempire la stringa")] string CarattereRiempimento) + { + try + { + string[] PatternDiviso = Pattern.Split(char.Parse(Separatore)); + var lista = nome.Split(char.Parse(Separatore)); + + string nuovaStringa = ""; + + for (int i = 0; i < lista.Length; i++) + { + int quantitaSpazi; + try + { + if (int.Parse(PatternDiviso[i]) == 0) + { + quantitaSpazi = 0; + } + else + { + quantitaSpazi = int.Parse(PatternDiviso[i]) - lista[i].Length; + } + } + catch (Exception) + { + quantitaSpazi = 0; + } + + nuovaStringa = nuovaStringa + new string(char.Parse(CarattereRiempimento), quantitaSpazi) + lista[i] + Separatore; + } + nuovaStringa = nuovaStringa.Substring(0, nuovaStringa.Length - 1); + + return nuovaStringa; + } + catch (Exception e) + { + return $"Stringa non corrispondente al pattern: {e.ToString()}"; + } + + } + } } diff --git a/ExcelGenericUDF/packages.config b/ExcelGenericUDF/packages.config index 21752b3..e4dcd2b 100644 --- a/ExcelGenericUDF/packages.config +++ b/ExcelGenericUDF/packages.config @@ -4,5 +4,6 @@ + \ No newline at end of file