Skip to content

Commit

Permalink
#46 - handle more cell errors in both text and image cells
Browse files Browse the repository at this point in the history
  • Loading branch information
GrandyB committed Feb 1, 2023
1 parent ad6acdf commit c6a5edb
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/main/java/application/services/FileUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ public void updateFile(CellWrapper cellWrapper, String newValue) throws Exceptio
FileExtension ext = cellWrapper.getFileExtension();
switch (ext.getType()) {
case TEXT:
/** Add padding if applicable (@see {@link CellWrapper#getPadding}). */
fileIO.writeTextFile(destFilePath, cellWrapper.getPadding().insert(0, newValue).toString());
/**
* Add padding if applicable (@see {@link CellWrapper#getPadding}), and replace
* GSheet errors with blank values.
*/
fileIO.writeTextFile(destFilePath,
cellWrapper.getPadding().insert(0, isGoogleSheetErrorCode(newValue) ? "" : newValue).toString());
break;
case IMAGE:
updateImage(newValue, destFilePath, ext.getExtension());
Expand All @@ -116,7 +120,7 @@ private void updateImage(String newValue, String destFilePath, String ext) throw
try {
fileIO.downloadAndConvertImage(newValue, destFilePath, ext);
} catch (MalformedURLException e) {
if (newValue.trim().isEmpty() || "#n/a".equals(newValue.trim().toLowerCase())) {
if (newValue.trim().isEmpty() || isGoogleSheetErrorCode(newValue)) {
// URL isn't a link, but be lenient
// Log it but don't throw it in the user's face; create an empty image
LOGGER.warn("URL found in cell was '{}' - permitting it, but outputting a transparent image", newValue);
Expand All @@ -134,6 +138,22 @@ private void updateImage(String newValue, String destFilePath, String ext) throw
}
}

private boolean isGoogleSheetErrorCode(String value) {
switch (value.trim().toLowerCase()) {
case "#n/a":
case "#div/0!":
case "#name?":
case "#null!":
case "#num!":
case "#ref!":
case "#value!":
case "#error!":
return true;
default:
return false;
}
}

private void updateVideo(String newValue, String destFilePath, String ext) throws Exception {
try {
fileIO.downloadAndSaveFile(newValue, destFilePath, ext);
Expand Down

0 comments on commit c6a5edb

Please sign in to comment.