qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
17.64k stars 1.69k forks source link

how to get a cell coordinate,there is GetRows GetColumns but no coordinate #1854

Closed answer91 closed 3 months ago

answer91 commented 3 months ago

Description

Steps to reproduce the issue: 1. 2. 3.

Describe the results you received:

Describe the results you expected:

Output of go version:

(paste your output here)

Excelize version or commit ID:

(paste here)

Environment details (OS, Microsoft Excel™ version, physical, etc.):

xuri commented 3 months ago

Thanks for your issue. As the documentation says, the Excelize library provides a set of utility functions that allow you to convert between cell names and coordinates, you can also use these functions when iterating rows or column cells. For example, convert column/row index to cell name when iterating rows:

package main

import (
    "fmt"

    "github.com/xuri/excelize/v2"
)

func main() {
    f := excelize.NewFile()
    defer func() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    if err := f.SetSheetRow("Sheet1", "A1", &[]interface{}{1, 2}); err != nil {
        fmt.Println(err)
        return
    }
    if err := f.SetSheetRow("Sheet1", "A2", &[]interface{}{3, 4}); err != nil {
        fmt.Println(err)
        return
    }
    rows, err := f.GetRows("Sheet1")
    if err != nil {
        fmt.Println(err)
        return
    }
    for rowIdx, row := range rows {
        for colIdx, cell := range row {
            // Convert column/row index to cell name
            cellName, err := excelize.CoordinatesToCellName(colIdx+1, rowIdx+1)
            if err != nil {
                fmt.Println(err)
                return
            }
            fmt.Printf("value of cell %s is %s\r\n", cellName, cell)
        }
    }
}

I've closed this issue. If you have any questions, please let me know, and you can reopen this anytime.