Skip to content

Commit f9ebbb9

Browse files
authored
Merge pull request #32 from dotnet-campus/t/lindexi/EmuPercentage
提供 EmuPercentage 支持传入百分号字符串
2 parents 181266a + a2f867c commit f9ebbb9

3 files changed

Lines changed: 80 additions & 4 deletions

File tree

src/dotnetCampus.OpenXmlUnitConverter/EmuPercentage.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace dotnetCampus.OpenXmlUnitConverter
1+
using System;
2+
using DocumentFormat.OpenXml;
3+
4+
namespace dotnetCampus.OpenXmlUnitConverter
25
{
36
/// <summary>
47
/// 用 Emu 表示的百分数
@@ -14,6 +17,54 @@ public EmuPercentage(double value)
1417
Value = value;
1518
}
1619

20+
/// <summary>
21+
/// 用 Emu 表示的百分数
22+
/// </summary>
23+
/// <param name="int32Value">支持传入带百分号的内容</param>
24+
public EmuPercentage(Int32Value int32Value) : this(int32Value.InnerText)
25+
{
26+
}
27+
28+
internal EmuPercentage(string? percentageText)
29+
{
30+
if (!string.IsNullOrEmpty(percentageText))
31+
{
32+
if (int.TryParse(percentageText, out var intValue))
33+
{
34+
Value = intValue;
35+
return;
36+
}
37+
else
38+
{
39+
// 如果是带了百分比的
40+
if (percentageText!.Length > 1 && percentageText.EndsWith("%"))
41+
{
42+
#if NETCOREAPP3_1_OR_GREATER
43+
var percentageSpan = percentageText.AsSpan().Slice(0, percentageText.Length - 1);
44+
if (double.TryParse(percentageSpan, out var doubleValue))
45+
#else
46+
var newPercentageText = percentageText.Substring(0, percentageText.Length - 1);
47+
if (double.TryParse(newPercentageText, out var doubleValue))
48+
#endif
49+
{
50+
// 根据 OpenXml 规则,这里和像素百分比转换是 1000 倍
51+
var pixelPercentageValue = doubleValue * 1000;
52+
53+
// 这里的百分比一定是 Pixel 的百分比级的,需要经过转换才能计算为 Emu 的百分比
54+
// 为了将 PixelPercentageValue 转换为 EmuPercentage 类型,需要经过换算逻辑
55+
var pixel = new Pixel(pixelPercentageValue);
56+
var emuPercentageValue = pixel.ToEmu();
57+
58+
Value = emuPercentageValue.Value;
59+
return;
60+
}
61+
}
62+
}
63+
}
64+
65+
throw new ArgumentException($"Can not convert PercentageText={percentageText} to {nameof(EmuPercentage)} value.");
66+
}
67+
1768
/// <summary>
1869
/// 用 Emu 表示的百分数
1970
/// </summary>

src/dotnetCampus.OpenXmlUnitConverter/Percentage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public Percentage(int value)
2424
/// 从一个 OpenXML 的数值转换为百分比
2525
/// </summary>
2626
/// [dotnet OpenXML 修复 Office 文档里面的百分比内容包含百分号](https://blog.lindexi.com/post/dotnet-OpenXML-%E4%BF%AE%E5%A4%8D-Office-%E6%96%87%E6%A1%A3%E9%87%8C%E9%9D%A2%E7%9A%84%E7%99%BE%E5%88%86%E6%AF%94%E5%86%85%E5%AE%B9%E5%8C%85%E5%90%AB%E7%99%BE%E5%88%86%E5%8F%B7.html )
27-
28-
public Percentage(Int32Value int32Value) : this(int32Value.InnerText)
27+
public Percentage(Int32Value int32Value) : this(int32Value.InnerText ?? string.Empty)
2928
{
3029
}
3130

@@ -62,7 +61,8 @@ internal Percentage(string percentageText)
6261
/// <summary>
6362
/// 百分比与 OpenXML 比例
6463
/// </summary>
65-
public const double Precision = 100000.0;
64+
/// 百分比与 OpenXML 比例的数值差值是 1000 倍,再乘以百分比的 100 倍
65+
public const double Precision = 1000 * 100.0;
6666

6767
/// <summary>
6868
/// 表示 0 的值
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using DocumentFormat.OpenXml;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using MSTest.Extensions.Contracts;
4+
5+
namespace dotnetCampus.OpenXmlUnitConverter.Tests;
6+
7+
[TestClass]
8+
public class EmuPercentageTest
9+
{
10+
[ContractTestCase]
11+
public void CreateEmuPercentage()
12+
{
13+
"传入带百分号的 Int32Value 值,可以成功转换为 EmuPercentage 对象".Test(() =>
14+
{
15+
var int32Value = new Int32Value
16+
{
17+
InnerText = "10%"
18+
};
19+
20+
var emuPercentage = new EmuPercentage(int32Value);
21+
var pixelPercentage = emuPercentage.ToPixelPercentage();
22+
Assert.AreEqual(10 * 1000, pixelPercentage.IntValue);
23+
});
24+
}
25+
}

0 commit comments

Comments
 (0)