-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFunction.cs
More file actions
105 lines (94 loc) · 3.85 KB
/
Copy pathMyFunction.cs
File metadata and controls
105 lines (94 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using ExcelDna.Integration;
using ExcelDna.IntelliSense;
using ExcelDna.Registration;
using System;
using System.Collections.Concurrent;
using System.Linq;
using WPSExcelDna;
//using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
//using Microsoft.Office.Interop.Excel;
//using static System.Net.Mime.MediaTypeNames;
//using NumSharp;
//using static JsonRead.GlobalDict;
namespace WpsFunction
{
public class AddIn : IExcelAddIn
{
public void AutoOpen()
{
RegisterFunctions();
WpsReg.wpsRegistry();//wps注册
IntelliSenseServer.Install();
}
public void AutoClose()
{
IntelliSenseServer.Uninstall();
WpsReg.wpsUnRegistry();//wps反向注册
GC.Collect();//垃圾回收
}
public void RegisterFunctions()
{
// There are various options for wrapping and transforming your functions
// See the Source\Samples\Registration.Sample project for a comprehensive example
// Here we just change the attribute before registering the functions
ExcelRegistration.GetExcelFunctions()
.Select(UpdateHelpTopic)
.ProcessParamsRegistrations()
.RegisterFunctions();
}
public ExcelFunctionRegistration UpdateHelpTopic(ExcelFunctionRegistration funcReg)
{
funcReg.FunctionAttribute.HelpTopic = "http://www.bing.com";
return funcReg;
}
}
public class sthExcelFunction//excel普通公式
{
[ExcelFunction(ExplicitRegistration = true, Category = "字符串连接", Description = "根据连接参数连接字符串")]
public static string sthLink(
[ExcelArgument(Name = "符号", Description = "连接符号")] string symbol,
[ExcelArgument(Name = "数据", Description = "输入需要连接的数据")] params object[] optional)
{
StringBuilder str = new StringBuilder(100);
foreach (var arg in optional)
{
if (arg is object[,])
{
int rows = ((object[,])arg).GetLength(0);//行数量
int cols = ((object[,])arg).GetLength(1);//列数量
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (((object[,])arg)[i, j] is not ExcelEmpty) { str.Append(((object[,])arg)[i, j] + symbol); }
}
}
}
else
{
if (arg is not ExcelEmpty) { str.Append(arg + symbol); }//ExcelEmpty
}
}
return sthReplace(symbol, str.ToString());//string.Join(symbol, strArray);
}
[ExcelFunction(ExplicitRegistration = true, Category = "替换多余符号", Description = "替换字符串中多余符号")]
public static string sthReplace(
[ExcelArgument(Name = "符号", Description = "多余符号")] string symbol,
[ExcelArgument(Name = "数据", Description = "输入需要替换多余符号的字符串")] string str)
{
str = Regex.Replace(str, symbol + "+", symbol);//替换重复项
str = Regex.Replace(str, "^" + symbol + "+|" + symbol + "+$", "");//替换首尾
return str;
}
[ExcelFunction(ExplicitRegistration = true, Category = "分解", Description = "返回分解后第n个数据")]
public static string sthFenJie(
[ExcelArgument(Name = "数据", Description = "输入字符串")] string str,
[ExcelArgument(Name = "符号", Description = "分割符号")] string symbol,
[ExcelArgument(Name = "索引", Description = "返回分解后第n个数据")] int number)
{
return str.Split(symbol)[number - 1];
}
}
}