Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,45 @@ func (ws *xlsxWorksheet) prepareSheetXML(col, row int) {
}
rowData := &ws.SheetData.Row[row-1]
fillColumns(rowData, col, row)
ws.expandSheetDimension(col, row)
}

func (ws *xlsxWorksheet) expandSheetDimension(col, row int) {
if col < 1 || row < 1 {
return
}
cell, err := CoordinatesToCellName(col, row)
if err != nil {
return
}
if ws.Dimension == nil || ws.Dimension.Ref == "" {
ws.Dimension = &xlsxDimension{Ref: cell}
return
}
coordinates, err := rangeRefToCoordinates(ws.Dimension.Ref)
if err != nil {
ws.Dimension = &xlsxDimension{Ref: cell}
return
}
_ = sortCoordinates(coordinates)
if col < coordinates[0] {
coordinates[0] = col
}
if row < coordinates[1] {
coordinates[1] = row
}
if col > coordinates[2] {
coordinates[2] = col
}
if row > coordinates[3] {
coordinates[3] = row
}
ref, err := coordinatesToRangeRef(coordinates)
if err != nil {
ws.Dimension = &xlsxDimension{Ref: cell}
return
}
ws.Dimension = &xlsxDimension{Ref: ref}
}

// fillColumns fill cells in the column of the row as contiguous.
Expand Down
28 changes: 27 additions & 1 deletion sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,14 @@ func TestWorksheetWriter(t *testing.T) {
// Test set cell value with alternate content
f.Sheet.Delete("xl/worksheets/sheet1.xml")
worksheet := xml.Header + `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheetData><row r="1"><c r="A1"><v>%d</v></c></row></sheetData><mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"><mc:Choice xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" Requires="a14"><xdr:twoCellAnchor editAs="oneCell"></xdr:twoCellAnchor></mc:Choice><mc:Fallback/></mc:AlternateContent></worksheet>`
updatedWorksheet := xml.Header + `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1"></dimension><sheetData><row r="1"><c r="A1"><v>%d</v></c></row></sheetData><mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"><mc:Choice xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" Requires="a14"><xdr:twoCellAnchor editAs="oneCell"></xdr:twoCellAnchor></mc:Choice><mc:Fallback/></mc:AlternateContent></worksheet>`
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(fmt.Sprintf(worksheet, 1)))
f.checked = sync.Map{}
assert.NoError(t, f.SetCellValue("Sheet1", "A1", 2))
f.workSheetWriter()
value, ok := f.Pkg.Load("xl/worksheets/sheet1.xml")
assert.True(t, ok)
assert.Equal(t, fmt.Sprintf(worksheet, 2), string(value.([]byte)))
assert.Equal(t, fmt.Sprintf(updatedWorksheet, 2), string(value.([]byte)))
}

func TestGetWorkbookPath(t *testing.T) {
Expand Down Expand Up @@ -840,6 +841,31 @@ func TestSheetDimension(t *testing.T) {
assert.Empty(t, dimension)
assert.Equal(t, err, ErrSheetNameBlank)

f = NewFile()
assert.NoError(t, f.SetCellValue(sheetName, "C5", "value"))
dimension, err = f.GetSheetDimension(sheetName)
assert.NoError(t, err)
assert.Equal(t, "C5", dimension)

filePath := filepath.Join(t.TempDir(), "sheet-dimension.xlsx")
assert.NoError(t, f.SaveAs(filePath))
assert.NoError(t, f.Close())

f, err = OpenFile(filePath)
assert.NoError(t, err)
dimension, err = f.GetSheetDimension(sheetName)
assert.NoError(t, err)
assert.Equal(t, "C5", dimension)
assert.NoError(t, f.Close())

f = NewFile()
assert.NoError(t, f.SetSheetDimension(sheetName, "B2:E61"))
assert.NoError(t, f.SetCellValue(sheetName, "G64", "value"))
dimension, err = f.GetSheetDimension(sheetName)
assert.NoError(t, err)
assert.Equal(t, "B2:G64", dimension)
assert.NoError(t, f.Close())

// Test get the worksheet dimension with in mode
f, err = OpenFile(filepath.Join("test", "Book1.xlsx"), Options{UnzipXMLSizeLimit: 128})
assert.NoError(t, err)
Expand Down