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

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

Merged
merged 4 commits into from
Sep 30, 2024

Conversation

Zncl2222
Copy link
Contributor

@Zncl2222 Zncl2222 commented Sep 24, 2024

PR Details

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

  • MoveSheet: Moves the sheet to the new position using its current index and the target index. The other sheets will be shifted to the left or the right.

  • SwapSheets: Swaps the positions of two sheets in the workbook based on their indices.

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

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

…r in the workbook (qax-os#1076)

- Add MoveSheet and SwapSheets functions to change sheet order in the workbook
- Add unit tests for MoveSheet and SwapSheets functions
@Zncl2222 Zncl2222 force-pushed the feature/MoveSheet-and-SwapSheets branch from 239697b to 8f98bb3 Compare September 24, 2024 15:52
1. Add failing scenarios where sourceIdx is provided with invalid values
@Zncl2222 Zncl2222 changed the title Add MoveSheet and SwapSheets functions to support changing sheet orde… #1076 Add MoveSheet and SwapSheets functions to support changing sheet order in the workbook Sep 24, 2024
@xuri xuri added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Sep 27, 2024
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

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

Thanks for your pull request. The swap sheet can be implemented by move sheet, due to the principle of minimum availability, I suggest just introducing the MoveSheet function to keep the library core and easy. I made some changes based on your branch.

workbook.go Outdated Show resolved Hide resolved
Copy link

codecov bot commented Sep 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

Additional details and impacted files
@@           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 Coverage Δ
unittests 99.32% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@xuri xuri merged commit b23e5a2 into qax-os:master Sep 30, 2024
41 checks passed
@xuri xuri added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
Status: Features
Development

Successfully merging this pull request may close these issues.

2 participants