CONVERT.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. require __DIR__ . '/../../Header.php';
  4. $category = 'Engineering';
  5. $functionName = 'CONVERT';
  6. $description = 'Converts a number from one measurement system to another';
  7. $helper->titles($category, $functionName, $description);
  8. // Create new PhpSpreadsheet object
  9. $spreadsheet = new Spreadsheet();
  10. $worksheet = $spreadsheet->getActiveSheet();
  11. // Add some data
  12. $conversions = [
  13. [1, '"lbm"', '"kg"'],
  14. [1, '"gal"', '"l"'],
  15. [24, '"in"', '"ft"'],
  16. [100, '"yd"', '"m"'],
  17. [500, '"mi"', '"km"'],
  18. [7.5, '"min"', '"sec"'],
  19. [5, '"F"', '"C"'],
  20. [32, '"C"', '"K"'],
  21. [100, '"m2"', '"ft2"'],
  22. ];
  23. $testDataCount = count($conversions);
  24. $worksheet->fromArray($conversions, null, 'A1');
  25. for ($row = 1; $row <= $testDataCount; ++$row) {
  26. $worksheet->setCellValue('D' . $row, '=CONVERT(' . implode(',', $conversions[$row - 1]) . ')');
  27. }
  28. $worksheet->setCellValue('H1', '=CONVERT(CONVERT(100,"m","ft"),"m","ft")');
  29. for ($row = 1; $row <= $testDataCount; ++$row) {
  30. $helper->log(sprintf(
  31. '(A%d): Unit of Measure Conversion Formula %s - %d %s is %f %s',
  32. $row,
  33. $worksheet->getCell('D' . $row)->getValue(),
  34. $worksheet->getCell('A' . $row)->getValue(),
  35. trim($worksheet->getCell('B' . $row)->getValue(), '"'),
  36. $worksheet->getCell('D' . $row)->getCalculatedValue(),
  37. trim($worksheet->getCell('C' . $row)->getValue(), '"')
  38. ));
  39. }
  40. $helper->log('Old method for area conversions, before MS Excel introduced area Units of Measure');
  41. $helper->log(sprintf(
  42. '(A%d): Unit of Measure Conversion Formula %s result is %s',
  43. $row,
  44. $worksheet->getCell('H1')->getValue(),
  45. $worksheet->getCell('H1')->getCalculatedValue()
  46. ));