QXlsx icon indicating copy to clipboard operation
QXlsx copied to clipboard

how to set column width when create a new worksheet

Open turbin opened this issue 4 years ago • 6 comments

Hi, I am using this project to handle XLSX file, it is working perfect. but I met an issue. I want to set the column width after I create a new xlsx document object, but I get the 0 value calling Worksheet::columnWidth(),after I called WorksheetsetColumnWidth() to set a width value. I am reading those code about this part, my question is:

why no calling columnInfo->isSetwidth =true after calling columnInfo->width = width when call the method Worksheet::setColumnWidth();

It makes that the method "Worksheet::columnWidth()" will return the default value (default value is 0), when columnInfo->isSetwidth is not true and I call it.

Thanks for your working.

turbin avatar Apr 07 '21 02:04 turbin

Dear @turbin

Thank you for reporting.

I created a repo for testing. https://github.com/JayTwoLab/qxlsx-issues-152

Can you make a pull request for testing?

j2doll avatar Apr 15 '21 13:04 j2doll

Seems I have same issue. I would like to set column width if after autosizeColumnWidth it is less than my minWidth.

if (!doc.autosizeColumnWidth(1, maxColumn))
        return;
    for (auto i=1; i <=maxColumn; i++) {
        const auto width = doc.columnWidth(i);
        if (width < minWidth)
            doc.setColumnWidth(i, minWidth);
        else if (width > maxWidth)
            doc.setColumnWidth(i, maxWidth);
        qDebug() << i << width << doc.columnWidth(i);
    }

But in console I see that all doc.columWidth return 0 How can I improve my code?

cezaryece avatar Jan 13 '23 15:01 cezaryece

Can you test the new code? https://github.com/QtExcel/QXlsx/commit/00380c3ffad1fad1fb63b1a244aa2864db1d3b8d

j2doll avatar Jan 16 '23 14:01 j2doll

Its only sets predefined value of column width. My goal is to

  1. optimize column width (doc.autosizeColumnWidth)
  2. set column width to some minimum value if column width is smaller
  3. set column width to some maximum value if column width is larger For that I need actual column width, not default value.

cezaryece avatar Jan 16 '23 17:01 cezaryece

Okay i will keep this issue. Thanks for reporting.

j2doll avatar Jan 18 '23 12:01 j2doll

As workaround I get QXlsx code to my app and refactor doc.autosizeColumnWidth to achieving my goals.

cezaryece avatar Jan 19 '23 10:01 cezaryece