Skip to content

Commit

Permalink
Report error when table has multiple columns with the same name (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfbvs committed Feb 19, 2021
1 parent 0de5383 commit 65adcbe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
31 changes: 25 additions & 6 deletions src/amplxl/src/ampl_xl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,7 @@ ExcelReadManager::manage_data(){
// check the headers of the table and adjust last_col
result = check_columns(node, first_row, first_col, last_col);

if (result == -2){
msg = "Could not parse header";
logger.log(msg, LOG_ERROR);
if (result == -2 || result == -3){
return 1;
}
else if (result != -1){
Expand Down Expand Up @@ -1017,9 +1015,16 @@ ExcelManager::check_columns(
){
// get the columns names in the row
std::map<std::string, std::string> excel_col_map;
parse_header(first_col, last_col, first_row, node, excel_col_map);
int res = parse_header(first_col, last_col, first_row, node, excel_col_map);

if (res){
// error in logger
return -3;
}

if (excel_col_map.size() == 0){
std::string msg = "Parse header found 0 columns.";
logger.log(msg, LOG_ERROR);
return -2;
}

Expand Down Expand Up @@ -3922,7 +3927,7 @@ write_indexing_sets(



void
int
ExcelManager::parse_header(

const std::string & first_col,
Expand Down Expand Up @@ -3968,8 +3973,21 @@ ExcelManager::parse_header(
if (is_numeric){
xl_col_name = "ampl-numeric-" + xl_col_name;
}

if (xl_col_map.find(xl_col_name) != xl_col_map.end()){
std::string msg = "Column \'" + xl_col_name;
msg += "\' at cell \'";
msg += iter_col;
msg += row_id;
msg += "\' already defined at cell \'";
msg += xl_col_map[xl_col_name];
msg += row_id;
msg += "\'";
logger.log(msg, LOG_ERROR);
return 1;
}

xl_col_map[xl_col_name] = iter_col;
//~ nempty = 0;

if (verbose == 73){
printf("Found column %s\n", xl_col_name.c_str());
Expand All @@ -3996,6 +4014,7 @@ ExcelManager::parse_header(

ecm.next(iter_col);
}
return 0;
};


Expand Down
4 changes: 3 additions & 1 deletion src/amplxl/src/ampl_xl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ class ExcelManager
// node is an xml node that holds the spreadsheet rows as children
// remaining parameters define the bounds of the header in the table
// returns -1 if all columns are found or the index of the missing column
// return -2 if no columns found
// return -3 if a duplicate column is found
int check_columns(
const pugi::xml_node &node,
const int first_row,
Expand Down Expand Up @@ -321,7 +323,7 @@ class ExcelManager
);


void
int
parse_header(
const std::string & first_col,
std::string & last_col,
Expand Down

0 comments on commit 65adcbe

Please sign in to comment.