@@ -754,17 +754,27 @@ create_tbl <- function(doc, cols) {
754754 sheets_to_error <- mismatch_per_sheet %> %
755755 keep(names(. ) %in% closest_sheets )
756756
757- # Write out the error
758- sheets_to_error %> %
759- map2_chr(names(sheets_to_error ), function (vars , sheet_name ) {
760- paste0(
761- " Sheet '" , sheet_name , " ' is the closest match, but unable to match the following column(s)\n " ,
762- paste(names(vars ), collapse = " \n " )
763- )
764- }) %> %
765- paste0(collapse = " \n " ) %> %
766- paste0(" Unable to identify a sheet with all columns.\n " , . ) %> %
767- (call. <- FALSE )
757+ # 1. Check for "where" columns
758+ has_where_col <- sheets_to_error %> %
759+ map_lgl(~ any(stringr :: str_detect(names(.x ), stringr :: regex(" ^where" , ignore_case = TRUE )))) %> %
760+ any()
761+
762+ # 2. Generate the formatted list of sheet matches
763+ sheet_details <- sheets_to_error %> %
764+ purrr :: imap_chr(~ {
765+ paste0(" Sheet '" , .y , " ' is missing: " , paste(names(.x ), collapse = " , " ))
766+ })
767+
768+ # 3. Use cli_abort with bullets to replace the standard Error: prefix
769+ cli_abort(
770+ c(
771+ " x" = " Unable to identify a sheet with all columns." ,
772+ " i" = " Closest matches identified:" ,
773+ " *" = sheet_details ,
774+ if (has_where_col ) c(" !" = " Tip: A 'where' column was detected. Check if {.arg where_sep_sheet} is set correctly." )
775+ ),
776+ call = NULL
777+ )
768778 } else if (length(matches ) == 1 ) {
769779 # Check names and write a better warning message if names don't work
770780 ds_nm <- matches [[1 ]] %> % names()
0 commit comments