Home » excel » javascript – Set column width for a single column in office.js excel add-in

javascript – Set column width for a single column in office.js excel add-in

Posted by: admin May 14, 2020 Leave a comment

Questions:

In my excel add-in I am trying to set the width of column ‘A’ after an auto-formatting call. The reason behind this is that I want column ‘A’ to have a minimum width since after calling autofitColumns(), it is quite a bit smaller than I would like it.

The ideal scenario would be to set a minimum column width throughout my spreadsheet before the column widths are auto-formatted (ie. no column could be less than size ’50’). However, simply being able to set the width for column ‘A’ would be enough.

Here is the function I have right now:

excelUtils.autofitContent = function (sheet) {

    sheet.getUsedRange().getEntireRow().format.autofitRows();
    sheet.getUsedRange().getEntireColumn().format.autofitColumns();
    // TODO: set column A width here
};
How to&Answers:

You can get a reference to the column you want by getting a reference to any cell in that column and then set the range.format.columnWidth property. The units of measure are points.

const range = sheet.getRange("A1");
range.format.columnWidth = 100;