Posted 20 May 2021, 9:05 pm EST
Hi,
For this you need to override the getAutoFitWidth method on CellType Class and return the calculated width for the column and whenever you need to auto-fit the column you just call the sheet.autoFitColumn method. Please refer to the following code snippet and attached sample that demonstrates the same.
spread.options.autoFitType = GC.Spread.Sheets.AutoFitType.cellWithHeader;
ComboBoxCellType.prototype.getAutoFitWidth = function (hitInfo) {
return (
this.iconWidth +
getTextWidth(this.selectedOption.text, this.select.style.font)
);
};
function getTextWidth(text, fontStyle) {
var c = document.createElement("canvas");
var ctx = c.getContext("2d");
ctx.font = fontStyle;
var txt = text;
return Math.ceil(ctx.measureText(txt).width);
}
let headerCellType = new ComboBoxCellType([
{ text: "The is the long text", value: "text2" },
{ text: "the is the much more longer text", value: "text2" },
{
text: "This is longest text to demonstrates the autoFit feature",
value: "text3"
}
]);
sheet1.setCellType(0, 0, headerCellType, 1);
document.getElementById("autoFit").addEventListener("click", () => {
sheet1.autoFitColumn(0);
});
sample: https://codesandbox.io/s/great-hodgkin-lcmd5?file=/src/index.js:304-377
Regards
Avinash