javascriptgoogle-sheetsgoogle-apps-scriptpixel

Getting and Setting Cell Pixel Width and Height of Google Sheet with Apps Script


I was wondering if anyone had a good way to get and set the height and width in pixels for a Google Cell using Google Apps Script. There is a getWidth() function build in, but it returns the width of the range in cells which is not what I need.

For example:

function giveWidth() 
{
     var ss = SpreadsheetApp.getActive();
     return ss.getRange("C11").getWidth();
}

output: 1.0


Solution

  • If you need to get the width of cell C11 which it's column index is 3, you just use this;

    ss.getColumnWidth(3)
    

    Similarly, if you need to set the width of the 3rd column which is column "C", you can use this where the column index and width is defined.

    ss.setColumnWidth(3, 200)
    

    Same is true for rows, such as the below will set the height of the 3rd row by using the index of the row and the height value.

    ss.setRowHeight(3, 200)