Skip to content

Commit cab2a1d

Browse files
committed
Aggiunte nuove funzioni
1 parent fb1a5b3 commit cab2a1d

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

ExcelGenericUDF/ExcelGenericUDF.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
<Private>True</Private>
4444
<EmbedInteropTypes>False</EmbedInteropTypes>
4545
</Reference>
46+
<Reference Include="Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
47+
<EmbedInteropTypes>True</EmbedInteropTypes>
48+
<HintPath>..\packages\Microsoft.Office.Interop.Excel.15.0.4795.1000\lib\net20\Microsoft.Office.Interop.Excel.dll</HintPath>
49+
<Private>True</Private>
50+
</Reference>
4651
<Reference Include="System" />
4752
<Reference Include="System.Core" />
4853
<Reference Include="System.Xml.Linq" />

ExcelGenericUDF/Udf.cs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using ExcelDna.Integration;
88
using ExcelDna.IntelliSense;
99

10+
using Excel = Microsoft.Office.Interop.Excel;
11+
1012
namespace ExcelGenericUDF
1113
{
1214
public class Udf : IExcelAddIn
@@ -21,7 +23,7 @@ public void AutoClose()
2123

2224
}
2325

24-
[ExcelFunction(Description = "Data una matrice con tipico, potenza e lunghezza del cavo restituisce la sezione")]
26+
[ExcelFunction(Description = "Data una matrice con tipico, potenza e lunghezza del cavo restituisce la sezione", Name ="Claudio.CalcolaCavi")]
2527
public static string ClaudioCalcolaCavi(
2628
[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,
2729
[ExcelArgument(Description = "Inserire il tipico desiderato già presente nella matrice", Name = "Tipico")] string tipico,
@@ -60,5 +62,90 @@ public static string ClaudioCalcolaCavi(
6062
return e.ToString();
6163
}
6264
}
65+
66+
[ExcelFunction(Description = "Dato il numero della scheda ne restituisce il nome",Name = "Claudio.Scheda.Nome")]
67+
public static object ClaudioNomeScheda(
68+
[ExcelArgument(Name = "Numero Scheda", Description = "Inserire il numero della scheda")] int i)
69+
{
70+
try
71+
{
72+
Excel.Application application = (Excel.Application)ExcelDnaUtil.Application;
73+
Excel.Workbook workbook = application.ActiveWorkbook;
74+
Excel.Worksheet worksheets = workbook.Worksheets[i];
75+
76+
return worksheets.Name;
77+
}
78+
catch (Exception e)
79+
{
80+
return e.ToString();
81+
}
82+
}
83+
84+
[ExcelFunction(Description = "Dato il numero della scheda ne modifica il nome", Name ="Claudio.Scheda.Rinomia")]
85+
public static object ClaudioRinomicaScheda(
86+
[ExcelArgument(Name ="Numero Scheda", Description ="Inserire il numero della scheda")] int i,
87+
[ExcelArgument(Name ="Nome", Description ="Inserire il nuovo nome della scheda")] string nome)
88+
{
89+
try
90+
{
91+
Excel.Application application = (Excel.Application)ExcelDnaUtil.Application;
92+
Excel.Workbook workbook = application.ActiveWorkbook;
93+
Excel.Worksheet worksheets = workbook.Worksheets[i];
94+
95+
worksheets.Name = nome;
96+
97+
return worksheets.Name;
98+
}
99+
catch (Exception e)
100+
{
101+
return e.ToString();
102+
}
103+
}
104+
[ExcelFunction(Name ="Claudio.Stringa.Spazia", Description ="Data una stringa ed un pattern ne spazia il contenuto")]
105+
public static string ClaudioSepara(
106+
[ExcelArgument(Name ="Stringa Iniziale", Description ="")] string nome,
107+
[ExcelArgument(Name = "Pattern", Description = "Indica con quanti caratteri separatori riempiere la sottostringa. es. 10-10-5")] string Pattern,
108+
[ExcelArgument(Name = "Carattere Separatore", Description = "Indica con quale carattere la stringa viene separata")] string Separatore,
109+
[ExcelArgument(Name = "Carattere di Riempimento", Description = "Indica con quale carattere riempire la stringa")] string CarattereRiempimento)
110+
{
111+
try
112+
{
113+
string[] PatternDiviso = Pattern.Split(char.Parse(Separatore));
114+
var lista = nome.Split(char.Parse(Separatore));
115+
116+
string nuovaStringa = "";
117+
118+
for (int i = 0; i < lista.Length; i++)
119+
{
120+
int quantitaSpazi;
121+
try
122+
{
123+
if (int.Parse(PatternDiviso[i]) == 0)
124+
{
125+
quantitaSpazi = 0;
126+
}
127+
else
128+
{
129+
quantitaSpazi = int.Parse(PatternDiviso[i]) - lista[i].Length;
130+
}
131+
}
132+
catch (Exception)
133+
{
134+
quantitaSpazi = 0;
135+
}
136+
137+
nuovaStringa = nuovaStringa + new string(char.Parse(CarattereRiempimento), quantitaSpazi) + lista[i] + Separatore;
138+
}
139+
nuovaStringa = nuovaStringa.Substring(0, nuovaStringa.Length - 1);
140+
141+
return nuovaStringa;
142+
}
143+
catch (Exception e)
144+
{
145+
return $"Stringa non corrispondente al pattern: {e.ToString()}";
146+
}
147+
148+
}
149+
63150
}
64151
}

ExcelGenericUDF/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<package id="ExcelDna.AddIn" version="0.33.9" targetFramework="net452" />
55
<package id="ExcelDna.Integration" version="0.33.9" targetFramework="net452" />
66
<package id="ExcelDna.IntelliSense" version="1.0.0" targetFramework="net452" />
7+
<package id="Microsoft.Office.Interop.Excel" version="15.0.4795.1000" targetFramework="net452" />
78
<package id="UIAComWrapper" version="1.1.0.14" targetFramework="net452" />
89
</packages>

0 commit comments

Comments
 (0)