How to use numFormat method of diff Package

Best Got code snippet using diff.numFormat

style_sheet.go

Source:style_sheet.go Github

copy

Full Screen

...8	"github.com/plandem/xlsx/internal/number_format"9	_ "unsafe"10)11//go:linkname fromStyleFormat github.com/plandem/xlsx/format.fromStyleFormat12func fromStyleFormat(f *format.StyleFormat) (font *ml.Font, fill *ml.Fill, alignment *ml.CellAlignment, numFormat *ml.NumberFormat, protection *ml.CellProtection, border *ml.Border, namedInfo *ml.NamedStyleInfo)13//StyleSheet is a higher level object that wraps ml.StyleSheet with functionality14type StyleSheet struct {15	ml ml.StyleSheet16	//hash -> index for styles17	directStyleIndex map[hash.Code]format.DirectStyleID18	diffStyleIndex   map[hash.Code]format.DiffStyleID19	namedStyleIndex  map[hash.Code]format.NamedStyleID20	//hash -> index for types21	borderIndex map[hash.Code]int22	fillIndex   map[hash.Code]int23	fontIndex   map[hash.Code]int24	numberIndex map[hash.Code]int25	//hash for typed number formats26	typedStyles map[numberFormat.Type]format.DirectStyleID27	doc  *Spreadsheet28	file *ooxml.PackageFile29}30func newStyleSheet(f interface{}, doc *Spreadsheet) *StyleSheet {31	ss := &StyleSheet{32		doc:              doc,33		directStyleIndex: make(map[hash.Code]format.DirectStyleID),34		diffStyleIndex:   make(map[hash.Code]format.DiffStyleID),35		namedStyleIndex:  make(map[hash.Code]format.NamedStyleID),36		borderIndex:      make(map[hash.Code]int),37		fillIndex:        make(map[hash.Code]int),38		fontIndex:        make(map[hash.Code]int),39		numberIndex:      make(map[hash.Code]int),40		typedStyles:      make(map[numberFormat.Type]format.DirectStyleID),41	}42	ss.file = ooxml.NewPackageFile(doc.pkg, f, &ss.ml, nil)43	if ss.file.IsNew() {44		ss.doc.pkg.ContentTypes().RegisterContent(ss.file.FileName(), internal.ContentTypeStyles)45		ss.doc.relationships.AddFile(internal.RelationTypeStyles, ss.file.FileName())46		ss.file.MarkAsUpdated()47		ss.addDefaults()48		ss.buildIndexes()49	}50	return ss51}52//adds a default items for new created xlsx53func (ss *StyleSheet) addDefaults() {54	//TODO: research more about default items for a new XLSX55	//..56	//add default types57	ss.ml.Fills.Items = append(ss.ml.Fills.Items,58		&ml.Fill{59			Pattern: &ml.PatternFill{60				Type: format.PatternTypeNone,61			},62		},63		&ml.Fill{64			Pattern: &ml.PatternFill{65				Type: format.PatternTypeGray125,66			},67		},68	)69	ss.ml.Borders.Items = append(ss.ml.Borders.Items, &ml.Border{70		Left:   &ml.BorderSegment{},71		Right:  &ml.BorderSegment{},72		Top:    &ml.BorderSegment{},73		Bottom: &ml.BorderSegment{},74	})75	ss.ml.Fonts.Items = append(ss.ml.Fonts.Items, &ml.Font{76		Family: format.FontFamilySwiss,77		Scheme: format.FontSchemeMinor,78		Name:   "Calibri",79		Size:   11.0,80		//Color: ml.Color{Theme: 1}81	})82	//add default ref for CellStyleXfs83	ss.ml.CellStyleXfs.Items = append(ss.ml.CellStyleXfs.Items, &ml.NamedStyle{84		FontId:   0,85		FillId:   0,86		BorderId: 0,87		NumFmtId: 0,88	})89	//add default ref for CellXfs90	ss.ml.CellXfs.Items = append(ss.ml.CellXfs.Items, &ml.DirectStyle{91		XfId: ml.NamedStyleID(0),92		Style: ml.Style{93			FontId:   0,94			FillId:   0,95			BorderId: 0,96			NumFmtId: 0,97		},98	})99	//add default ref for CellStyles100	index := 0101	ss.ml.CellStyles.Items = append(ss.ml.CellStyles.Items, &ml.NamedStyleInfo{102		Name:      "Normal",103		XfId:      ml.NamedStyleID(0),104		BuiltinId: &index,105	})106	/*107		TODO: replace hardcoded defaults with format108		def := format.NewStyles(109			format.NamedStyle(format.NamedStyleNormal),110			format.Font.Default,111		)112	*/113}114//build indexes for all indexes115func (ss *StyleSheet) buildIndexes() {116	//build indexes for fonts117	for id, f := range ss.ml.Fonts.Items {118		ss.fontIndex[hash.Font(f).Hash()] = id119	}120	//build indexes for fill121	for id, f := range ss.ml.Fills.Items {122		ss.fillIndex[hash.Fill(f).Hash()] = id123	}124	//build indexes for border125	for id, f := range ss.ml.Borders.Items {126		ss.borderIndex[hash.Border(f).Hash()] = id127	}128	//build indexes for number formats129	for _, f := range ss.ml.NumberFormats.Items {130		//N.B.: NumberFormat uses ID, not indexes131		ss.numberIndex[hash.NumberFormat(f).Hash()] = f.ID132	}133	//build indexes for named styles134	for id, xf := range ss.ml.CellStyleXfs.Items {135		ss.namedStyleIndex[hash.NamedStyle(xf).Hash()] = format.NamedStyleID(id)136	}137	//build indexes for direct styles138	for id, xf := range ss.ml.CellXfs.Items {139		ss.directStyleIndex[hash.DirectStyle(xf).Hash()] = format.DirectStyleID(id)140	}141	//build indexes for differential styles142	for id, dxf := range ss.ml.Dxfs.Items {143		ss.diffStyleIndex[hash.DiffStyle(dxf).Hash()] = format.DiffStyleID(id)144	}145}146//adds a number formats for each type of number format if required. These styles will be used by cell's typed SetXXX methods147func (ss *StyleSheet) addTypedStylesIfRequired() {148	if len(ss.typedStyles) == 0 {149		for _, t := range []numberFormat.Type{150			numberFormat.General,151			numberFormat.Integer,152			numberFormat.Float,153			numberFormat.Date,154			numberFormat.Time,155			numberFormat.DateTime,156			numberFormat.DeltaTime,157		} {158			id, _ := numberFormat.Default(t)159			ss.typedStyles[t] = ss.addStyle(format.NewStyles(format.NumberFormatID(id)))160		}161		ss.file.MarkAsUpdated()162	}163}164//resolveNumberFormat returns resolved NumberFormat code for styleID165func (ss *StyleSheet) resolveNumberFormat(id ml.DirectStyleID) string {166	style := ss.ml.CellXfs.Items[id]167	//return code for built-in number format168	if number := numberFormat.Normalize(ml.NumberFormat{ID: style.NumFmtId}); len(number.Code) > 0 {169		return number.Code170	}171	//try to lookup through custom formats and find same ID172	for _, f := range ss.ml.NumberFormats.Items {173		if style.NumFmtId == f.ID {174			return f.Code175		}176	}177	//N.B.: wtf is going on?! non built-in and not existing id?178	_, code := numberFormat.Default(numberFormat.General)179	return code180}181//resolveDirectStyle returns resolved StyleFormat for DirectStyleID182func (ss *StyleSheet) resolveDirectStyle(id ml.DirectStyleID) *format.StyleFormat {183	if id == 0 {184		return nil185	}186	cellStyle := ss.ml.CellXfs.Items[id]187	style := &format.StyleFormat{}188	_ = cellStyle189	//TODO: Populate format.StyleFormat with required information190	panic(errorNotSupported)191	return style192}193//adds a differential style194func (ss *StyleSheet) addDiffStyle(f *format.StyleFormat) format.DiffStyleID {195	ss.file.LoadIfRequired(ss.buildIndexes)196	//get settings for style197	font, fill, alignment, numFormat, protection, border, _ := fromStyleFormat(f)198	dXf := &ml.DiffStyle{199		Font:         font,200		Fill:         fill,201		Border:       border,202		NumberFormat: numFormat,203		Alignment:    alignment,204		Protection:   protection,205	}206	//return id of already existing information207	key := hash.DiffStyle(dXf).Hash()208	if id, ok := ss.diffStyleIndex[key]; ok {209		return format.DiffStyleID(id)210	}211	//add a new one and return related id212	nextID := format.DiffStyleID(len(ss.ml.Dxfs.Items))213	ss.ml.Dxfs.Items = append(ss.ml.Dxfs.Items, dXf)214	ss.diffStyleIndex[key] = nextID215	ss.file.MarkAsUpdated()216	return nextID217}218//add a named style if required219func (ss *StyleSheet) addNamedStyleIfRequired(namedInfo *ml.NamedStyleInfo, style ml.Style) ml.NamedStyleID {220	if namedInfo == nil {221		return 0222	}223	namedStyle := ml.NamedStyle(style)224	key := hash.NamedStyle(&namedStyle).Hash()225	//TODO: check if it's possible to have 2 same built-styles226	//if there is already same styles, then use it227	if id, ok := ss.namedStyleIndex[key]; ok {228		namedInfo.XfId = ml.NamedStyleID(id)229	} else {230		//add a new style231		nextID := format.NamedStyleID(len(ss.ml.CellStyleXfs.Items))232		ss.ml.CellStyleXfs.Items = append(ss.ml.CellStyleXfs.Items, &namedStyle)233		ss.namedStyleIndex[key] = nextID234		//add style info235		namedInfo.XfId = ml.NamedStyleID(nextID)236		ss.ml.CellStyles.Items = append(ss.ml.CellStyles.Items, namedInfo)237	}238	//add named info239	ss.file.MarkAsUpdated()240	return namedInfo.XfId241}242//adds a style. Style can be Direct or Named. Depends on settings.243func (ss *StyleSheet) addStyle(f *format.StyleFormat) format.DirectStyleID {244	ss.file.LoadIfRequired(ss.buildIndexes)245	//get settings and add information if required246	font, fill, alignment, numFormat, protection, border, namedInfo := fromStyleFormat(f)247	fontID := ss.addFontIfRequired(font)248	fillID := ss.addFillIfRequired(fill)249	borderID := ss.addBorderIfRequired(border)250	numID := ss.addNumFormatIfRequired(numFormat)251	/*252		Note to remember excel internals:253		---254		cell.s = cellXfs.index  => DirectStyleID255		cellXfs.xfId = cellStyleXf.index => NamedStyleID256		cellStyle.xfId = cellStyleXf.index => NamedStyleID257	*/258	XfId := ml.NamedStyleID(0)259	style := ml.Style{260		FontId:            fontID,261		FillId:            fillID,262		BorderId:          borderID,263		NumFmtId:          numID,264		Alignment:         alignment,...

Full Screen

Full Screen

styles.go

Source:styles.go Github

copy

Full Screen

...45		o(s)46	}47}48//private method used by stylesheet manager to unpack StyleFormat49func fromStyleFormat(f *StyleFormat) (font *ml.Font, fill *ml.Fill, alignment *ml.CellAlignment, numFormat *ml.NumberFormat, protection *ml.CellProtection, border *ml.Border, namedInfo *ml.NamedStyleInfo) {50	style := f.styleInfo51	named := f.namedInfo52	//copy non-empty namedInfo53	if *named != (ml.NamedStyleInfo{}) {54		namedInfo = &ml.NamedStyleInfo{}55		*namedInfo = *named56	}57	//copy non-empty alignment58	if *style.Alignment != (ml.CellAlignment{}) {59		alignment = &ml.CellAlignment{}60		*alignment = *style.Alignment61	}62	//copy non-empty font63	if (*style.Font != ml.Font{} && *style.Font != ml.Font{Size: 0, Family: 0, Charset: 0}) {64		font = &ml.Font{}65		*font = *style.Font66	}67	//copy non-empty numFormat68	if *style.NumberFormat != (ml.NumberFormat{}) {69		numFormat = &ml.NumberFormat{}70		*numFormat = *style.NumberFormat71	}72	//copy non-empty protection73	if *style.Protection != (ml.CellProtection{}) {74		protection = &ml.CellProtection{}75		*protection = *style.Protection76	}77	//copy non-empty border78	border = &ml.Border{}79	*border = *style.Border80	if reflect.DeepEqual(border.Left, &ml.BorderSegment{}) {81		border.Left = nil82	} else {83		border.Left = &ml.BorderSegment{}84		*border.Left = *style.Border.Left...

Full Screen

Full Screen

numFormat

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3    fmt.Println(numFormat(1234))4}5import "fmt"6func numFormat(num int) string {7    return fmt.Sprintf("%d", num)8}9import "fmt"10func numFormat(num int) string {11    return fmt.Sprintf("%d", num)12}13import "fmt"14func numFormat(num int) string {15    return fmt.Sprintf("%d", num)16}17import "fmt"18func numFormat(num int) string {19    return fmt.Sprintf("%d", num)20}21import "fmt"22func numFormat(num int) string {23    return fmt.Sprintf("%d", num)24}25import "fmt"26func numFormat(num int) string {27    return fmt.Sprintf("%d", num)28}29import "fmt"30func numFormat(num int) string {31    return fmt.Sprintf("%d", num)32}33import "fmt"34func numFormat(num int) string {35    return fmt.Sprintf("%d", num)36}37import "fmt"38func numFormat(num int) string {39    return fmt.Sprintf("%d", num)40}41import "fmt"42func numFormat(num int) string {43    return fmt.Sprintf("%d", num)44}45import "fmt"46func numFormat(num int) string {47    return fmt.Sprintf("%d", num)48}

Full Screen

Full Screen

numFormat

Using AI Code Generation

copy

Full Screen

1import (2func (n numFormat) String() string {3	return fmt.Sprintf("%d", n)4}5func main() {6	fmt.Printf("Value of n1 is %v and Type of n1 is %T7	fmt.Printf("Value of n2 is %v and Type of n2 is %T8}

Full Screen

Full Screen

numFormat

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main(){3    fmt.Println("Enter a number : ")4    fmt.Scan(&num)5    fmt.Println(numFormat(num))6}7func numFormat(num float64) string {8    return fmt.Sprintf("%.2f", num)9}

Full Screen

Full Screen

numFormat

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    num := numFormat.NumFormat(123456789)4    fmt.Println(num)5}6import (7func NumFormat(num int) string {8    str := strconv.Itoa(num)9    for i := len(str) - 1; i >= 0; i-- {10        rev = rev + string(str[i])11    }12    for i := 0; i < len(rev); i++ {13        if i%3 == 0 && i != 0 {14            numStr = numStr + "," + string(rev[i])15        } else {16            numStr = numStr + string(rev[i])17        }18    }19    for i := len(numStr) - 1; i >= 0; i-- {20        finalNum = finalNum + string(numStr[i])21    }22}23func FormatFloat(f float64, fmt byte, prec, bitSize int) string24import (25func main() {26    num := fmt.Sprintf("%.2f", 12345.6789)27    fmt.Println(num)28}

Full Screen

Full Screen

numFormat

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3    fmt.Println("f1=", f1)4    fmt.Printf("f1=%f5    fmt.Printf("f1=%9.2f6    fmt.Printf("f1=%9.4f7    fmt.Printf("f1=%9.f8    fmt.Printf("f1=%9.f

Full Screen

Full Screen

numFormat

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	num := numFormat.NumFormat()4	fmt.Println(num)5}6import (7func main() {8	num := numFormat.NumFormat()9	fmt.Println(num)10}11import (12func main() {13	num := numFormat.NumFormat()14	fmt.Println(num)15}16import (17func main() {18	num := numFormat.NumFormat()19	fmt.Println(num)20}21import (22func main() {23	num := numFormat.NumFormat()24	fmt.Println(num)25}26import (27func main() {28	num := numFormat.NumFormat()29	fmt.Println(num)30}31import (32func main() {33	num := numFormat.NumFormat()34	fmt.Println(num)35}36import (37func main() {38	num := numFormat.NumFormat()39	fmt.Println(num)40}41import (42func main() {43	num := numFormat.NumFormat()44	fmt.Println(num)45}46import (47func main() {48	num := numFormat.NumFormat()49	fmt.Println(num)50}51import (52func main() {

Full Screen

Full Screen

numFormat

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "strconv"3func main(){4    fmt.Println(strconv.FormatInt(int64(i), 2))5    fmt.Println(strconv.FormatFloat(float64(f), 'f', 6, 64))6}

Full Screen

Full Screen

numFormat

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	fmt.Println("Hello, playground")4	fmt.Println(numFormat(num))5}6import "fmt"7func numFormat(num float64) string {8	var numFormat = fmt.Sprintf("%.2f", num)9}10import "fmt"11func numFormat(num float64) string {12	var numFormat = fmt.Sprintf("%.2f", num)13}14import "fmt"15func numFormat(num float64) string {16	var numFormat = fmt.Sprintf("%.2f", num)17}18import "fmt"19func numFormat(num float64) string {20	var numFormat = fmt.Sprintf("%.2f", num)21}22import "fmt"23func numFormat(num float64) string {24	var numFormat = fmt.Sprintf("%.2f", num)25}26import "fmt"27func numFormat(num float64) string {28	var numFormat = fmt.Sprintf("%.2f", num)29}30import "fmt"31func numFormat(num float64) string {32	var numFormat = fmt.Sprintf("%.2f", num)33}34import "fmt"35func numFormat(num float64) string {36	var numFormat = fmt.Sprintf("%.2f", num)37}38import "fmt"39func numFormat(num float64) string {40	var numFormat = fmt.Sprintf("%.2f", num)41}42import "fmt"43func numFormat(num float64) string {

Full Screen

Full Screen

numFormat

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	fmt.Println(numFormat.NumFormat(1234567890))4}5import "fmt"6func init() {7	fmt.Println("init method called")8}9func main() {10	fmt.Println("main method called")11}12import (13func init() {14	fmt.Println("main init method called")15}16func main() {17	fmt.Println("main method called")18}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful