@@ -52,9 +52,33 @@ pub fn read(bytes: &[u8], name: &str, sheet: Option<&str>, opts: &CsvOptions) ->
5252 let g = grid ( bytes, sheet) ?;
5353 let title = if g. names . len ( ) > 1 { format ! ( "{name} {}" , g. pick) } else { name. to_string ( ) } ;
5454 let ( ds, mut report) = read_rows ( & g. headers , & g. rows , 0 , & title, opts, Some ( ( & g. dates , & g. phantom_dates ) ) ) ?;
55+ let errors: usize = g. error_cells . iter ( ) . sum ( ) ;
5556 let rounded: usize = g. rounded_dates . iter ( ) . sum ( ) ;
57+ let fields: Vec < Field > =
58+ if errors > 0 || rounded > 0 { g. headers . iter ( ) . map ( |h| Field :: new ( h, FieldType :: Text ) ) . collect ( ) } else { vec ! [ ] } ;
59+ if errors > 0 {
60+ let what = if errors == 1 { "1 spreadsheet error cell was" . to_string ( ) } else { format ! ( "{errors} spreadsheet error cells were" ) } ;
61+ let mut message = format ! ( "{what} read as empty, in {}" , crate :: io:: named_fields( & fields, & g. error_cells) ) ;
62+ let headings: usize = g. header_error_cells . iter ( ) . sum ( ) ;
63+ if headings > 0 {
64+ let what = if headings == 1 {
65+ "1 erroneous heading was named by its column letter" . to_string ( )
66+ } else {
67+ format ! ( "{headings} erroneous headings were named by their column letters" )
68+ } ;
69+ message. push_str ( & format ! ( "; {what}, {}" , crate :: io:: named_fields( & fields, & g. header_error_cells) ) ) ;
70+ }
71+ if g. empty_error_rows > 0 {
72+ let what = if g. empty_error_rows == 1 {
73+ "1 otherwise empty row with error cells was omitted" . to_string ( )
74+ } else {
75+ format ! ( "{} otherwise empty rows with error cells were omitted" , g. empty_error_rows)
76+ } ;
77+ message. push_str ( & format ! ( "; {what}" ) ) ;
78+ }
79+ report. messages . push ( message) ;
80+ }
5681 if rounded > 0 {
57- let fields: Vec < Field > = g. headers . iter ( ) . map ( |h| Field :: new ( h, FieldType :: Text ) ) . collect ( ) ;
5882 report. messages . push ( format ! (
5983 "{rounded} serial date values were rounded to the nearest microsecond, in {}" ,
6084 crate :: io:: named_fields( & fields, & g. rounded_dates)
@@ -82,6 +106,12 @@ struct Grid {
82106 rows : Vec < Vec < String > > ,
83107 dates : Vec < Option < FieldType > > ,
84108 rounded_dates : Vec < usize > ,
109+ /// Source cells whose spreadsheet error value became empty text, per source column.
110+ error_cells : Vec < usize > ,
111+ /// Error-valued headings replaced with column letters, per source column.
112+ header_error_cells : Vec < usize > ,
113+ /// Otherwise empty rows with errors, omitted by the existing blank-row rule.
114+ empty_error_rows : usize ,
85115 /// Per column that isn't one of dates, the serial-60 cells read as empty.
86116 phantom_dates : Vec < usize > ,
87117}
@@ -173,10 +203,20 @@ fn grid_unguarded(mut wb: Sheets<Cursor<&[u8]>>, sheet: Option<&str>) -> Result<
173203 // as one that isn't a date (null, and counted); in a column of other things it is read as
174204 // empty and counted here, as it would not be text of a day.
175205 let mut phantom_dates = vec ! [ 0usize ; w] ;
176- let mut rows = range. rows ( ) . map ( |r| {
177- r. iter ( )
206+ let mut error_cells = vec ! [ 0usize ; w] ;
207+ let mut rows = range. rows ( ) . enumerate ( ) . map ( |( row, r) | {
208+ let mut had_error = false ;
209+ let cells = r
210+ . iter ( )
178211 . enumerate ( )
179212 . map ( |( k, d) | match ( d, dates. get ( k) ) {
213+ ( Data :: Error ( _) , _) => {
214+ had_error = true ;
215+ if let Some ( n) = error_cells. get_mut ( k) {
216+ * n += 1 ;
217+ }
218+ String :: new ( )
219+ }
180220 ( Data :: DateTime ( t) , Some ( None ) ) if is_phantom_day ( t) => {
181221 if let Some ( n) = phantom_dates. get_mut ( k) {
182222 * n += 1 ;
@@ -185,23 +225,41 @@ fn grid_unguarded(mut wb: Sheets<Cursor<&[u8]>>, sheet: Option<&str>) -> Result<
185225 }
186226 _ => cell ( d) ,
187227 } )
188- . collect :: < Vec < String > > ( )
228+ . collect :: < Vec < String > > ( ) ;
229+ ( row, cells, had_error)
189230 } ) ;
231+ let mut empty_error_rows = 0 ;
232+ let mut keep = |( _, cells, had_error) : & ( usize , Vec < String > , bool ) | {
233+ let nonempty = cells. iter ( ) . any ( |c| !c. trim ( ) . is_empty ( ) ) ;
234+ if !nonempty && * had_error {
235+ empty_error_rows += 1 ;
236+ }
237+ nonempty
238+ } ;
190239 // The first row with anything in it names the columns.
191- let headers: Vec < String > = rows. by_ref ( ) . find ( |r| r . iter ( ) . any ( |c| !c . trim ( ) . is_empty ( ) ) ) . unwrap_or_default ( ) ;
240+ let ( header_row , headers, _ ) = rows. by_ref ( ) . find ( & mut keep ) . unwrap_or_default ( ) ;
192241 if headers. is_empty ( ) {
193242 return Err ( DataError :: Format ( format ! ( "sheet '{pick}' is empty" ) ) ) ;
194243 }
195244 // Columns without a heading are named by their letter, as the spreadsheet shows them (the
196245 // range starts at its first column with anything in it).
197246 let first_col = range. start ( ) . map ( |( _, c) | c as usize ) . unwrap_or ( 0 ) ;
247+ let source_header = range. rows ( ) . nth ( header_row) ;
248+ let mut header_error_cells = vec ! [ 0usize ; w] ;
198249 let headers: Vec < String > = headers
199250 . into_iter ( )
200251 . enumerate ( )
201- . map ( |( i, h) | if h. trim ( ) . is_empty ( ) { column_letter ( first_col + i) } else { h. trim ( ) . to_string ( ) } )
252+ . map ( |( i, h) | {
253+ if matches ! ( source_header. and_then( |r| r. get( i) ) , Some ( Data :: Error ( _) ) )
254+ && let Some ( n) = header_error_cells. get_mut ( i)
255+ {
256+ * n = 1 ;
257+ }
258+ if h. trim ( ) . is_empty ( ) { column_letter ( first_col + i) } else { h. trim ( ) . to_string ( ) }
259+ } )
202260 . collect ( ) ;
203- let rows: Vec < Vec < String > > = rows. filter ( |r| r . iter ( ) . any ( |c| !c . trim ( ) . is_empty ( ) ) ) . collect ( ) ;
204- Ok ( Grid { names, pick, headers, rows, dates, rounded_dates, phantom_dates } )
261+ let rows = rows. filter ( keep ) . map ( | ( _ , cells , _ ) | cells ) . collect ( ) ;
262+ Ok ( Grid { names, pick, headers, rows, dates, rounded_dates, error_cells , header_error_cells , empty_error_rows , phantom_dates } )
205263}
206264
207265/// Check every ZIP member before calamine opens it, without suffix or case filters. ODS
@@ -747,6 +805,57 @@ mod tests {
747805 assert ! ( !report. messages. iter( ) . any( |m| m. contains( "isn't a date" ) ) , "{:?}" , report. messages) ;
748806 }
749807
808+ /// Multiple errors in one column and another column count once each; blank cells don't.
809+ #[ test]
810+ fn error_cells_are_counted_without_changing_other_values ( ) {
811+ let error = r#"<c t="e"><v>#DIV/0!</v></c>"# ;
812+ let book = xlsx ( & [ ( "S" , & [ & [ "id" , "note" , "other" ] , & [ "7" , error, "kept" ] , & [ "8" , error, error] , & [ "9" , "" , "" ] ] ) ] ) ;
813+ let ( ds, report) = read ( & book, "book" , None , & CsvOptions :: default ( ) ) . unwrap ( ) ;
814+ assert_eq ! ( report. messages, [ "3 spreadsheet error cells were read as empty, in 'note', 'other'" ] ) ;
815+ assert_eq ! ( report. skipped, 0 ) ;
816+ let attrs: Vec < _ > = ds. features . iter ( ) . map ( |f| f. attrs . clone ( ) ) . collect ( ) ;
817+ use crate :: Value :: { Int , Null , Text } ;
818+ assert_eq ! ( attrs, [ vec![ Int ( 7 ) , Null , Text ( "kept" . into( ) ) ] , vec![ Int ( 8 ) , Null , Null ] , vec![ Int ( 9 ) , Null , Null ] ] ) ;
819+ }
820+
821+ /// Error headings still use column letters, with the lost heading counted separately.
822+ #[ test]
823+ fn spreadsheet_error_headings_are_reported ( ) {
824+ let error = r#"<c t="e"><v>#REF!</v></c>"# ;
825+ for heading in [ "" , error] {
826+ let book = xlsx ( & [ ( "S" , & [ & [ "id" , heading] , & [ "7" , "kept" ] ] ) ] ) ;
827+ let ( ds, report) = read ( & book, "book" , None , & CsvOptions :: default ( ) ) . unwrap ( ) ;
828+ assert_eq ! ( ds. fields. iter( ) . map( |f| f. name. as_str( ) ) . collect:: <Vec <_>>( ) , [ "id" , "B" ] ) ;
829+ assert_eq ! ( ds. features[ 0 ] . attrs, [ crate :: Value :: Int ( 7 ) , crate :: Value :: Text ( "kept" . into( ) ) ] ) ;
830+ let expected = if heading. is_empty ( ) {
831+ vec ! [ ]
832+ } else {
833+ vec ! [ "1 spreadsheet error cell was read as empty, in 'B'; 1 erroneous heading was named by its column letter, 'B'" ]
834+ } ;
835+ assert_eq ! ( report. messages, expected) ;
836+ }
837+ }
838+
839+ /// Error-only rows and genuinely blank rows keep their omission behavior, but only the
840+ /// former lose source content and need a counted notice.
841+ #[ test]
842+ fn spreadsheet_error_only_rows_are_counted_when_omitted ( ) {
843+ let error = r#"<c t="e"><v>#N/A</v></c>"# ;
844+ for cell in [ "" , error] {
845+ let book = xlsx ( & [ ( "S" , & [ & [ "id" , "note" ] , & [ "7" , "kept" ] , & [ cell, cell] , & [ "" , "" ] , & [ "8" , "" ] ] ) ] ) ;
846+ let ( ds, report) = read ( & book, "book" , None , & CsvOptions :: default ( ) ) . unwrap ( ) ;
847+ let attrs: Vec < _ > = ds. features . iter ( ) . map ( |f| f. attrs . clone ( ) ) . collect ( ) ;
848+ use crate :: Value :: { Int , Null , Text } ;
849+ assert_eq ! ( attrs, [ vec![ Int ( 7 ) , Text ( "kept" . into( ) ) ] , vec![ Int ( 8 ) , Null ] ] ) ;
850+ let expected = if cell. is_empty ( ) {
851+ vec ! [ ]
852+ } else {
853+ vec ! [ "2 spreadsheet error cells were read as empty, in 'id', 'note'; 1 otherwise empty row with error cells was omitted" ]
854+ } ;
855+ assert_eq ! ( report. messages, expected) ;
856+ }
857+ }
858+
750859 #[ test]
751860 fn reads_sheets ( ) {
752861 let book = xlsx ( & [
0 commit comments