Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also consider cellname as y #11

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewXLSXReader(reader io.Reader, opts *trdsql.ReadOpts) (trdsql.Reader, erro
}
isFilter = true
cellX--
cellY--
}
Comment on lines 46 to 50
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The decrement of cellY has been removed in the latest commit, which suggests that the previous logic was incorrect. However, the increment of cellY by 1 is not present in the provided code. If the intention was to increment cellY, this change is missing and should be added to correctly reflect the pull request summary. If the increment is happening elsewhere in the code not shown in the diff, ensure that it is correctly implemented and tested.


rows, err := f.GetRows(sheet)
Expand All @@ -54,10 +55,7 @@ func NewXLSXReader(reader io.Reader, opts *trdsql.ReadOpts) (trdsql.Reader, erro
}

r := XLSXReader{}
skip := 0
if cellY > 0 {
skip = cellY - 1
}
skip := cellY
if opts.InSkip > 0 {
skip = opts.InSkip
}
Expand Down Expand Up @@ -87,15 +85,15 @@ func NewXLSXReader(reader io.Reader, opts *trdsql.ReadOpts) (trdsql.Reader, erro
}
}

r.names, r.types = nameType(rows[header], cellX, columnNum, opts.InHeader)
r.names, r.types = nameType(rows[header], cellX, cellY, columnNum, opts.InHeader)
rowNum := len(rows) - skip
body := make([][]any, 0, rowNum)
validColumns := make([]bool, columnNum)
for i := 0; i < len(r.names); i++ {
if r.names[i] != "" {
validColumns[i] = true
} else {
name, err := cellName(cellX + i)
name, err := cellName(cellX+i, cellY)
if err != nil {
return nil, err
}
Comment on lines +88 to 99
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop on lines 92-99 is using cellX+i and cellY to generate cell names for columns without names. However, there is a potential off-by-one error because cellX is decremented earlier in the code (lines 48-49), which is not reflected in the loop. This could lead to incorrect cell names if cellX is not zero. Ensure that the logic correctly calculates the cell name based on the actual X and Y coordinates.

Expand Down Expand Up @@ -134,8 +132,8 @@ func NewXLSXReader(reader io.Reader, opts *trdsql.ReadOpts) (trdsql.Reader, erro
return r, nil
}

func cellName(i int) (string, error) {
cn, err := excelize.CoordinatesToCellName(i+1, 1)
func cellName(x int, y int) (string, error) {
cn, err := excelize.CoordinatesToCellName(x+1, y+1)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -195,15 +193,15 @@ func parseExtend(ext string) (string, string) {
}
}

func nameType(row []string, cellX int, columnNum int, header bool) ([]string, []string) {
func nameType(row []string, cellX int, cellY int, columnNum int, header bool) ([]string, []string) {
nameMap := make(map[string]bool)
names := make([]string, columnNum)
types := make([]string, columnNum)
c := 0
for i := cellX; i < cellX+columnNum; i++ {
if header && len(row) > i && row[i] != "" {
if _, ok := nameMap[row[i]]; ok {
name, err := cellName(cellX + i)
name, err := cellName(cellX+i, cellY)
if err != nil {
names[c] = row[i] + "_" + fmt.Sprint(i)
} else {
Comment on lines +196 to 207
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nameType function now includes cellY as a parameter, which is consistent with the pull request summary. However, there is a potential issue with the error handling in the loop on lines 204-206. If an error occurs when calling cellName, the code falls back to concatenating the row name with the index i. This fallback does not seem to handle the error properly, as it does not log or return the error. It is important to handle errors appropriately to avoid silent failures and ensure that the caller is aware of any issues.

Expand Down