2023-01-02 11:47:31 +08:00
|
|
|
// Copyright 2016 - 2023 The excelize Authors. All rights reserved. Use of
|
2019-06-05 22:06:15 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
2022-02-17 00:09:11 +08:00
|
|
|
// Package excelize providing a set of functions that allow you to write to and
|
|
|
|
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
|
|
|
|
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
|
|
|
|
// Supports complex components by high compatibility, and provided streaming
|
|
|
|
// API for generating or reading data from a worksheet with huge amounts of
|
2023-01-02 11:47:31 +08:00
|
|
|
// data. This library needs Go version 1.16 or later.
|
2019-06-05 22:06:15 +08:00
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
package excelize
|
|
|
|
|
2022-05-02 12:30:18 +08:00
|
|
|
import "strings"
|
2021-08-13 01:32:44 +08:00
|
|
|
|
|
|
|
// Rect gets merged cell rectangle coordinates sequence.
|
|
|
|
func (mc *xlsxMergeCell) Rect() ([]int, error) {
|
|
|
|
var err error
|
|
|
|
if mc.rect == nil {
|
2022-10-28 00:31:55 +08:00
|
|
|
mergedCellsRef := mc.Ref
|
|
|
|
if !strings.Contains(mergedCellsRef, ":") {
|
|
|
|
mergedCellsRef += ":" + mergedCellsRef
|
|
|
|
}
|
|
|
|
mc.rect, err = rangeRefToCoordinates(mergedCellsRef)
|
2021-08-13 01:32:44 +08:00
|
|
|
}
|
|
|
|
return mc.rect, err
|
|
|
|
}
|
2019-12-14 19:57:37 +08:00
|
|
|
|
2022-09-18 00:07:15 +08:00
|
|
|
// MergeCell provides a function to merge cells by given range reference and
|
2021-08-09 22:22:43 +08:00
|
|
|
// sheet name. Merging cells only keeps the upper-left cell value, and
|
|
|
|
// discards the other values. For example create a merged cell of D3:E9 on
|
|
|
|
// Sheet1:
|
2019-12-14 19:57:37 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// err := f.MergeCell("Sheet1", "D3", "E9")
|
2019-12-14 19:57:37 +08:00
|
|
|
//
|
|
|
|
// If you create a merged cell that overlaps with another existing merged cell,
|
2022-09-18 00:07:15 +08:00
|
|
|
// those merged cells that already exist will be removed. The cell references
|
2021-08-13 01:32:44 +08:00
|
|
|
// tuple after merging in the following range will be: A1(x3,y1) D1(x2,y1)
|
|
|
|
// A8(x3,y4) D8(x2,y4)
|
2019-12-14 19:57:37 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// B1(x1,y1) D1(x2,y1)
|
|
|
|
// +------------------------+
|
|
|
|
// | |
|
2023-01-13 00:05:46 +08:00
|
|
|
// A4(x3,y3) | C4(x4,y3) |
|
2022-08-13 11:21:59 +08:00
|
|
|
// +------------------------+ |
|
|
|
|
// | | | |
|
|
|
|
// | |B5(x1,y2) | D5(x2,y2)|
|
|
|
|
// | +------------------------+
|
|
|
|
// | |
|
|
|
|
// |A8(x3,y4) C8(x4,y4)|
|
|
|
|
// +------------------------+
|
2022-01-09 00:20:42 +08:00
|
|
|
func (f *File) MergeCell(sheet, hCell, vCell string) error {
|
2022-09-28 00:04:17 +08:00
|
|
|
rect, err := rangeRefToCoordinates(hCell + ":" + vCell)
|
2019-12-14 19:57:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-18 00:07:15 +08:00
|
|
|
// Correct the range reference, such correct C1:B3 to B1:C3.
|
2021-08-13 01:32:44 +08:00
|
|
|
_ = sortCoordinates(rect)
|
2019-12-14 19:57:37 +08:00
|
|
|
|
2022-01-09 00:20:42 +08:00
|
|
|
hCell, _ = CoordinatesToCellName(rect[0], rect[1])
|
|
|
|
vCell, _ = CoordinatesToCellName(rect[2], rect[3])
|
2019-04-30 18:39:27 +08:00
|
|
|
|
2020-11-10 23:48:09 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-04-30 18:39:27 +08:00
|
|
|
if err != nil {
|
2019-12-14 19:57:37 +08:00
|
|
|
return err
|
2019-04-30 18:39:27 +08:00
|
|
|
}
|
2023-04-24 00:02:13 +08:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2022-01-09 00:20:42 +08:00
|
|
|
ref := hCell + ":" + vCell
|
2020-11-10 23:48:09 +08:00
|
|
|
if ws.MergeCells != nil {
|
2021-08-13 01:32:44 +08:00
|
|
|
ws.MergeCells.Cells = append(ws.MergeCells.Cells, &xlsxMergeCell{Ref: ref, rect: rect})
|
2019-12-14 19:57:37 +08:00
|
|
|
} else {
|
2021-08-13 01:32:44 +08:00
|
|
|
ws.MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: ref, rect: rect}}}
|
2019-04-30 18:39:27 +08:00
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
ws.MergeCells.Count = len(ws.MergeCells.Cells)
|
2022-05-02 12:30:18 +08:00
|
|
|
return err
|
2019-04-30 18:39:27 +08:00
|
|
|
}
|
|
|
|
|
2022-09-18 00:07:15 +08:00
|
|
|
// UnmergeCell provides a function to unmerge a given range reference.
|
2022-09-28 00:04:17 +08:00
|
|
|
// For example unmerge range reference D3:E9 on Sheet1:
|
2019-12-13 21:43:59 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// err := f.UnmergeCell("Sheet1", "D3", "E9")
|
2019-12-13 21:43:59 +08:00
|
|
|
//
|
2022-09-28 00:04:17 +08:00
|
|
|
// Attention: overlapped range will also be unmerged.
|
2022-09-18 00:07:15 +08:00
|
|
|
func (f *File) UnmergeCell(sheet, hCell, vCell string) error {
|
2020-11-10 23:48:09 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-12-13 21:43:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-24 00:02:13 +08:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2022-09-28 00:04:17 +08:00
|
|
|
rect1, err := rangeRefToCoordinates(hCell + ":" + vCell)
|
2019-12-13 21:43:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-18 00:07:15 +08:00
|
|
|
// Correct the range reference, such correct C1:B3 to B1:C3.
|
2019-12-29 16:02:31 +08:00
|
|
|
_ = sortCoordinates(rect1)
|
2019-12-13 21:43:59 +08:00
|
|
|
|
|
|
|
// return nil since no MergeCells in the sheet
|
2020-11-10 23:48:09 +08:00
|
|
|
if ws.MergeCells == nil {
|
2019-12-13 21:43:59 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-13 01:32:44 +08:00
|
|
|
if err = f.mergeOverlapCells(ws); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-13 21:43:59 +08:00
|
|
|
i := 0
|
2021-08-13 01:32:44 +08:00
|
|
|
for _, mergeCell := range ws.MergeCells.Cells {
|
|
|
|
if mergeCell == nil {
|
2019-12-14 19:57:37 +08:00
|
|
|
continue
|
|
|
|
}
|
2022-10-28 00:31:55 +08:00
|
|
|
mergedCellsRef := mergeCell.Ref
|
|
|
|
if !strings.Contains(mergedCellsRef, ":") {
|
|
|
|
mergedCellsRef += ":" + mergedCellsRef
|
|
|
|
}
|
|
|
|
rect2, _ := rangeRefToCoordinates(mergedCellsRef)
|
2019-12-14 19:57:37 +08:00
|
|
|
if isOverlap(rect1, rect2) {
|
2019-12-13 21:43:59 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-08-13 01:32:44 +08:00
|
|
|
ws.MergeCells.Cells[i] = mergeCell
|
2019-12-13 21:43:59 +08:00
|
|
|
i++
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
ws.MergeCells.Cells = ws.MergeCells.Cells[:i]
|
|
|
|
ws.MergeCells.Count = len(ws.MergeCells.Cells)
|
2021-02-10 00:04:13 +08:00
|
|
|
if ws.MergeCells.Count == 0 {
|
|
|
|
ws.MergeCells = nil
|
|
|
|
}
|
2019-12-13 21:43:59 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-14 19:57:37 +08:00
|
|
|
// GetMergeCells provides a function to get all merged cells from a worksheet
|
|
|
|
// currently.
|
|
|
|
func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) {
|
|
|
|
var mergeCells []MergeCell
|
2020-11-10 23:48:09 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-12-14 19:57:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return mergeCells, err
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
if ws.MergeCells != nil {
|
2021-08-13 01:32:44 +08:00
|
|
|
if err = f.mergeOverlapCells(ws); err != nil {
|
|
|
|
return mergeCells, err
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
mergeCells = make([]MergeCell, 0, len(ws.MergeCells.Cells))
|
|
|
|
for i := range ws.MergeCells.Cells {
|
|
|
|
ref := ws.MergeCells.Cells[i].Ref
|
2022-09-18 00:07:15 +08:00
|
|
|
cell := strings.Split(ref, ":")[0]
|
|
|
|
val, _ := f.GetCellValue(sheet, cell)
|
2019-12-14 19:57:37 +08:00
|
|
|
mergeCells = append(mergeCells, []string{ref, val})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mergeCells, err
|
|
|
|
}
|
|
|
|
|
2021-08-13 01:32:44 +08:00
|
|
|
// overlapRange calculate overlap range of merged cells, and returns max
|
|
|
|
// column and rows of the range.
|
|
|
|
func overlapRange(ws *xlsxWorksheet) (row, col int, err error) {
|
|
|
|
var rect []int
|
|
|
|
for _, mergeCell := range ws.MergeCells.Cells {
|
|
|
|
if mergeCell == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if rect, err = mergeCell.Rect(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
x1, y1, x2, y2 := rect[0], rect[1], rect[2], rect[3]
|
|
|
|
if x1 > col {
|
|
|
|
col = x1
|
|
|
|
}
|
|
|
|
if x2 > col {
|
|
|
|
col = x2
|
|
|
|
}
|
|
|
|
if y1 > row {
|
|
|
|
row = y1
|
|
|
|
}
|
|
|
|
if y2 > row {
|
|
|
|
row = y2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// flatMergedCells convert merged cells range reference to cell-matrix.
|
|
|
|
func flatMergedCells(ws *xlsxWorksheet, matrix [][]*xlsxMergeCell) error {
|
|
|
|
for i, cell := range ws.MergeCells.Cells {
|
|
|
|
rect, err := cell.Rect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
x1, y1, x2, y2 := rect[0]-1, rect[1]-1, rect[2]-1, rect[3]-1
|
|
|
|
var overlapCells []*xlsxMergeCell
|
|
|
|
for x := x1; x <= x2; x++ {
|
|
|
|
for y := y1; y <= y2; y++ {
|
|
|
|
if matrix[x][y] != nil {
|
|
|
|
overlapCells = append(overlapCells, matrix[x][y])
|
|
|
|
}
|
|
|
|
matrix[x][y] = cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(overlapCells) != 0 {
|
|
|
|
newCell := cell
|
|
|
|
for _, overlapCell := range overlapCells {
|
|
|
|
newCell = mergeCell(cell, overlapCell)
|
|
|
|
}
|
|
|
|
newRect, _ := newCell.Rect()
|
|
|
|
x1, y1, x2, y2 := newRect[0]-1, newRect[1]-1, newRect[2]-1, newRect[3]-1
|
|
|
|
for x := x1; x <= x2; x++ {
|
|
|
|
for y := y1; y <= y2; y++ {
|
|
|
|
matrix[x][y] = newCell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.MergeCells.Cells[i] = newCell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mergeOverlapCells merge overlap cells.
|
|
|
|
func (f *File) mergeOverlapCells(ws *xlsxWorksheet) error {
|
|
|
|
rows, cols, err := overlapRange(ws)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rows == 0 || cols == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
matrix := make([][]*xlsxMergeCell, cols)
|
|
|
|
for i := range matrix {
|
|
|
|
matrix[i] = make([]*xlsxMergeCell, rows)
|
|
|
|
}
|
|
|
|
_ = flatMergedCells(ws, matrix)
|
|
|
|
mergeCells := ws.MergeCells.Cells[:0]
|
|
|
|
for _, cell := range ws.MergeCells.Cells {
|
|
|
|
rect, _ := cell.Rect()
|
|
|
|
x1, y1, x2, y2 := rect[0]-1, rect[1]-1, rect[2]-1, rect[3]-1
|
|
|
|
if matrix[x1][y1] == cell {
|
|
|
|
mergeCells = append(mergeCells, cell)
|
|
|
|
for x := x1; x <= x2; x++ {
|
|
|
|
for y := y1; y <= y2; y++ {
|
|
|
|
matrix[x][y] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.MergeCells.Count, ws.MergeCells.Cells = len(mergeCells), mergeCells
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mergeCell merge two cells.
|
|
|
|
func mergeCell(cell1, cell2 *xlsxMergeCell) *xlsxMergeCell {
|
|
|
|
rect1, _ := cell1.Rect()
|
|
|
|
rect2, _ := cell2.Rect()
|
|
|
|
|
|
|
|
if rect1[0] > rect2[0] {
|
|
|
|
rect1[0], rect2[0] = rect2[0], rect1[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if rect1[2] < rect2[2] {
|
|
|
|
rect1[2], rect2[2] = rect2[2], rect1[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
if rect1[1] > rect2[1] {
|
|
|
|
rect1[1], rect2[1] = rect2[1], rect1[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if rect1[3] < rect2[3] {
|
|
|
|
rect1[3], rect2[3] = rect2[3], rect1[3]
|
|
|
|
}
|
2022-01-09 00:20:42 +08:00
|
|
|
hCell, _ := CoordinatesToCellName(rect1[0], rect1[1])
|
|
|
|
vCell, _ := CoordinatesToCellName(rect1[2], rect1[3])
|
|
|
|
return &xlsxMergeCell{rect: rect1, Ref: hCell + ":" + vCell}
|
2021-08-13 01:32:44 +08:00
|
|
|
}
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
// MergeCell define a merged cell data.
|
|
|
|
// It consists of the following structure.
|
|
|
|
// example: []string{"D4:E10", "cell value"}
|
|
|
|
type MergeCell []string
|
|
|
|
|
|
|
|
// GetCellValue returns merged cell value.
|
|
|
|
func (m *MergeCell) GetCellValue() string {
|
|
|
|
return (*m)[1]
|
|
|
|
}
|
|
|
|
|
2022-09-18 00:07:15 +08:00
|
|
|
// GetStartAxis returns the top left cell reference of merged range, for
|
2021-11-29 01:21:03 +08:00
|
|
|
// example: "C2".
|
2019-04-30 18:39:27 +08:00
|
|
|
func (m *MergeCell) GetStartAxis() string {
|
2022-09-18 00:07:15 +08:00
|
|
|
return strings.Split((*m)[0], ":")[0]
|
2019-04-30 18:39:27 +08:00
|
|
|
}
|
|
|
|
|
2022-09-18 00:07:15 +08:00
|
|
|
// GetEndAxis returns the bottom right cell reference of merged range, for
|
2021-11-29 01:21:03 +08:00
|
|
|
// example: "D4".
|
2019-04-30 18:39:27 +08:00
|
|
|
func (m *MergeCell) GetEndAxis() string {
|
2023-08-15 00:01:57 +08:00
|
|
|
coordinates := strings.Split((*m)[0], ":")
|
|
|
|
if len(coordinates) == 2 {
|
|
|
|
return coordinates[1]
|
|
|
|
}
|
|
|
|
return coordinates[0]
|
2019-06-05 22:06:15 +08:00
|
|
|
}
|