Skip to content

Commit

Permalink
Merge pull request #158 from AmpersandTarski/feature/79-excel-import-…
Browse files Browse the repository at this point in the history
…multi-value-columns

Feature/79 excel import multi value columns
  • Loading branch information
Michiel-s authored Oct 4, 2023
2 parents aa8b4bd + f0d4163 commit 6a1b5bc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Given a version number MAJOR.MINOR.PATCH, increment the:
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format. In our case this is e.g. `-rc.1`, `-rc.2`.

## Unreleased changes
* [Issue 79](https://github.com/AmpersandTarski/prototype/issues/79) Add support for delimited multi value columns in excel importer
* Add codebase of new developed frontend. Not integrated in building image yet.

## v1.17.0 (24 may 2023)
Expand Down
31 changes: 28 additions & 3 deletions src/Ampersand/IO/ExcelImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ protected function parseBlock(Worksheet $worksheet, int $startRowNr, ?int $endRo
try {
$col = $cell->getColumn();
$line2[$col] = trim((string) $cell->getCalculatedValue()); // no leading/trailing spaces allowed

// Handle possibility that column contains multiple values to insert into the relation seperated by a delimiter
// The syntax to indicate this is: '[Concept,]' where in this example the comma ',' is the delimiter
// The square brackets are needed to indicate that this is a multi value column
$delimiter = null;
if (substr(
$line2[$col], 0, 1) === '['
&& substr($line2[$col], -1) === ']'
) {
$delimiter = substr($line2[$col], -2, 1);
$line2[$col] = substr($line2[$col], 1, strlen($line2[$col]) - 3);
}

// Import header can be determined now using line 1 and line 2
if ($col === 'A') {
Expand All @@ -247,12 +259,14 @@ protected function parseBlock(Worksheet $worksheet, int $startRowNr, ?int $endRo
$header[$col] = ['concept' => $rightConcept
,'relation' => $this->ampersandApp->getRelation(substr($line1[$col], 0, -1), $rightConcept, $leftConcept) // @phan-suppress-current-line PhanTypeInvalidDimOffset
,'flipped' => true
,'delimiter' => $delimiter
];
} else {
$rightConcept = $this->ampersandApp->getModel()->getConceptByLabel($line2[$col]);
$header[$col] = ['concept' => $rightConcept
,'relation' => $this->ampersandApp->getRelation($line1[$col], $leftConcept, $rightConcept) // @phan-suppress-current-line PhanTypeInvalidDimOffset
,'flipped' => false
,'delimiter' => $delimiter
];
}
}
Expand Down Expand Up @@ -297,6 +311,7 @@ protected function processDataRow(Atom $leftAtom, Row $row, array $headerInfo):
foreach ($row->getCellIterator('B') as $cell) {
try {
$col = $cell->getColumn();
$rightAtoms = [];

// Skip cell if column must not be imported
if (!array_key_exists($col, $headerInfo)) {
Expand All @@ -309,12 +324,22 @@ protected function processDataRow(Atom $leftAtom, Row $row, array $headerInfo):
if ($cellvalue === '') {
continue; // continue to next cell
} elseif ($cellvalue === '_NEW') {
$rightAtom = $leftAtom;
$rightAtoms[] = $leftAtom;
} else {
$rightAtom = (new Atom($cellvalue, $headerInfo[$col]['concept']))->add();
if (is_null($headerInfo[$col]['delimiter'])) {
$rightAtoms[] = new Atom($cellvalue, $headerInfo[$col]['concept']);
// Handle case with delimited multi values
} else {
foreach (explode($headerInfo[$col]['delimiter'], $cellvalue) as $value) {
$rightAtoms[] = new Atom($value, $headerInfo[$col]['concept']);
}
}
}

$leftAtom->link($rightAtom, $headerInfo[$col]['relation'], $headerInfo[$col]['flipped'])->add();
foreach ($rightAtoms as $rightAtom) {
$rightAtom->add();
$leftAtom->link($rightAtom, $headerInfo[$col]['relation'], $headerInfo[$col]['flipped'])->add();
}
} catch (Exception $e) {
$this->throwException($e, $cell);
}
Expand Down

0 comments on commit 6a1b5bc

Please sign in to comment.