question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting column width for .xls

See original GitHub issue

I have gone through the docs, and the relevant examples I have found, even the test files on the repo, and all seem to be dealing with xlsx files (for which I have no difficulty to set the column widths), but none of the solutions I found work with xls files.

I am setting the bookType when writing the file, and using wpx when setting the column properties.

So, how would one go about doing that?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
SheetJSDevcommented, Aug 28, 2021

The following should work for BIFF8 XLS write. Feel free to send a PR:

diff --git a/bits/39_xlsbiff.js b/bits/39_xlsbiff.js
index b5eb743..755b5de 100644
--- a/bits/39_xlsbiff.js
+++ b/bits/39_xlsbiff.js
@@ -967,6 +967,20 @@ function parse_ColInfo(blob, length, opts) {
        if(opts.biff >= 5 || !opts.biff) o.level = (flags >> 8) & 0x7;
        return o;
 }
+function write_ColInfo(col, idx) {
+       var o = new_buf(12);
+       o.write_shift(2, idx);
+       o.write_shift(2, idx);
+       o.write_shift(2, col.width * 256);
+       o.write_shift(2, 0);
+       var f = 0;
+       if(col.hidden) f |= 1;
+       o.write_shift(1, f);
+       f = col.level || 0;
+       o.write_shift(1, f);
+       o.write_shift(2, 0);
+       return o;
+}
 
 /* [MS-XLS] 2.4.257 */
 function parse_Setup(blob, length) {

diff --git a/bits/78_writebiff.js b/bits/78_writebiff.js
index 6d0cf37..d761cae 100644
--- a/bits/78_writebiff.js
+++ b/bits/78_writebiff.js
@@ -164,6 +164,16 @@ function write_ws_biff8_hlinks(ba/*:BufArray*/, ws) {
        delete ws['!links'];
 }
 
+function write_ws_cols_biff8(ba, cols, ws) {
+       if(!cols) return;
+       var cnt = 0;
+       cols.forEach(function(col, idx) {
+               if(++cnt <= 256 && col) {
+                       write_biff_rec(ba, "ColInfo", write_ColInfo(col_obj_w(idx, col), idx));
+               }
+       });
+}
+
 function write_ws_biff8_cell(ba/*:BufArray*/, cell/*:Cell*/, R/*:number*/, C/*:number*/, opts) {
        var os = 16 + get_cell_style(opts.cellXfs, cell, opts);
        if(cell.v == null && !cell.bf) {
@@ -227,6 +237,8 @@ function write_ws_biff8(idx/*:number*/, opts, wb/*:Workbook*/) {
        write_biff_rec(ba, "HCenter", writebool(false));
        write_biff_rec(ba, "VCenter", writebool(false));
        /* ... */
+       if(b8) write_ws_cols_biff8(ba, ws["!cols"], ws);
+       /* ... */
        write_biff_rec(ba, 0x200, write_Dimensions(range, opts));
        /* ... */

Here’s a simple test you can run once you make those changes:

var widths = [1,2,3,4,5,6,7];
var ws = XLSX.utils.aoa_to_sheet([
	widths,
	widths.map(function(n) { return new Array(n+1).join("0"); }) // wch is based on "0"
]);
ws["!cols"] = widths.map(function(w) { return { wch: w }; });
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Width");
XLSX.writeFile(wb, "issue1208.xls");
2reactions
Vacalexiscommented, Feb 6, 2020

Hello, I’m experiencing the same problem. Column width works for .xlsx files but not for .ods or .xls. Any news on this subject?

Similar issue here: https://github.com/SheetJS/sheetjs/issues/1454

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change the column width and row height - Microsoft Support
To change the default column width for the entire workbook, right-click a sheet tab, and then click Select All Sheets on the shortcut...
Read more >
How to change and AutoFit column width in Excel - Ablebits
How to set column width to a certain number · Select one or more columns that you wish to resize. To select all...
Read more >
How to Change Column Width in Excel
Adjust Column Width with Mouse · Set Column Width to a Specific Number · Set Column Width by Right-Click · Adjust Column Width...
Read more >
4 Methods for How To Change Cell Size in Excel | Indeed.com
To enable AutoFit, start by clicking on a cell in the row or column you want to change. Then, click on the "Home"...
Read more >
Excel 2010: Modifying Columns, Rows, and Cells - GCF Global
To set column width with a specific measurement: · Select the columns you want to modify. · Click the Format command on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found