Repository: unidoc/unioffice (Go)

Issue:
Problem with spreadsheet.InsertRow
## Description
There is a problem with spreadsheet.InsertRow.

![template](https://user-images.githubusercontent.com/2686935/46864177-3351aa00-ce22-11e8-92f8-05676c60250d.png)

1. we insert new row, number 2
2. rows after are renumbered
3. but merged cells still on the same row(3) like before, they are not moved down to line 4

```
package main // import "github.com/l0rda/testgooxmlinsert"

import (
	"log"

	"baliance.com/gooxml/spreadsheet"
)

func test1() {
	wb, err := spreadsheet.Open("test.xlsx")
	if err != nil {
		log.Fatal(err)
	}
	defer wb.Close()
	sheet, err := wb.GetSheet("test")
	if err != nil {
		log.Fatal(err)
	}
	sheet.InsertRow(2)
	wb.SaveToFile("out1.xlsx")
}

func main() {
	test1()
}

```

## Expected Behavior
![expected](https://user-images.githubusercontent.com/2686935/46864188-3a78b800-ce22-11e8-9fcb-9984727b7713.png)

## Actual Behavior
![actual](https://user-images.githubusercontent.com/2686935/46864196-41072f80-ce22-11e8-8224-e35caf938552.png)


Working example https://github.com/l0rda/testgooxmlinsert


Discussion:
Thanks for the great bug report! Can you try that branch out and see if it works for you?
Thanks. Now it works as expected.

Waiting for merge.





Patch:
diff --git a/spreadsheet/sheet.go b/spreadsheet/sheet.go
index b0ade154a5..9d08864d94 100644
--- a/spreadsheet/sheet.go
+++ b/spreadsheet/sheet.go
@@ -136,6 +136,24 @@ func (s Sheet) InsertRow(rowNum int) Row {
 			}
 		}
 	}
+
+	// check for merged cells, and try to intelligently adjust them
+	// issue #212
+	for _, mc := range s.MergedCells() {
+		from, to, err := reference.ParseRangeReference(mc.Reference())
+		if err != nil {
+			continue
+		}
+		if int(from.RowIdx) >= rowNum {
+			from.RowIdx++
+		}
+		if int(to.RowIdx) >= rowNum {
+			to.RowIdx++
+		}
+		ref := fmt.Sprintf("%s:%s", from, to)
+		mc.SetReference(ref)
+	}
+
 	// finally AddNumberedRow will add and re-sort rows
 	return s.AddNumberedRow(rIdx)
 }


Test patch:
diff --git a/spreadsheet/worksheet_test.go b/spreadsheet/worksheet_test.go
index 9793f8f46b..ce31261e6e 100644
--- a/spreadsheet/worksheet_test.go
+++ b/spreadsheet/worksheet_test.go
@@ -15,6 +15,7 @@ import (
 	"testing"
 
 	"baliance.com/gooxml/schema/soo/sml"
+	"baliance.com/gooxml/spreadsheet"
 	"baliance.com/gooxml/testhelper"
 	"baliance.com/gooxml/zippkg"
 )
@@ -38,3 +39,28 @@ func TestWorksheetUnmarshal(t *testing.T) {
 
 	testhelper.CompareGoldenXML(t, "worksheet.xml", got.Bytes())
 }
+
+// Issue #212
+func TestInsertMergedCells(t *testing.T) {
+	wb := spreadsheet.New()
+	sheet := wb.AddSheet()
+	sheet.AddMergedCells("A1", "C1")
+	sheet.AddMergedCells("A2", "C2")
+	sheet.AddMergedCells("A3", "C3")
+	sheet.AddMergedCells("D1", "E3")
+	sheet.InsertRow(2)
+
+	// should go down a line
+	for i, exp := range []string{
+		"A1:C1", // before inserted row, no change
+		"A3:C3", // after inserted row, moved down
+		"A4:C4", // after inserted row, moved down
+		"D1:E4", // covers inserted row, expanded
+	} {
+		got := sheet.MergedCells()[i].Reference()
+
+		if got != exp {
+			t.Errorf("expected %s, got %s", exp, got)
+		}
+	}
+}