Skip to content

Commit b01d492

Browse files
committed
Add 2 new functions: SetCellFloat and SetCellRichText
- Update unit test
1 parent 634a922 commit b01d492

File tree

2 files changed

+310
-5
lines changed

2 files changed

+310
-5
lines changed

Excelize.Tests/UnitTest.cs

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,100 @@ public void TestCellHyperlink()
599599
Assert.Empty(f.Close());
600600
}
601601

602+
[Fact]
603+
public void TestCellRichText()
604+
{
605+
File f = Excelize.NewFile();
606+
RichTextRun[] expected = new RichTextRun[]
607+
{
608+
new RichTextRun
609+
{
610+
Text = "bold",
611+
Font = new Font
612+
{
613+
Bold = true,
614+
Color = "2354E8",
615+
Family = "Times New Roman",
616+
},
617+
},
618+
new RichTextRun
619+
{
620+
Text = " and ",
621+
Font = new Font { Family = "Times New Roman" },
622+
},
623+
new RichTextRun
624+
{
625+
Text = "italic ",
626+
Font = new Font
627+
{
628+
Bold = true,
629+
Color = "E83723",
630+
Italic = true,
631+
Family = "Times New Roman",
632+
},
633+
},
634+
new RichTextRun
635+
{
636+
Text = "text with color and font-family,",
637+
Font = new Font
638+
{
639+
Bold = true,
640+
Color = "2354E8",
641+
Family = "Times New Roman",
642+
},
643+
},
644+
new RichTextRun
645+
{
646+
Text = "\r\nlarge text with ",
647+
Font = new Font { Size = 14, Color = "AD23E8" },
648+
},
649+
new RichTextRun
650+
{
651+
Text = "strike",
652+
Font = new Font { Color = "E89923", Strike = true },
653+
},
654+
new RichTextRun
655+
{
656+
Text = " superscript",
657+
Font = new Font { Color = "DBC21F", VertAlign = "superscript" },
658+
},
659+
new RichTextRun
660+
{
661+
Text = " and ",
662+
Font = new Font
663+
{
664+
Size = 14,
665+
Color = "AD23E8",
666+
VertAlign = "baseline",
667+
},
668+
},
669+
new RichTextRun
670+
{
671+
Text = "underline",
672+
Font = new Font { Color = "23E833", Underline = "single" },
673+
},
674+
new RichTextRun
675+
{
676+
Text = " subscript.",
677+
Font = new Font { Color = "017505", VertAlign = "subscript" },
678+
},
679+
};
680+
Assert.Null(
681+
Record.Exception(() =>
682+
{
683+
f.SetCellRichText("Sheet1", "A1", expected);
684+
int style = f.NewStyle(new Style { Alignment = new Alignment { WrapText = true } });
685+
f.SetCellStyle("Sheet1", "A1", "A1", style);
686+
})
687+
);
688+
RuntimeError err = Assert.Throws<RuntimeError>(() =>
689+
f.SetCellRichText("SheetN", "A1", new RichTextRun[] { })
690+
);
691+
Assert.Equal("sheet SheetN does not exist", err.Message);
692+
Assert.Null(Record.Exception(() => f.SaveAs("TestCellRichText.xlsx")));
693+
Assert.Empty(f.Close());
694+
}
695+
602696
[Fact]
603697
public void TestCoordinates()
604698
{
@@ -1130,16 +1224,12 @@ public void TestStyle()
11301224
Assert.Null(
11311225
Record.Exception(() =>
11321226
{
1227+
f.SetCellFloat("Sheet1", "A14", 1.325, 2, 32);
11331228
Assert.Equal("100", f.CalcCellValue("Sheet1", "A4"));
11341229
f.SetActiveSheet(f.NewSheet("Sheet2"));
11351230
Assert.Equal(1, f.GetActiveSheetIndex());
11361231
f.MoveSheet("Sheet2", "Sheet1");
11371232
Assert.Equal(0, f.GetActiveSheetIndex());
1138-
})
1139-
);
1140-
Assert.Null(
1141-
Record.Exception(() =>
1142-
{
11431233
Assert.Equal("100", f.GetCellValue("Sheet1", "A4"));
11441234
})
11451235
);
@@ -1159,6 +1249,7 @@ public void TestStyle()
11591249
new() { "TRUE" },
11601250
new() { "FALSE" },
11611251
new() { "default" },
1252+
new() { "1.33" },
11621253
},
11631254
f.GetRows("Sheet1")
11641255
);
@@ -1245,6 +1336,8 @@ public void TestStyle()
12451336
Assert.Equal(expected, err.Message);
12461337
err = Assert.Throws<RuntimeError>(() => f.SetCellDefault("Sheet1", "A13", "default"));
12471338
Assert.Equal(expected, err.Message);
1339+
err = Assert.Throws<RuntimeError>(() => f.SetCellFloat("Sheet1", "A1", 1.325, 2, 32));
1340+
Assert.Equal(expected, err.Message);
12481341
err = Assert.Throws<RuntimeError>(() => f.SetCellFormula("Sheet1", "A1", "=A2"));
12491342
Assert.Equal(expected, err.Message);
12501343
err = Assert.Throws<RuntimeError>(() => f.SetCellHyperLink("Sheet1", "A1", "", "External"));
@@ -1255,6 +1348,10 @@ public void TestStyle()
12551348
Assert.Equal(expected, err.Message);
12561349
err = Assert.Throws<RuntimeError>(() => f.SetCellInt("Sheet1", "A1", 100));
12571350
Assert.Equal(expected, err.Message);
1351+
err = Assert.Throws<RuntimeError>(() =>
1352+
f.SetCellRichText("Sheet1", "A1", new RichTextRun[] { })
1353+
);
1354+
Assert.Equal(expected, err.Message);
12581355
err = Assert.Throws<RuntimeError>(() => f.SetCellValue("Sheet1", "A1", 100));
12591356
Assert.Equal(expected, err.Message);
12601357
err = Assert.Throws<RuntimeError>(() => f.SetColOutlineLevel("Sheet1", "A", 1));

Excelize/Excelize.cs

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,16 @@ internal static extern IntPtr SetCellDefault(
401401
string value
402402
);
403403

404+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
405+
internal static extern IntPtr SetCellFloat(
406+
long fileIdx,
407+
string sheet,
408+
string cell,
409+
double value,
410+
long precision,
411+
long bitSize
412+
);
413+
404414
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
405415
internal static extern IntPtr SetCellFormula(
406416
long fileIdx,
@@ -428,6 +438,15 @@ internal static extern IntPtr SetCellInt(
428438
long value
429439
);
430440

441+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
442+
internal static extern IntPtr SetCellRichText(
443+
long fileIdx,
444+
[MarshalAs(UnmanagedType.LPUTF8Str)] string sheet,
445+
[MarshalAs(UnmanagedType.LPUTF8Str)] string cell,
446+
[In, MarshalAs(UnmanagedType.LPArray)] TypesC.RichTextRun[] options,
447+
long length
448+
);
449+
431450
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
432451
internal static extern IntPtr SetCellStr(
433452
long fileIdx,
@@ -3314,6 +3333,35 @@ public void SetCellDefault(string sheet, string cell, string value)
33143333
throw new RuntimeError(err);
33153334
}
33163335

3336+
/// <summary>
3337+
/// SetCellFloat sets a floating point value into a cell. The precision
3338+
/// parameter specifies how many places after the decimal will be shown
3339+
/// while -1 is a special value that will use as many decimal places as
3340+
/// necessary to represent the number. bitSize is 32 or 64 depending on
3341+
/// if a float32 or float64 was originally used for the value.
3342+
/// </summary>
3343+
/// <param name="sheet">The worksheet name</param>
3344+
/// <param name="cell">The cell reference</param>
3345+
/// <param name="value">The cell value</param>
3346+
/// <param name="precision">The cell value precision</param>
3347+
/// <param name="bitSize">The cell value bit size</param>
3348+
/// <exception cref="RuntimeError">Return None if no error occurred,
3349+
/// otherwise raise a RuntimeError with the message.</exception>
3350+
public void SetCellFloat(
3351+
string sheet,
3352+
string cell,
3353+
double value,
3354+
int precision,
3355+
int bitSize
3356+
)
3357+
{
3358+
string err = Marshal.PtrToStringUTF8(
3359+
Lib.SetCellFloat(FileIdx, sheet, cell, value, precision, bitSize)
3360+
);
3361+
if (!string.IsNullOrEmpty(err))
3362+
throw new RuntimeError(err);
3363+
}
3364+
33173365
/// <summary>
33183366
/// SetCellFormula provides a function to set formula on the cell is
33193367
/// taken according to the given worksheet name and cell formula
@@ -3582,6 +3630,166 @@ public void SetCellInt(string sheet, string cell, long value)
35823630
throw new RuntimeError(err);
35833631
}
35843632

3633+
/// <summary>
3634+
/// SetCellRichText provides a function to set cell with rich text by
3635+
/// given worksheet name, cell reference and rich text runs.
3636+
/// <example>
3637+
/// For example, set rich text on the A1 cell of the worksheet named
3638+
/// Sheet1:
3639+
/// <code>
3640+
/// using ExcelizeCs;
3641+
///
3642+
/// class Program
3643+
/// {
3644+
/// static void Main()
3645+
/// {
3646+
/// ExcelizeCs.File f = Excelize.NewFile();
3647+
/// try
3648+
/// {
3649+
/// f.SetCellRichText(
3650+
/// "Sheet1",
3651+
/// "A1",
3652+
/// new RichTextRun[]
3653+
/// {
3654+
/// new RichTextRun
3655+
/// {
3656+
/// Text = "bold",
3657+
/// Font = new Font
3658+
/// {
3659+
/// Bold = true,
3660+
/// Color = "2354E8",
3661+
/// Family = "Times New Roman",
3662+
/// },
3663+
/// },
3664+
/// new RichTextRun
3665+
/// {
3666+
/// Text = " and ",
3667+
/// Font = new Font
3668+
/// {
3669+
/// Family = "Times New Roman",
3670+
/// },
3671+
/// },
3672+
/// new RichTextRun
3673+
/// {
3674+
/// Text = "italic ",
3675+
/// Font = new Font
3676+
/// {
3677+
/// Bold = true,
3678+
/// Color = "E83723",
3679+
/// Italic = true,
3680+
/// Family = "Times New Roman",
3681+
/// },
3682+
/// },
3683+
/// new RichTextRun
3684+
/// {
3685+
/// Text = "text with color and font-family,",
3686+
/// Font = new Font
3687+
/// {
3688+
/// Bold = true,
3689+
/// Color = "2354E8",
3690+
/// Family = "Times New Roman",
3691+
/// },
3692+
/// },
3693+
/// new RichTextRun
3694+
/// {
3695+
/// Text = "\r\nlarge text with ",
3696+
/// Font = new Font
3697+
/// {
3698+
/// Size = 14,
3699+
/// Color = "AD23E8",
3700+
/// },
3701+
/// },
3702+
/// new RichTextRun
3703+
/// {
3704+
/// Text = "strike",
3705+
/// Font = new Font
3706+
/// {
3707+
/// Color = "E89923",
3708+
/// Strike = true,
3709+
/// },
3710+
/// },
3711+
/// new RichTextRun
3712+
/// {
3713+
/// Text = " superscript",
3714+
/// Font = new Font
3715+
/// {
3716+
/// Color = "DBC21F",
3717+
/// VertAlign = "superscript",
3718+
/// },
3719+
/// },
3720+
/// new RichTextRun
3721+
/// {
3722+
/// Text = " and ",
3723+
/// Font = new Font
3724+
/// {
3725+
/// Size = 14,
3726+
/// Color = "AD23E8",
3727+
/// VertAlign = "baseline",
3728+
/// },
3729+
/// },
3730+
/// new RichTextRun
3731+
/// {
3732+
/// Text = "underline",
3733+
/// Font = new Font
3734+
/// {
3735+
/// Color = "23E833",
3736+
/// Underline = "single",
3737+
/// },
3738+
/// },
3739+
/// new RichTextRun
3740+
/// {
3741+
/// Text = " subscript.",
3742+
/// Font = new Font
3743+
/// {
3744+
/// Color = "017505",
3745+
/// VertAlign = "subscript",
3746+
/// },
3747+
/// },
3748+
/// }
3749+
/// );
3750+
/// int style = f.NewStyle(
3751+
/// new Style
3752+
/// {
3753+
/// Alignment = new Alignment { WrapText = true },
3754+
/// }
3755+
/// );
3756+
/// f.SetCellStyle("Sheet1", "A1", "A1", style);
3757+
/// f.SaveAs("Book1.xlsx");
3758+
/// }
3759+
/// catch (RuntimeError err)
3760+
/// {
3761+
/// Console.WriteLine(err.Message);
3762+
/// }
3763+
/// finally
3764+
/// {
3765+
/// string err = f.Close();
3766+
/// if (!string.IsNullOrEmpty(err))
3767+
/// Console.WriteLine(err);
3768+
/// }
3769+
/// }
3770+
/// }
3771+
/// </code>
3772+
/// </example>
3773+
/// </summary>
3774+
/// <param name="sheet">The worksheet name</param>
3775+
/// <param name="cell">The cell reference</param>
3776+
/// <param name="runs">The rich text runs</param>
3777+
/// <exception cref="RuntimeError">Return None if no error occurred,
3778+
/// otherwise raise a RuntimeError with the message.</exception>
3779+
public void SetCellRichText(string sheet, string cell, RichTextRun[] runs)
3780+
{
3781+
TypesC.RichTextRun[] arr = new TypesC.RichTextRun[runs.Length];
3782+
for (int i = 0; i < runs.Length; i++)
3783+
{
3784+
arr[i] = (TypesC.RichTextRun)Lib.CsToC(runs[i], new TypesC.RichTextRun());
3785+
}
3786+
string err = Marshal.PtrToStringUTF8(
3787+
Lib.SetCellRichText(FileIdx, sheet, cell, arr, arr.Length)
3788+
);
3789+
if (!string.IsNullOrEmpty(err))
3790+
throw new RuntimeError(err);
3791+
}
3792+
35853793
/// <summary>
35863794
/// SetCellStr provides a function to set string type value of a cell.
35873795
/// Total number of characters that a cell can contain 32767 characters.

0 commit comments

Comments
 (0)