brechtsanders / xlsxio

XLSX I/O - C library for reading and writing .xlsx files
MIT License
397 stars 113 forks source link

xlsxioread_sheet_open does not seem to be honoring the flags sent. I have tried all of them, but it is not skipping blank cells oe extra cells #129

Closed iweave1 closed 2 months ago

iweave1 commented 2 months ago

Hi. I am using the example code to count rows and columns in a spreadsheet passed in as arguments via commandline. The xlsxioread_sheet_open function does not seem to be honoring the flags. Here is my code:

`#include

include

include

include

int main(int argc, char* argv[]) {

if (argc<2) {
    printf("Usage: xl_row_col_count <Excel Filename>\n");   
    exit(1);
}
//open .xlsx file for reading
xlsxioreader xlsxioread;
const char* filename = argv[1];
if ((xlsxioread = xlsxioread_open(filename)) == NULL) {
  fprintf(stderr, "Error opening .xlsx file\n");
  return 1;
}

//read values from first sheet
int row_count = 0;
int col_count = 0;  
char* value;
xlsxioreadersheet sheet;
const char* sheetname = NULL;
printf("Contents of first sheet:\n");
if ((sheet = xlsxioread_sheet_open(xlsxioread, sheetname, XLSXIOREAD_SKIP_EXTRA_CELLS | XLSXIOREAD_SKIP_EMPTY_ROWS)) != NULL) {
  //read all rows
  while (xlsxioread_sheet_next_row(sheet)) {
    row_count++;    
    //read all columns
        while ((value = xlsxioread_sheet_next_cell(sheet)) != NULL) {
            //printf("%s\t", value);
            xlsxioread_free(value);
        if (value != NULL) {
            if (strcmp(value, "") != 0) {
                col_count++;
            }
        }
        }

  }
    xlsxioread_sheet_close(sheet);
    printf("%d Rows, %d Columns.\n",row_count, col_count);
}

//clean up
xlsxioread_close(xlsxioread);

}`

iweave1 commented 2 months ago

I just realized my mistake. It is counting each cell in a row each time it counts a row, thus multiplying the column count by the row count.