11from Standard.Base import all
22import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
3+ import Standard.Base.Metadata.Display
34import Standard.Base.System.File.Generic.Writable_File.Writable_File
45import Standard.Base.System.File_Format_Metadata.File_Format_Metadata
56import 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
69
710import project.Data.Match_Columns.Match_Columns
811import 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
1014import project.Excel.Excel_Workbook.Excel_Workbook
1115import project.Internal.Excel_Reader
1216import project.Internal.Excel_Writer
@@ -22,52 +26,87 @@ should_treat_as_xls_format xls_format file =
2226
2327## Read the file to a `Table` from an Excel file
2428type Excel_Format
25- ## Read Excels files into a Table or Vector .
29+ ## Reads an Excel file as a connection to an `Excel_Workbook` .
2630
2731 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).
3564 - headers: If set to `True`, the first row is used as column names. If
3665 set to `False`, the column names are Excel column names. If set to
3766 `Infer`, the process tries to infer if headers are present on the first
3867 row. If the column names are not unique, numeric suffixes will be
3968 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.
4072 - xls_format:
4173 If set to `True`, the file is read as an Excel 95-2003 format.
4274 If set to `False`, the file is read as an Excel 2007+ format.
4375 `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)
4778
4879 ## PRIVATE
4980 ADVANCED
5081 If the File_Format supports reading from the file, return a configured instance.
5182 for_read : File_Format_Metadata -> Excel_Format | Nothing
5283 for_read file:File_Format_Metadata =
5384 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
5686
5787 ## PRIVATE
5888 If this File_Format should be used for writing to that file, return a configured instance.
5989 for_file_write : Writable_File -> Excel_Format | Nothing
6090 for_file_write file = Excel_Format.for_read file
6191
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+
62101 ## PRIVATE
63102 ADVANCED
64103 Implements the `File.read` for this `File_Format`
65104 read : File -> Problem_Behavior -> Any
66105 read self file on_problems =
67106 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
71110
72111 ## PRIVATE
73112 Implements decoding the format from a stream.
@@ -88,8 +127,10 @@ type Excel_Format
88127 Error.throw (Illegal_Argument.Error message)
89128
90129 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)
93134
94135 ## PRIVATE
95136 ADVANCED
@@ -114,13 +155,7 @@ type Excel_Format
114155 write_table : File -> Table -> Existing_File_Behavior -> Match_Columns -> Problem_Behavior -> File
115156 write_table self file table on_existing_file match_columns on_problems =
116157 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
124159
125160## PRIVATE
126161 This function returns:
@@ -142,3 +177,15 @@ xls_format_from_metadata (metadata : File_Format_Metadata) =
142177 ".xls" -> True
143178 ".xlt" -> True
144179 _ -> 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