1
1
from Standard.Base import all
2
2
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
3
+ import Standard.Base.Metadata.Display
3
4
import Standard.Base.System.File.Generic.Writable_File.Writable_File
4
5
import Standard.Base.System.File_Format_Metadata.File_Format_Metadata
5
6
import Standard.Base.System.Input_Stream.Input_Stream
7
+ from Standard.Base.Metadata.Choice import Option
8
+ from Standard.Base.Metadata.Widget import Text_Input, Numeric_Input
6
9
7
10
import project.Data.Match_Columns.Match_Columns
8
11
import project.Data.Table.Table
9
- import project.Excel.Excel_Section.Excel_Section
12
+ import project.Excel.Excel_Range.Excel_Range
13
+ import project.Internal.Excel_Section.Excel_Section
10
14
import project.Excel.Excel_Workbook.Excel_Workbook
11
15
import project.Internal.Excel_Reader
12
16
import project.Internal.Excel_Writer
@@ -22,52 +26,87 @@ should_treat_as_xls_format xls_format file =
22
26
23
27
## Read the file to a `Table` from an Excel file
24
28
type Excel_Format
25
- ## Read Excels files into a Table or Vector .
29
+ ## Reads an Excel file as a connection to an `Excel_Workbook` .
26
30
27
31
Arguments:
28
- - section: The `Excel_Section` to read from the workbook.
29
- This can be one of:
30
- - `Workbook` - open the workbook as a connection.
31
- - `Sheet_Names` - outputs a `Vector` of sheet names.
32
- - `Range_Names` - outputs a `Vector` of range names.
33
- - `Worksheet` - outputs a `Table` containing the specified sheet.
34
- - `Cell_Range` - outputs a `Table` containing the specified range.
32
+ - xls_format:
33
+ If set to `True`, the file is read as an Excel 95-2003 format.
34
+ If set to `False`, the file is read as an Excel 2007+ format.
35
+ `Infer` will attempt to deduce this from the extension of the filename.
36
+ - default_sheet: The default sheet to use when writing a table to a file
37
+ with these format settings.
38
+ @default_sheet Text_Input
39
+ Workbook (xls_format : Boolean | Infer = Infer) (default_sheet : Text = "EnsoSheet")
40
+
41
+ ## Reads a sheet from an Excel file as a `Table`.
42
+
43
+ Arguments:
44
+ - sheet: The sheet number or name.
45
+ - headers: If set to `True`, the first row is used as column names. If
46
+ set to `False`, the column names are Excel column names. If set to
47
+ `Infer`, the process tries to infer if headers are present on the first
48
+ row. If the column names are not unique, numeric suffixes will be
49
+ appended to disambiguate them.
50
+ - skip_rows: The number of rows to skip before reading the data.
51
+ - row_limit: The maximum number of rows to read. If set to `Nothing`, all
52
+ rows are read.
53
+ - xls_format:
54
+ If set to `True`, the file is read as an Excel 95-2003 format.
55
+ If set to `False`, the file is read as an Excel 2007+ format.
56
+ `Infer` will attempt to deduce this from the extension of the filename.
57
+ @sheet (Text_Input display=Display.Always)
58
+ Sheet (sheet:(Integer|Text)=1) (headers:(Boolean|Infer)=Infer) (skip_rows:Integer=0) (row_limit:(Integer|Nothing)=Nothing) (xls_format:Boolean|Infer=Infer)
59
+
60
+ ## Reads a range from an Excel file as a `Table`.
61
+
62
+ Arguments:
63
+ - address: A name of a range or an Excel-style address (e.g. Sheet1!A1:B2).
35
64
- headers: If set to `True`, the first row is used as column names. If
36
65
set to `False`, the column names are Excel column names. If set to
37
66
`Infer`, the process tries to infer if headers are present on the first
38
67
row. If the column names are not unique, numeric suffixes will be
39
68
appended to disambiguate them.
69
+ - skip_rows: The number of rows to skip before reading the data.
70
+ - row_limit: The maximum number of rows to read. If set to `Nothing`, all
71
+ rows are read.
40
72
- xls_format:
41
73
If set to `True`, the file is read as an Excel 95-2003 format.
42
74
If set to `False`, the file is read as an Excel 2007+ format.
43
75
`Infer` will attempt to deduce this from the extension of the filename.
44
- - default_sheet: The default sheet to use if `section` is set to
45
- `Excel_Section.Workbook`.
46
- Excel (section:Excel_Section=Excel_Section.Workbook) (headers:(Boolean|Infer)=Infer) (xls_format:(Boolean|Infer)=Infer) (default_sheet:Text="EnsoSheet")
76
+ @address Text_Input
77
+ Range (address:(Text|Excel_Range)) (headers:(Boolean|Infer)=Infer) (skip_rows:Integer=0) (row_limit:(Integer|Nothing)=Nothing) (xls_format : Boolean | Infer = Infer)
47
78
48
79
## PRIVATE
49
80
ADVANCED
50
81
If the File_Format supports reading from the file, return a configured instance.
51
82
for_read : File_Format_Metadata -> Excel_Format | Nothing
52
83
for_read file:File_Format_Metadata =
53
84
is_xls = xls_format_from_metadata file
54
- is_xls.if_not_nothing <|
55
- Excel_Format.Excel xls_format=is_xls
85
+ is_xls.if_not_nothing <| Excel_Format.Workbook xls_format=is_xls
56
86
57
87
## PRIVATE
58
88
If this File_Format should be used for writing to that file, return a configured instance.
59
89
for_file_write : Writable_File -> Excel_Format | Nothing
60
90
for_file_write file = Excel_Format.for_read file
61
91
92
+ ## PRIVATE
93
+ get_dropdown_options : Vector Option
94
+ get_dropdown_options =
95
+ fqn = Meta.get_qualified_type_name Excel_Format
96
+ workbook = Option "Excel Workbook" fqn+".Workbook"
97
+ sheet = Option "Excel Sheet" fqn+".Sheet"
98
+ range = Option "Excel Range" fqn+".Range"
99
+ [workbook, sheet, range]
100
+
62
101
## PRIVATE
63
102
ADVANCED
64
103
Implements the `File.read` for this `File_Format`
65
104
read : File -> Problem_Behavior -> Any
66
105
read self file on_problems =
67
106
format = should_treat_as_xls_format self.xls_format file
68
- case self.section of
69
- Excel_Section .Workbook -> Excel_Workbook.new file format self.headers
70
- _ -> Excel_Reader.read_file file self.section self.headers on_problems format
107
+ case self of
108
+ Excel_Format .Workbook _ _ -> Excel_Workbook.new file format
109
+ _ -> Excel_Reader.read_file file (as_section self) on_problems format
71
110
72
111
## PRIVATE
73
112
Implements decoding the format from a stream.
@@ -88,8 +127,10 @@ type Excel_Format
88
127
Error.throw (Illegal_Argument.Error message)
89
128
90
129
Excel_Reader.handle_bad_format_with_handler bad_format <|
91
- workbook = Excel_Workbook.from_stream stream xls_format self.headers
92
- workbook.read_section self.section
130
+ workbook = Excel_Workbook.from_stream stream xls_format
131
+ case self of
132
+ Excel_Format.Workbook _ _ -> workbook
133
+ _ -> workbook.read_section (as_section self)
93
134
94
135
## PRIVATE
95
136
ADVANCED
@@ -114,13 +155,7 @@ type Excel_Format
114
155
write_table : File -> Table -> Existing_File_Behavior -> Match_Columns -> Problem_Behavior -> File
115
156
write_table self file table on_existing_file match_columns on_problems =
116
157
format = should_treat_as_xls_format self.xls_format file
117
-
118
- case self.section of
119
- Excel_Section.Sheet_Names -> Error.throw (Illegal_Argument.Error "Sheet_Names cannot be used for `write`.")
120
- Excel_Section.Range_Names -> Error.throw (Illegal_Argument.Error "Range_Names cannot be used for `write`.")
121
- Excel_Section.Workbook ->
122
- Excel_Writer.write_file file table on_existing_file (Excel_Section.Worksheet self.default_sheet) True match_columns on_problems format
123
- _ -> Excel_Writer.write_file file table on_existing_file self.section self.headers match_columns on_problems format
158
+ Excel_Writer.write_file file table on_existing_file (as_section self) match_columns on_problems format
124
159
125
160
## PRIVATE
126
161
This function returns:
@@ -142,3 +177,15 @@ xls_format_from_metadata (metadata : File_Format_Metadata) =
142
177
".xls" -> True
143
178
".xlt" -> True
144
179
_ -> Nothing
180
+
181
+ ## PRIVATE
182
+ Converts this format to a corresponding `Excel_Section`.
183
+ as_section (format : Excel_Format) -> Excel_Section = case format of
184
+ Excel_Format.Workbook _ default_sheet ->
185
+ ## This case is only used for when writing to the workbook.
186
+ This results in writing to the default sheet, and we always write headers (ignored with append).
187
+ Excel_Section.Worksheet default_sheet headers=True
188
+ Excel_Format.Sheet sheet headers skip_rows row_limit _ ->
189
+ Excel_Section.Worksheet sheet headers skip_rows row_limit
190
+ Excel_Format.Range address headers skip_rows row_limit _ ->
191
+ Excel_Section.Cell_Range address headers skip_rows row_limit
0 commit comments