Small fixes.. (#11338)

- Handle calculated header rows in Excel.
![image](https://github.com/user-attachments/assets/e0b307b1-90c2-435b-8d78-9e1b0e8d3932)
- Better reporting of Incomparable Values (bug fix in handler, add catch to add_row_number and running).
![image](https://github.com/user-attachments/assets/9d2ee953-ae5f-45f3-b3fa-6d593529bfc9)
- Remove default from `tokenize` as it was generating hundreds of rows.
- Added error to `Data.read` if no path provided.
![image](https://github.com/user-attachments/assets/71c8cd5f-ec40-4d8c-9972-94aa6fb9d3de)
This commit is contained in:
James Dunkerley 2024-10-16 18:31:26 +01:00 committed by GitHub
parent fb821303ab
commit 96fa2ee35a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 52 additions and 22 deletions

View File

@ -6,6 +6,7 @@ import project.Data.Vector.Vector
import project.Enso_Cloud.Data_Link.Data_Link
import project.Enso_Cloud.Data_Link_Helpers
import project.Error.Error
import project.Errors.Common.Missing_Argument
import project.Errors.File_Error.File_Error
import project.Errors.Illegal_Argument.Illegal_Argument
import project.Errors.Problem_Behavior.Problem_Behavior
@ -68,7 +69,7 @@ from project.System.File_Format import Auto_Detect, File_Format
@path Text_Input
@format File_Format.default_widget
read : Text | URI | File -> File_Format -> Problem_Behavior -> Any ! File_Error
read path format=Auto_Detect (on_problems : Problem_Behavior = ..Report_Warning) = case path of
read path=(Missing_Argument.throw "path") format=Auto_Detect (on_problems : Problem_Behavior = ..Report_Warning) = case path of
_ : Text -> if Data_Read_Helpers.looks_like_uri path then Data_Read_Helpers.fetch_following_data_links path format=format else
read (File.new path) format on_problems
uri : URI -> Data_Read_Helpers.fetch_following_data_links uri format=format
@ -103,7 +104,7 @@ read path format=Auto_Detect (on_problems : Problem_Behavior = ..Report_Warning)
@path Text_Input
@encoding Encoding.default_widget
read_text : (Text | File) -> Encoding -> Problem_Behavior -> Text
read_text path (encoding : Encoding = Encoding.default) (on_problems : Problem_Behavior = ..Report_Warning) =
read_text path=(Missing_Argument.throw "path") (encoding : Encoding = Encoding.default) (on_problems : Problem_Behavior = ..Report_Warning) =
File.new path . read_text encoding on_problems
## GROUP Input
@ -198,7 +199,7 @@ list (directory:(Text | File)=enso_project.root) (name_filter:Text="") recursive
@format Data_Read_Helpers.format_widget_with_raw_response
@headers Header.default_widget
fetch : (URI | Text) -> HTTP_Method -> Vector (Header | Pair Text Text) -> File_Format -> Any ! Request_Error | HTTP_Error
fetch (uri:(URI | Text)) (method:HTTP_Method=..Get) (headers:(Vector (Header | Pair Text Text))=[]) (format = Auto_Detect) =
fetch (uri:(URI | Text)=(Missing_Argument.throw "uri")) (method:HTTP_Method=..Get) (headers:(Vector (Header | Pair Text Text))=[]) (format = Auto_Detect) =
Data_Read_Helpers.fetch_following_data_links uri method headers (Data_Read_Helpers.handle_legacy_format "fetch" "format" format)
## ALIAS http post, upload
@ -325,7 +326,7 @@ fetch (uri:(URI | Text)) (method:HTTP_Method=..Get) (headers:(Vector (Header | P
@headers Header.default_widget
@response_format Data_Read_Helpers.format_widget_with_raw_response
post : (URI | Text) -> Request_Body -> HTTP_Method -> Vector (Header | Pair Text Text) -> File_Format -> Any ! Request_Error | HTTP_Error
post (uri:(URI | Text)) (body:Request_Body=..Empty) (method:HTTP_Method=..Post) (headers:(Vector (Header | Pair Text Text))=[]) (response_format = Auto_Detect) =
post (uri:(URI | Text)=(Missing_Argument.throw "uri")) (body:Request_Body=..Empty) (method:HTTP_Method=..Post) (headers:(Vector (Header | Pair Text Text))=[]) (response_format = Auto_Detect) =
response = HTTP.post uri body method headers
Data_Read_Helpers.decode_http_response_following_data_links response (Data_Read_Helpers.handle_legacy_format "post" "response_format" response_format)
@ -344,7 +345,7 @@ post (uri:(URI | Text)) (body:Request_Body=..Empty) (method:HTTP_Method=..Post)
@uri Text_Input
@headers Header.default_widget
download : (URI | Text) -> Writable_File -> HTTP_Method -> Vector (Header | Pair Text Text) -> File ! Request_Error | HTTP_Error
download (uri:(URI | Text)) file:Writable_File (method:HTTP_Method=..Get) (headers:(Vector (Header | Pair Text Text))=[]) =
download (uri:(URI | Text)=(Missing_Argument.throw "uri")) file:Writable_File (method:HTTP_Method=..Get) (headers:(Vector (Header | Pair Text Text))=[]) =
Context.Output.if_enabled disabled_message="As writing is disabled, cannot download to a file. Press the Write button ▶ to perform the operation." panic=False <|
response = HTTP.fetch uri method headers
case Data_Link.is_data_link response.body.metadata of

View File

@ -25,6 +25,7 @@ import project.Data.Time.Time_Zone.Time_Zone
import project.Data.Vector.Vector
import project.Error.Error
import project.Errors.Common.Index_Out_Of_Bounds
import project.Errors.Common.Missing_Argument
import project.Errors.Common.Syntax_Error
import project.Errors.Encoding_Error.Encoding_Error
import project.Errors.Illegal_Argument.Illegal_Argument
@ -452,7 +453,7 @@ Text.split self delimiter="," case_sensitivity=Case_Sensitivity.Sensitive use_re
'Hello Big\r\nWide\tWorld\nGoodbye!' . tokenize "(\S+)(?:\s+|$)"
== ["Hello","Big","Wide","World","Goodbye!"]
Text.tokenize : Text -> Case_Sensitivity -> Vector Text
Text.tokenize self pattern="." case_sensitivity=Case_Sensitivity.Sensitive =
Text.tokenize self pattern:Text=(Missing_Argument.throw "pattern") case_sensitivity:Case_Sensitivity=..Sensitive =
case_insensitive = case_sensitivity.is_case_insensitive_in_memory
compiled_pattern = Regex.compile pattern case_insensitive=case_insensitive
compiled_pattern.tokenize self

View File

@ -267,8 +267,11 @@ type Incomparable_Values
to_display_text : Text
to_display_text self =
case self.left.is_nothing && self.right.is_nothing of
True -> "Incomparable_Values"
False -> "Cannot compare `" + self.left.to_text + "` with `" + self.right.to_text + "`"
True -> "Could not compare values."
False ->
left_text = self.left.to_display_text + " (" + (Meta.type_of self.left).to_text + ")"
right_text = self.right.to_display_text + " (" + (Meta.type_of self.right).to_text + ")"
"Cannot compare " + left_text + " with " + right_text + "."
## PRIVATE
ADVANCED
@ -276,9 +279,9 @@ type Incomparable_Values
`Incomparable_Values` if any occur.
handle_errors ~function =
handle t = Panic.catch t handler=(_-> Error.throw (Incomparable_Values.Error Nothing Nothing))
handle_cmp_exc = Panic.catch CompareException handler=(exc-> Error.throw (Incomparable_Values.Error exc.getLeftOperand exc.getRightOperand))
handle_cmp_exc = Panic.catch CompareException handler=(exc-> Error.throw (Incomparable_Values.Error exc.payload.getLeftOperand exc.payload.getRightOperand))
handle ClassCastException <| handle No_Such_Method <| handle Type_Error <| handle Unsupported_Argument_Types <| handle_cmp_exc <|
handle Type_Error <| handle Unsupported_Argument_Types <| handle ClassCastException <| handle_cmp_exc <|
function
@Builtin_Type

View File

@ -2269,7 +2269,7 @@ type Table
@order_by (Widget_Helpers.make_order_by_selector display=..When_Modified)
add_row_number : Text -> Integer -> Integer -> Vector (Text | Integer | Regex) | Text | Integer | Regex -> Vector (Text | Sort_Column) | Text -> Problem_Behavior -> Table
add_row_number self (name:Text="Row") (from:Integer=0) (step:Integer=1) (group_by:(Vector | Text | Integer | Regex)=[]) (order_by:(Vector | Text)=[]) (on_problems:Problem_Behavior=..Report_Warning) =
Add_Row_Number.add_row_number self name from step group_by order_by on_problems
Incomparable_Values.handle_errors <| Add_Row_Number.add_row_number self name from step group_by order_by on_problems
## ALIAS add column, expression, formula, new column, update column
GROUP Standard.Base.Values
@ -3634,7 +3634,7 @@ type Table
@of Widget_Helpers.make_column_name_selector
running : Statistic -> (Text | Integer) -> Text -> Set_Mode -> Vector (Text | Integer | Regex) | Text | Integer | Regex -> Vector (Text | Sort_Column) | Text -> Problem_Behavior -> Table
running self (statistic:Statistic=..Count) (of:(Text | Integer)=0) (as:Text='') (set_mode:Set_Mode=..Add) (group_by:(Vector | Text | Integer | Regex)=[]) (order_by:(Vector | Text)=[]) (on_problems:Problem_Behavior=..Report_Warning) =
Add_Running.add_running self statistic of as set_mode group_by order_by on_problems
Incomparable_Values.handle_errors <| Add_Running.add_running self statistic of as set_mode group_by order_by on_problems
## PRIVATE
column_naming_helper : Column_Naming_Helper

View File

@ -1,7 +1,5 @@
package org.enso.table.excel;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.util.CellReference;
import org.enso.table.problems.ProblemAggregator;
import org.enso.table.util.NameDeduplicator;
@ -56,13 +54,10 @@ public class ExcelHeaders {
}
int currentEndCol = endCol == -1 ? row.getLastColumn() : endCol;
DataFormatter formatter = new DataFormatter();
String[] output = new String[currentEndCol - startCol + 1];
for (int col = startCol; col <= currentEndCol; col++) {
Cell cell = row.get(col);
String cellText = cell == null ? "" : formatter.formatCellValue(cell);
String cellText = row.getFormattedCell(col);
String name = cellText.isEmpty() ? "" : deduplicator.makeUnique(cellText);
output[col - startCol] = name;

View File

@ -2,14 +2,13 @@ package org.enso.table.excel;
import java.time.LocalDateTime;
import java.time.ZoneId;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.*;
import org.graalvm.polyglot.Context;
/** Wrapper class to handle Excel rows. */
public class ExcelRow {
private static final DataFormatter formatter = new DataFormatter();
private final Row row;
private final int firstColumn;
private final int lastColumn;
@ -111,6 +110,37 @@ public class ExcelRow {
return column;
}
/** Returns the formatted cell value. */
public String getFormattedCell(int col) {
var cell = get(col);
if (cell == null) {
return "";
}
var rawCellType = cell.getCellType();
var cellType =
rawCellType == CellType.FORMULA ? cell.getCachedFormulaResultType() : rawCellType;
return switch (cellType) {
case ERROR ->
// Want to show the error message rather than empty.
FormulaError.forInt(cell.getErrorCellValue()).getString();
case NUMERIC -> {
// Special handling for Number or Date cells as want to keep formatting.
var format = ExcelNumberFormat.from(cell, null);
var value = cell.getNumericCellValue();
yield format == null
? Double.toString(value)
: formatter.formatRawCellContents(value, format.getIdx(), format.getFormat());
}
default -> {
// Use the default read and then toString.
var value = getCellValue(col);
yield value == null ? "" : value.toString();
}
};
}
public String[] getCellsAsText(int startCol, int endCol) {
Context context = Context.getCurrent();
int currentEndCol = endCol == -1 ? getLastColumn() : endCol;