Skip to content

Commit

Permalink
Add 3 new functions: set_cell_int, set_cell_rich_text and set_cell_str
Browse files Browse the repository at this point in the history
- Update unit tests and docs for the function
- Setup security policy and code scanning
  • Loading branch information
xuri committed Dec 23, 2024
1 parent dc21761 commit 1ac3c8f
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Security Policy

## Supported Versions

We will dive into any security-related issue as long as your Excelize version is still supported by us. When reporting an issue, include as much information as possible, but no need to fill fancy forms or answer tedious questions. Just tell us what you found, how to reproduce it, and any concerns you have about it. We will respond as soon as possible and follow up with any missing information.

## Reporting a Vulnerability

Please e-mail us directly at `[email protected]` or use the security issue template on GitHub. In general, public disclosure is made after the issue has been fully identified and a patch is ready to be released. A security issue gets the highest priority assigned and a reply regarding the vulnerability is given within a typical 24 hours. Thank you!
35 changes: 35 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: "CodeQL"

on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
- cron: '0 6 * * 3'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-24.04

strategy:
fail-fast: false
matrix:
language: ['go', 'python']

steps:
- name: Checkout repository
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}

- name: Autobuild
uses: github/codeql-action/autobuild@v3

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
195 changes: 195 additions & 0 deletions excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,201 @@ def set_cell_hyperlink(
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_cell_int(
self,
sheet: str,
cell: str,
value: int,
) -> Optional[Exception]:
"""
Set int type value of a cell by given worksheet name, cell reference and
cell value.
Args:
sheet (str): The worksheet name
cell (str): The cell reference
value (int): The cell value
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
"""
lib.SetCellInt.restype = c_char_p
err = lib.SetCellInt(
self.file_index,
sheet.encode(ENCODE),
cell.encode(ENCODE),
value,
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_cell_rich_text(
self,
sheet: str,
cell: str,
runs: List[RichTextRun],
) -> Optional[Exception]:
"""
Set cell with rich text by given worksheet name, cell reference and rich
text runs.
Args:
sheet (str): The worksheet name
cell (str): The cell reference
runs (List[RichTextRun]): The rich text runs
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
Example:
For example, set rich text on the A1 cell of the worksheet named
Sheet1:
.. code-block:: python
f = excelize.new_file()
err = f.set_cell_rich_text(
"Sheet1",
"A1",
[
excelize.RichTextRun(
text="bold",
font=excelize.Font(
bold=True,
color="2354e8",
family="Times New Roman",
),
),
excelize.RichTextRun(
text=" and ",
font=excelize.Font(
family="Times New Roman",
),
),
excelize.RichTextRun(
text="italic ",
font=excelize.Font(
bold=True,
color="e83723",
italic=True,
family="Times New Roman",
),
),
excelize.RichTextRun(
text="text with color and font-family,",
font=excelize.Font(
bold=True,
color="2354e8",
family="Times New Roman",
),
),
excelize.RichTextRun(
text="\r\nlarge text with ",
font=excelize.Font(
size=14,
color="ad23e8",
),
),
excelize.RichTextRun(
text="strike",
font=excelize.Font(
color="e89923",
strike=True,
),
),
excelize.RichTextRun(
text=" superscript",
font=excelize.Font(
color="dbc21f",
vert_align="superscript",
),
),
excelize.RichTextRun(
text=" and ",
font=excelize.Font(
size=14,
color="ad23e8",
vert_align="baseline",
),
),
excelize.RichTextRun(
text="underline",
font=excelize.Font(
color="23e833",
vert_align="single",
),
),
excelize.RichTextRun(
text=" subscript.",
font=excelize.Font(
color="017505",
vert_align="subscript",
),
),
],
)
if err:
print(err)
style, err = f.new_style(
excelize.Style(
alignment=excelize.Alignment(wrap_text=True),
)
)
if err:
print(err)
err = f.set_cell_style("Sheet1", "A1", "A1", style)
if err:
print(err)
err = f.save_as("Book1.xlsx")
if err:
print(err)
err = f.close()
if err:
print(err)
"""
lib.SetCellRichText.restype = c_char_p
vals = (types_go._RichTextRun * len(runs))()
for i, value in enumerate(runs):
vals[i] = py_value_to_c(value, types_go._RichTextRun())
err = lib.SetCellRichText(
self.file_index,
sheet.encode(ENCODE),
cell.encode(ENCODE),
byref(vals),
len(vals),
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_cell_str(
self,
sheet: str,
cell: str,
value: str,
) -> Optional[Exception]:
"""
Set string type value of a cell. Total number of characters that a cell
can contain 32767 characters.
Args:
sheet (str): The worksheet name
cell (str): The cell reference
value (str): The cell value
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
"""
lib.SetCellStr.restype = c_char_p
err = lib.SetCellStr(
self.file_index,
sheet.encode(ENCODE),
cell.encode(ENCODE),
value.encode(ENCODE),
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_cell_style(
self,
sheet: str,
Expand Down
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,59 @@ func SetCellHyperLink(idx int, sheet, cell, link, linkType *C.char, opts *C.stru
return C.CString(errNil)
}

// SetCellInt provides a function to set int type value of a cell by given
// worksheet name, cell reference and cell value.
//
//export SetCellInt
func SetCellInt(idx int, sheet, cell *C.char, value int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).SetCellInt(C.GoString(sheet), C.GoString(cell), value); err != nil {
return C.CString(err.Error())
}
return C.CString(errNil)
}

// SetCellRichText provides a function to set cell with rich text by given
// worksheet name, cell reference and rich text runs.
//
//export SetCellRichText
func SetCellRichText(idx int, sheet, cell *C.char, runs *C.struct_RichTextRun, length int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString("")
}
textRuns := make([]excelize.RichTextRun, length)
for i, val := range unsafe.Slice(runs, length) {
goVal, err := cValueToGo(reflect.ValueOf(val), reflect.TypeOf(excelize.RichTextRun{}))
if err != nil {
return C.CString(err.Error())
}
textRuns[i] = goVal.Elem().Interface().(excelize.RichTextRun)
}
if err := f.(*excelize.File).SetCellRichText(C.GoString(sheet), C.GoString(cell), textRuns); err != nil {
return C.CString(err.Error())
}
return C.CString(errNil)
}

// SetCellStr provides a function to set string type value of a cell. Total
// number of characters that a cell can contain 32767 characters.
//
//export SetCellStr
func SetCellStr(idx int, sheet, cell, value *C.char) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).SetCellStr(C.GoString(sheet), C.GoString(cell), C.GoString(value)); err != nil {
return C.CString(err.Error())
}
return C.CString(errNil)
}

// SetCellStyle provides a function to add style attribute for cells by given
// worksheet name, range reference and style ID. This function is concurrency
// safe. Note that diagonalDown and diagonalUp type border should be use same
Expand Down
Loading

0 comments on commit 1ac3c8f

Please sign in to comment.