Life761
Life761

Reputation: 31

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

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
};

Upvotes: 1

Views: 2416

Answers (1)

Rick Kirkham
Rick Kirkham

Reputation: 9784

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;

Upvotes: 2

Related Questions