Skip to content

Commit 094ff39

Browse files
authored
This related with qax-os#2123, made editAs attribute value empty for one cell anchor drawing object (qax-os#2128)
* Fix absolute positioning to-point error and adjust one cell anchor drawing object failed * Revert default column width and row height * Clean shared formula cell cache before remvoe it in calc chain * Revert adjustFormulaRef changes in the commit 3ccbdaa * Update unit tests * Format code
1 parent d230ecd commit 094ff39

File tree

13 files changed

+66
-54
lines changed

13 files changed

+66
-54
lines changed

adjust.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,30 +416,48 @@ func (f *File) adjustFormulaOperand(sheet, sheetN string, keepRelative bool, tok
416416
// adjustFormulaRef returns adjusted formula by giving adjusting direction and
417417
// the base number of column or row, and offset.
418418
func (f *File) adjustFormulaRef(sheet, sheetN, formula string, keepRelative bool, dir adjustDirection, num, offset int) (string, error) {
419-
var definedNames []string
419+
var (
420+
val string
421+
definedNames []string
422+
ps = efp.ExcelParser()
423+
)
420424
for _, definedName := range f.GetDefinedName() {
421425
if definedName.Scope == "Workbook" || definedName.Scope == sheet {
422426
definedNames = append(definedNames, definedName.Name)
423427
}
424428
}
425-
ps := efp.ExcelParser()
426-
tokens := ps.Parse(formula)
427-
for i, token := range tokens {
429+
for _, token := range ps.Parse(formula) {
428430
if token.TType == efp.TokenTypeUnknown {
429-
return formula, nil
431+
val = formula
432+
break
430433
}
431434
if token.TType == efp.TokenTypeOperand && token.TSubType == efp.TokenSubTypeRange {
432-
if inStrSlice(definedNames, token.TValue, true) != -1 || strings.ContainsAny(token.TValue, "[]") {
435+
if inStrSlice(definedNames, token.TValue, true) != -1 {
436+
val += token.TValue
437+
continue
438+
}
439+
if strings.ContainsAny(token.TValue, "[]") {
440+
val += token.TValue
433441
continue
434442
}
435443
operand, err := f.adjustFormulaOperand(sheet, sheetN, keepRelative, token, dir, num, offset)
436444
if err != nil {
437-
return ps.Render(), err
445+
return val, err
438446
}
439-
tokens[i].TValue = operand
447+
val += operand
448+
continue
440449
}
450+
if paren := transformParenthesesToken(token); paren != "" {
451+
val += transformParenthesesToken(token)
452+
continue
453+
}
454+
if token.TType == efp.TokenTypeOperand && token.TSubType == efp.TokenSubTypeText {
455+
val += string(efp.QuoteDouble) + strings.ReplaceAll(token.TValue, "\"", "\"\"") + string(efp.QuoteDouble)
456+
continue
457+
}
458+
val += token.TValue
441459
}
442-
return ps.Render(), nil
460+
return val, nil
443461
}
444462

445463
// transformParenthesesToken returns formula part with parentheses by given

cell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ func (f *File) removeFormula(c *xlsxC, ws *xlsxWorksheet, sheet string) error {
189189
}
190190
if c.F.T == STCellFormulaTypeShared && c.F.Ref != "" {
191191
si := c.F.Si
192-
ws.deleteSharedFormula(c)
193192
for r, row := range ws.SheetData.Row {
194193
for col, cell := range row.C {
195194
if cell.F != nil && cell.F.Si != nil && *cell.F.Si == *si {
196195
ws.SheetData.Row[r].C[col].F = nil
196+
ws.deleteSharedFormula(c)
197197
_ = f.deleteCalcChain(sheetID, cell.R)
198198
}
199199
}

chart_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,8 @@ func TestChartSize(t *testing.T) {
9393
t.FailNow()
9494
}
9595

96-
if !assert.Equal(t, 11, anchor.To.Col, "Expected 'to' column 11") ||
97-
!assert.Equal(t, 27, anchor.To.Row, "Expected 'to' row 27") {
98-
99-
t.FailNow()
100-
}
96+
assert.Equal(t, 14, anchor.To.Col, "Expected 'to' column 14")
97+
assert.Equal(t, 29, anchor.To.Row, "Expected 'to' row 29")
10198
}
10299

103100
func TestAddDrawingChart(t *testing.T) {

col.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222

2323
// Define the default cell size and EMU unit of measurement.
2424
const (
25-
defaultColWidth float64 = 10.5
26-
defaultColWidthPixels float64 = 84.0
27-
defaultRowHeight float64 = 15.6
28-
defaultRowHeightPixels float64 = 20.8
25+
defaultColWidth float64 = 9.140625
26+
defaultColWidthPixels float64 = 64
27+
defaultRowHeight float64 = 15
28+
defaultRowHeightPixels float64 = 20
2929
EMU int = 9525
3030
)
3131

@@ -625,7 +625,7 @@ func (f *File) positionObjectPixels(sheet string, col, row, width, height int, o
625625
// Initialized end cell to the same as the start cell.
626626
colEnd, rowEnd := colIdx, rowIdx
627627
x1, y1, x2, y2 := opts.OffsetX, opts.OffsetY, width, height
628-
if opts.Positioning == "" || opts.Positioning == "twoCell" {
628+
if opts.Positioning != "oneCell" {
629629
// Using a twoCellAnchor, the maximum possible offset is limited by the
630630
// "from" cell dimensions. If these were to be exceeded the "toPoint" would
631631
// be calculated incorrectly, since the requested "fromPoint" is not possible
@@ -801,10 +801,6 @@ func convertColWidthToPixels(width float64) float64 {
801801
if width == 0 {
802802
return pixels
803803
}
804-
if width < 1 {
805-
pixels = (width * 12) + 0.5
806-
return float64(int(pixels))
807-
}
808804
pixels = (width*maxDigitWidth + 0.5)
809805
return float64(int(pixels))
810806
}

col_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,5 +484,5 @@ func TestRemoveCol(t *testing.T) {
484484
}
485485

486486
func TestConvertColWidthToPixels(t *testing.T) {
487-
assert.Equal(t, -11.0, convertColWidthToPixels(-1))
487+
assert.Equal(t, -7.0, convertColWidthToPixels(-1))
488488
}

drawing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ func (f *File) addSheetDrawingChart(drawingXML string, rID int, opts *GraphicOpt
14661466
absoluteAnchor := xdrCellAnchor{
14671467
EditAs: opts.Positioning,
14681468
Pos: &xlsxPoint2D{},
1469-
Ext: &aExt{},
1469+
Ext: &xlsxPositiveSize2D{},
14701470
}
14711471

14721472
graphicFrame := xlsxGraphicFrame{

picture.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, ext string, rID, hyper
372372
return err
373373
}
374374
cellAnchor := xdrCellAnchor{}
375-
cellAnchor.EditAs = opts.Positioning
376375
from := xlsxFrom{}
377376
from.Col = colStart
378377
from.ColOff = x1 * EMU
@@ -387,6 +386,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, ext string, rID, hyper
387386
to.Row = rowEnd
388387
to.RowOff = y2 * EMU
389388
cellAnchor.To = &to
389+
cellAnchor.EditAs = opts.Positioning
390390
}
391391

392392
pic := xlsxPic{}
@@ -420,7 +420,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, ext string, rID, hyper
420420
if opts.Positioning == "oneCell" {
421421
cx := x2 * EMU
422422
cy := y2 * EMU
423-
cellAnchor.Ext = &aExt{
423+
cellAnchor.Ext = &xlsxPositiveSize2D{
424424
Cx: cx,
425425
Cy: cy,
426426
}

rows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ func (f *File) getRowHeight(sheet string, row int) int {
467467
return int(convertRowHeightToPixels(ws.SheetFormatPr.DefaultRowHeight))
468468
}
469469
// Optimization for when the row heights haven't changed.
470-
return int(math.Round(defaultRowHeightPixels))
470+
return int(defaultRowHeightPixels)
471471
}
472472

473473
// GetRowHeight provides a function to get row height by given worksheet name
@@ -1012,5 +1012,5 @@ func convertRowHeightToPixels(height float64) float64 {
10121012
if height == 0 {
10131013
return 0
10141014
}
1015-
return float64(int(height*4.0/3.0 + 0.5))
1015+
return math.Ceil(4.0 / 3.4 * height)
10161016
}

rows_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func TestRowHeight(t *testing.T) {
205205
assert.NoError(t, f.SetCellValue("Sheet2", "A2", true))
206206
height, err = f.GetRowHeight("Sheet2", 1)
207207
assert.NoError(t, err)
208-
assert.Equal(t, 15.6, height)
208+
assert.Equal(t, 15.0, height)
209209

210210
err = f.SaveAs(filepath.Join("test", "TestRowHeight.xlsx"))
211211
if !assert.NoError(t, err) {

slicer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ func (f *File) addDrawingSlicer(sheet, slicerName string, ns xml.Attr, opts *Sli
612612
Name: slicerName,
613613
},
614614
},
615-
Xfrm: xlsxXfrm{Off: xlsxOff{}, Ext: aExt{}},
615+
Xfrm: xlsxXfrm{Off: xlsxOff{}, Ext: xlsxPositiveSize2D{}},
616616
Graphic: &xlsxGraphic{
617617
GraphicData: &xlsxGraphicData{
618618
URI: NameSpaceDrawingMLSlicer.Value,
@@ -632,7 +632,7 @@ func (f *File) addDrawingSlicer(sheet, slicerName string, ns xml.Attr, opts *Sli
632632
},
633633
},
634634
SpPr: &xlsxSpPr{
635-
Xfrm: xlsxXfrm{Off: xlsxOff{X: 2914650, Y: 152400}, Ext: aExt{Cx: 1828800, Cy: 2238375}},
635+
Xfrm: xlsxXfrm{Off: xlsxOff{X: 2914650, Y: 152400}, Ext: xlsxPositiveSize2D{Cx: 1828800, Cy: 2238375}},
636636
SolidFill: &xlsxInnerXML{Content: "<a:prstClr val=\"white\"/>"},
637637
PrstGeom: xlsxPrstGeom{
638638
Prst: "rect",

0 commit comments

Comments
 (0)