{"size": 1578, "ext": "js", "max_stars_repo_path": "src/formatters/cols/formatCol.js", "max_stars_repo_name": "DavidRamos015/zipcelx", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/formatters/cols/formatCol.js", "max_issues_repo_name": "DavidRamos015/zipcelx", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/formatters/cols/formatCol.js", "max_forks_repo_name": "DavidRamos015/zipcelx", "max_forks_repo_forks_event_min_datetime": "2020-12-23T17:33:05.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-23T17:33:05.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 54.4137931034, "max_line_length": 148, "alphanum_fraction": 0.7027883397}
import Truncate from '../../util/Truncate';

// Column width measured as the number of characters of the maximum digit width of the numbers 0, 1, 2, …, 9 as rendered in the normal style's font.
// There are 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines.
export default (col, index) => {
  // If no width specified (0 width is not allowed as well) leave the definition empty and the width will be applied automatically.
  if (!col.width) return '';

  // To ensure the column number starts as in excel.
  const colIndex = index + 1;

  // Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi)
  // TODO make it configurable?
  const maximumDigitWidth = 6; // 6 is for "Calibri 12"

  // To translate from pixels to character width, use this calculation:
  // =Truncate(({pixels}-5)/{Maximum Digit Width} * 100+0.5)/100
  const characterWidth = Truncate((((col.width - 5) / maximumDigitWidth) * 100) + 0.5) / 100;

  // To translate from character width to real width, use this calculation:
  // =Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256
  const colWidth = Truncate((((characterWidth * maximumDigitWidth) + 5) / maximumDigitWidth) * 256) / 256;

  const style = col.style ? col.style : 1;

  // `col` format is described here: https://msdn.microsoft.com/en-us/library/office/documentformat.openxml.spreadsheet.column.aspx
  return `<col min="${colIndex}" max="${colIndex}" width="${colWidth}" style="${style}" />`;
};
