Search code examples
excellibxl

Can't get LibXL xlSheetGetNamedRange to work


I am trying to work with LibXL. I can extract data from sheets, but need a function to transform a string with Excel row and column indices into startRow, endRow, startCol, endCol i.e.

"A1:B3" into startRow = 0, endRow = 2, startCol = 0, endCol = 1 (LibXL uses 0 based indexing)

I have tried everything I can think of. The library does not come with any examples that use this function and the documentation is quite sparse. What am I doing wrong?

Here is my code:

int main()
{
    const char range[] = "B2:C3";
    int i, ret, rowFirst=0, rowLast=0, colFirst=0, colLast=0;
    BookHandle book;
    SheetHandle sheet;

    book = xlCreateBook();

    ret = xlBookLoad(book, "/home/jason/Downloads/panel.xls");

    sheet = xlBookGetSheet(book, 0);

    ret = xlSheetGetNamedRange(sheet, &range[0], &rowFirst, &rowLast, &colFirst, &colLast);

    printf("ret from xlSheet...Range = %d\n", ret);

    printf("%s\n", xlBookErrorMessage(book));
    printf("rowLast = %d\n", rowLast);
    printf("rowLast = %d\n", rowLast);
    printf("colFirst = %d\n", colFirst);
    printf("colLast = %d\n", colLast);

    return 0;

}

Solution

  • use xlSheetAddrToRowCol() function for this purpose:

    int rowStart, colStart, rowEnd, colEnd;
    int rowRelativeStart, colRelativeStart, rowRelativeEnd, colRelativeEnd;
    
    xlSheetAddrToRowCol(sheet, "B2", &rowStart, &colStart, &rowRelativeStart, &colRelativeStart);
    xlSheetAddrToRowCol(sheet, "C3", &rowEnd, &colEnd, &rowRelativeEnd, &colRelativeEnd);