VLOOKUP.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. require __DIR__ . '/../../Header.php';
  4. $helper->log('Searches for a value in the top row of a table or an array of values,
  5. and then returns a value in the same column from a row you specify
  6. in the table or array.');
  7. // Create new PhpSpreadsheet object
  8. $spreadsheet = new Spreadsheet();
  9. $worksheet = $spreadsheet->getActiveSheet();
  10. $data = [
  11. ['ID', 'First Name', 'Last Name', 'Salary'],
  12. [72, 'Emily', 'Smith', 64901],
  13. [66, 'James', 'Anderson', 70855],
  14. [14, 'Mia', 'Clark', 188657],
  15. [30, 'John', 'Lewis', 97566],
  16. [53, 'Jessica', 'Walker', 58339],
  17. [56, 'Mark', 'Reed', 125180],
  18. [79, 'Richard', 'Lopez', 91632],
  19. ];
  20. $worksheet->fromArray($data, null, 'B2');
  21. $lookupFields = [
  22. ['ID', 53, 66, 56],
  23. ['Name'],
  24. ['Salary'],
  25. ];
  26. $worksheet->fromArray($lookupFields, null, 'G3');
  27. $worksheet->getCell('H4')->setValue('=VLOOKUP(H3, B3:E9, 2, FALSE) & " " & VLOOKUP(H3, B3:E9, 3, FALSE)');
  28. $worksheet->getCell('I4')->setValue('=VLOOKUP(I3, B3:E9, 2, FALSE) & " " & VLOOKUP(I3, B3:E9, 3, FALSE)');
  29. $worksheet->getCell('J4')->setValue('=VLOOKUP(J3, B3:E9, 2, FALSE) & " " & VLOOKUP(J3, B3:E9, 3, FALSE)');
  30. $worksheet->getCell('H5')->setValue('=VLOOKUP(H3, B3:E9, 4, FALSE)');
  31. $worksheet->getCell('I5')->setValue('=VLOOKUP(I3, B3:E9, 4, FALSE)');
  32. $worksheet->getCell('J5')->setValue('=VLOOKUP(J3, B3:E9, 4, FALSE)');
  33. for ($column = 'H'; $column !== 'K'; ++$column) {
  34. for ($row = 4; $row <= 5; ++$row) {
  35. $cell = $worksheet->getCell("{$column}{$row}");
  36. $helper->log("{$column}{$row}: {$cell->getValue()} => {$cell->getCalculatedValue()}");
  37. }
  38. }