Anonymous - 2018-09-18

Originally posted by: piaoger

@teschste I met this issue and borrown a solution from sheetjs :) I called px2width before setting col width

// borrowed from SheetJS/js-xlsx
// https://github.com/SheetJS/js-xlsx/blob/master/bits/46_stycommon.js

// /* 18.3.1.13 width calculations */
// /* [MS-OI29500] 2.1.595 Column Width & Formatting */
// var DEF_MDW = 6, MAX_MDW = 15, MIN_MDW = 1, MDW = DEF_MDW;
// function width2px(width) { return Math.floor(( width + (Math.round(128/MDW))/256 )* MDW ); }
// function px2char(px) { return (Math.floor((px - 5)/MDW * 100 + 0.5))/100; }
// function char2width(chr) { return (Math.round((chr * MDW + 5)/MDW*256))/256; }

const (
    DEF_MDW = 6
    MAX_MDW = 15
    MIN_MDW = 1
    MDW     = DEF_MDW
)

func width2px(width float64) float64 {
    return math.Floor((width + (math.Round(128.0/MDW))/256.0) * MDW)
}

func px2char(px float64) float64 {
    return math.Floor(((px-5)/MDW*100 + 0.5) / 100.0)
}

func char2width(chr float64) float64 {
    return (math.Round((chr*MDW + 5) / MDW * 256.0)) / 256.0
}

func px2width(px float64) float64 {
    wch := px2char(px)
    width := char2width(wch)
    return width
}