Skip to content

Instantly share code, notes, and snippets.

@ttsukagoshi
Created October 15, 2020 03:12
Show Gist options
  • Save ttsukagoshi/bfd7057b717a5b9321329a3c138ecbc7 to your computer and use it in GitHub Desktop.
Save ttsukagoshi/bfd7057b717a5b9321329a3c138ecbc7 to your computer and use it in GitHub Desktop.
Gets the cells' height and width in pixels for the selected range in Google Spreadsheet in form of a 2-d JavaScript array; the array values are ordered in the same way as executing Range.getValues()
/**
* Gets the cells' height and width in pixels for the selected range in Google Spreadsheet in form of a 2-d JavaScript array;
* the array values are ordered in the same way as executing Range.getValues()
* @param {Object} activeSheet The active Sheet class object in Google Spreadsheet, e.g., SpreadsheetApp.getActiveSheet()
* @param {Object} activeRange The selected Range class object in Google Spreadsheet, e.g., SpreadsheetApp.getActiveRange()
*/
function cellPixSizes(activeSheet, activeRange) {
var rangeStartCell = { 'row': activeRange.getRow(), 'column': activeRange.getColumn() };
var cellValues = activeRange.getValues();
var cellPixSizes = cellValues.map(function(row, rowIndex) {
let rowPix = activeSheet.getRowHeight(rangeStartCell.row + rowIndex);
let rowPixSizes = row.map(function(column, colIndex) {
let cellSize = {'height': rowPix, 'width': activeSheet.getColumnWidth(rangeStartCell.column + colIndex)};
return cellSize;
});
return rowPixSizes;
});
return cellPixSizes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment