Skip to content

Commit 634a922

Browse files
committed
Add 2 new functions: SetCellHyperLink and SetCellStr
- Update unit test
1 parent 6c4b158 commit 634a922

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

Excelize.Tests/UnitTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,42 @@ public void TestCalcProps()
563563
Assert.Equal(opts, f.GetCalcProps());
564564
}
565565

566+
[Fact]
567+
public void TestCellHyperlink()
568+
{
569+
File f = Excelize.NewFile();
570+
string display = "https://github.com/xuri/excelize";
571+
Assert.Null(
572+
Record.Exception(() =>
573+
{
574+
f.SetCellStr("Sheet1", "A3", "HyperLink");
575+
f.SetCellHyperLink(
576+
"Sheet1",
577+
"A3",
578+
display,
579+
"External",
580+
new HyperlinkOpts { Display = display, Tooltip = "Excelize on GitHub" }
581+
);
582+
// Set underline and font color style for the cell.
583+
int style = f.NewStyle(
584+
new Style
585+
{
586+
Font = new Font { Color = "1265BE", Underline = "single" },
587+
}
588+
);
589+
f.SetCellStyle("Sheet1", "A3", "A3", style);
590+
})
591+
);
592+
RuntimeError err = Assert.Throws<RuntimeError>(() => f.SetCellStr("SheetN", "A3", ""));
593+
Assert.Equal("sheet SheetN does not exist", err.Message);
594+
err = Assert.Throws<RuntimeError>(() =>
595+
f.SetCellHyperLink("SheetN", "A3", display, "External")
596+
);
597+
Assert.Equal("sheet SheetN does not exist", err.Message);
598+
Assert.Null(Record.Exception(() => f.SaveAs("TestCellHyperLink.xlsx")));
599+
Assert.Empty(f.Close());
600+
}
601+
566602
[Fact]
567603
public void TestCoordinates()
568604
{
@@ -1211,6 +1247,10 @@ public void TestStyle()
12111247
Assert.Equal(expected, err.Message);
12121248
err = Assert.Throws<RuntimeError>(() => f.SetCellFormula("Sheet1", "A1", "=A2"));
12131249
Assert.Equal(expected, err.Message);
1250+
err = Assert.Throws<RuntimeError>(() => f.SetCellHyperLink("Sheet1", "A1", "", "External"));
1251+
Assert.Equal(expected, err.Message);
1252+
err = Assert.Throws<RuntimeError>(() => f.SetCellStr("Sheet1", "A1", ""));
1253+
Assert.Equal(expected, err.Message);
12141254
err = Assert.Throws<RuntimeError>(() => f.SetCellStyle("Sheet1", "A1", "B2", styleId));
12151255
Assert.Equal(expected, err.Message);
12161256
err = Assert.Throws<RuntimeError>(() => f.SetCellInt("Sheet1", "A1", 100));

Excelize/Excelize.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,16 @@ internal static extern IntPtr SetCellFormula(
410410
ref TypesC.FormulaOpts opts
411411
);
412412

413+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
414+
internal static extern IntPtr SetCellHyperLink(
415+
long fileIdx,
416+
string sheet,
417+
string cell,
418+
string link,
419+
string linkType,
420+
ref TypesC.HyperlinkOpts opts
421+
);
422+
413423
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
414424
internal static extern IntPtr SetCellInt(
415425
long fileIdx,
@@ -418,6 +428,14 @@ internal static extern IntPtr SetCellInt(
418428
long value
419429
);
420430

431+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
432+
internal static extern IntPtr SetCellStr(
433+
long fileIdx,
434+
[MarshalAs(UnmanagedType.LPUTF8Str)] string sheet,
435+
[MarshalAs(UnmanagedType.LPUTF8Str)] string cell,
436+
[MarshalAs(UnmanagedType.LPUTF8Str)] string value
437+
);
438+
421439
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
422440
internal static extern IntPtr SetCellStyle(
423441
long fileIdx,
@@ -3473,6 +3491,81 @@ public void SetCellFormula(
34733491
throw new RuntimeError(err);
34743492
}
34753493

3494+
/// <summary>
3495+
/// SetCellHyperLink provides a function to set cell hyperlink by given
3496+
/// worksheet name and link URL address. LinkType defines three types
3497+
/// of hyperlink <c>External</c> for website or <c>Location</c> for
3498+
/// moving to one of cell in this workbook or <c>None</c> for remove
3499+
/// hyperlink. Maximum limit hyperlinks in a worksheet is 65530. This
3500+
/// function is only used to set the hyperlink of the cell and doesn't
3501+
/// affect the value of the cell. If you need to set the value of the
3502+
/// cell, please use the other functions such as <c>SetCellStyle</c> or
3503+
/// <c>SetSheetRow</c>.
3504+
/// <example>
3505+
/// The below is example for external link:
3506+
/// <code>
3507+
/// string display = "https://github.com/xuri/excelize";
3508+
/// string tooltip = "Excelize on GitHub";
3509+
/// try
3510+
/// {
3511+
/// f.SetCellHyperLink(
3512+
/// "Sheet1",
3513+
/// "A3",
3514+
/// display,
3515+
/// "External",
3516+
/// new HyperlinkOpts { Display = display, Tooltip = tooltip }
3517+
/// );
3518+
/// // Set underline and font color style for the cell.
3519+
/// int style = f.NewStyle(
3520+
/// new Style
3521+
/// {
3522+
/// Font = new Font { Color = "1265BE", Underline = "single" },
3523+
/// }
3524+
/// );
3525+
/// f.SetCellStyle("Sheet1", "A3", "A3", style);
3526+
/// }
3527+
/// catch (RuntimeError err)
3528+
/// {
3529+
/// Console.WriteLine(err.Message);
3530+
/// }
3531+
/// </code>
3532+
/// This is another example for <c>Location</c>:
3533+
/// <code>
3534+
/// try
3535+
/// {
3536+
/// f.SetCellHyperLink("Sheet1", "A3", "Sheet1!A40", "Location");
3537+
/// }
3538+
/// catch (RuntimeError err)
3539+
/// {
3540+
/// Console.WriteLine(err.Message);
3541+
/// }
3542+
/// </code>
3543+
/// </example>
3544+
/// </summary>
3545+
/// <param name="sheet">The worksheet name</param>
3546+
/// <param name="cell">The cell reference</param>
3547+
/// <param name="link">The hyperlink</param>
3548+
/// <param name="linkType">The hyperlink type</param>
3549+
/// <param name="options">The hyperlink options</param>
3550+
/// <exception cref="RuntimeError">Return None if no error occurred,
3551+
/// otherwise raise a RuntimeError with the message.</exception>
3552+
public void SetCellHyperLink(
3553+
string sheet,
3554+
string cell,
3555+
string link,
3556+
string linkType,
3557+
HyperlinkOpts? options = null
3558+
)
3559+
{
3560+
var opts = (TypesC.HyperlinkOpts)
3561+
Lib.CsToC(options ?? new HyperlinkOpts(), new TypesC.HyperlinkOpts());
3562+
string err = Marshal.PtrToStringUTF8(
3563+
Lib.SetCellHyperLink(FileIdx, sheet, cell, link, linkType, ref opts)
3564+
);
3565+
if (!string.IsNullOrEmpty(err))
3566+
throw new RuntimeError(err);
3567+
}
3568+
34763569
/// <summary>
34773570
/// Set int type value of a cell by given worksheet name, cell reference
34783571
/// and cell value.
@@ -3489,6 +3582,22 @@ public void SetCellInt(string sheet, string cell, long value)
34893582
throw new RuntimeError(err);
34903583
}
34913584

3585+
/// <summary>
3586+
/// SetCellStr provides a function to set string type value of a cell.
3587+
/// Total number of characters that a cell can contain 32767 characters.
3588+
/// </summary>
3589+
/// <param name="sheet">The worksheet name</param>
3590+
/// <param name="cell">The cell reference</param>
3591+
/// <param name="value">The cell value</param>
3592+
/// <exception cref="RuntimeError">Return None if no error occurred,
3593+
/// otherwise raise a RuntimeError with the message.</exception>
3594+
public void SetCellStr(string sheet, string cell, string value)
3595+
{
3596+
string err = Marshal.PtrToStringUTF8(Lib.SetCellStr(FileIdx, sheet, cell, value));
3597+
if (!string.IsNullOrEmpty(err))
3598+
throw new RuntimeError(err);
3599+
}
3600+
34923601
/// <summary>
34933602
/// Set int type value of a cell by given worksheet name, cell reference
34943603
/// and cell value.

Excelize/TypesC.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ public unsafe struct HeaderFooterImageOptions
205205
public sbyte* Height;
206206
}
207207

208+
[StructLayout(LayoutKind.Sequential)]
209+
public unsafe struct HyperlinkOpts
210+
{
211+
public sbyte** Display;
212+
public sbyte** Tooltip;
213+
}
214+
208215
[StructLayout(LayoutKind.Sequential)]
209216
public unsafe struct Style
210217
{

Excelize/TypesCs.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,16 @@ public struct HeaderFooterOptions
389389
public string FirstFooter;
390390
}
391391

392+
/// <summary>
393+
/// HyperlinkOpts can be passed to SetCellHyperlink to set optional
394+
/// hyperlink attributes (e.g. display value)
395+
/// </summary>
396+
public struct HyperlinkOpts
397+
{
398+
public string? Display;
399+
public string? Tooltip;
400+
}
401+
392402
/// <summary>
393403
/// HeaderFooterImageOptions defines the settings for an image to be
394404
/// accessible from the worksheet header and footer options.

0 commit comments

Comments
 (0)