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
18.41k stars 1.72k forks source link

#1076 Add MoveSheet and SwapSheets functions to support changing sheet order in the workbook #1996

Closed Zncl2222 closed 1 month ago

Zncl2222 commented 2 months ago

PR Details

Implement MoveSheet and SwapSheets function to change the order of sheet in the workbook.

Description

Both MoveSheet and SwapSheets functions use the index as their input arguments instead of the SheetName. This is because SheetName is allowed to be duplicated within a workbook. To avoid potential confusion or conflicts caused by duplicate sheet names, the implementation relies on the sheet index.

Here is an example code snippet to reorder the sheet

package main

import (
    "fmt"
    "strconv"

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

func main() {
    f := excelize.NewFile()
    defer func() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    for i := 2; i < 10; i++ {
        f.NewSheet("Sheet" + strconv.Itoa(i))
       }
    if err := f.SaveAs("BeforeOrder.xlsx"); err != nil {
        fmt.Println(err)
    }
    if err := f.MoveSheet(7, 0); err != nil {
        fmt.Println(err)
    }
    if err := f.SaveAs("AfterOrder.xlsx"); err != nil {
        fmt.Println(err)
    }
}

Before Order

image

After Order

image

Related Issue

1076

Motivation and Context

Allow users to use this package more flexibly. Additional this can resolve the problem that SetSheetName can't update the sheet name in the formula or reference associated with the cell.

How Has This Been Tested

Types of changes

Checklist

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.32%. Comparing base (41c7dd3) to head (53bc715). Report is 2 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1996 +/- ## ======================================= Coverage 99.32% 99.32% ======================================= Files 32 32 Lines 25332 25358 +26 ======================================= + Hits 25160 25186 +26 Misses 92 92 Partials 80 80 ``` | [Flag](https://app.codecov.io/gh/qax-os/excelize/pull/1996/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=qax-os) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/qax-os/excelize/pull/1996/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=qax-os) | `99.32% <100.00%> (+<0.01%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=qax-os#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.