Posted 10 August 2021, 5:15 am EST
Hi Ryan,
For this, you may use the shrinkToFit feature of spreadJS. Please refer to the following code snippet.`
sheet.getCell(0, 5).text("This is a long text").shrinkToFit(true);
Further, if you only want to reduce the font size for a certain size you need to override the paint method and update the font size according to the need. Please refer to the following code snippet and attached sample that demonstrates the same.
let old = GC.Spread.Sheets.CellTypes.Text.prototype.paint;
GC.Spread.Sheets.CellTypes.Text.prototype.paint = function (
ctx,
value,
x,
y,
w,
h,
style,
context
) {
if (value && style.shrinkToFit) {
let sheet = context.sheet;
//console.log(sheet.defaults.colWidth);
if (getTextLength(value, style.font) > sheet.defaults.colWidth) {
//change the font according to the need
style.font = getUpdatedFont(style.font, "fontSize", "10pt");
}
}
old.apply(this, arguments);
};
sample: https://codesandbox.io/s/spread-js-starter-forked-zguzv?file=/src/index.js:425-941
Fonts demo: https://www.grapecity.com/spreadjs/demos/features/cells/font#demo_source_name
Regards,
Avinash