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.

define Cell Data Format

See original GitHub issue

Is there a way to enforce Cell Data Format for empty cells?

I generate the sheet with:

const ws = XLSX.utils.aoa_to_sheet([['column 1', 'column 2', 'column 3']]);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'SheetJS');
const contents = XLSX.write(wb, { type: 'buffer', bookType: 'xlsx' });

however, after downloading the sheet and opening it in Numbers the Data Format is automatic. I want to enforce this to be text.

image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SheetJSDevcommented, Jun 11, 2022

Samples:

To find the formats, run in NodeJS:

> require("xlsx").readFile("Types.xlsx", {cellNF: true, sheetStubs: true}).Sheets["Sheet 1"].A3.z
'm/d/yy h:mm AM/PM'
> require("xlsx").readFile("Types.xlsx", {cellNF: true, sheetStubs: true}).Sheets["Sheet 1"].B3.z
'General'
> require("xlsx").readFile("Types.xlsx", {cellNF: true, sheetStubs: true}).Sheets["Sheet 1"].C3.z
'@'

So forcing “Text” seems to be a matter of setting the cell number format to Text (@ in Excel).

This normally would be

const ws = XLSX.utils.aoa_to_sheet([
	['Date', 'Number', 'Text'],
	[{t: "z", z: "m/d/yy h:mm AM/PM"}, {t:"z", z: "General"}, {t: "z", z: "@"}]
]);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'SheetJS');
XLSX.writeFile(wb, "issue2171.xlsx", { sheetStubs: true });

However there is a logical bug in the cell filtering. Feel free to submit a PR:

diff --git a/bits/67_wsxml.js b/bits/67_wsxml.js
--- a/bits/67_wsxml.js
+++ b/bits/67_wsxml.js
@@ -258,7 +258,7 @@ function write_ws_xml_sheetviews(ws, opts, idx, wb)/*:string*/ {
 
 function write_ws_xml_cell(cell/*:Cell*/, ref, ws, opts/*::, idx, wb*/)/*:string*/ {
        if(cell.c) ws['!comments'].push([ref, cell.c]);
-       if(cell.v === undefined && typeof cell.f !== "string" || cell.t === 'z' && !cell.f) return "";
+       if((cell.v === undefined || cell.t === "z" && !(opts||{}).sheetStubs) && typeof cell.f !== "string" && typeof cell.z == "undefined") return "";
        var vv = "";
        var oldt = cell.t, oldv = cell.v;
        if(cell.t !== "z") switch(cell.t) {
0reactions
laurelgrcommented, Jul 19, 2022

Ah, npm website’s latest version is still 0.18.5, I didn’t notice you changed the installation to provide package yourself. 😅 Thanks for the heads up, I have a workaround so I’ll wait for next release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cell Formatting
Cell formats allow you to change the way cell data appears in the spreadsheet. · The formatting options allows for monetary units, scientific...
Read more >
Change the format of a cell - Microsoft Support
You can apply formatting to an entire cell and to the data inside a cell—or a group of cells. One way to think...
Read more >
Format Cells in Excel (Easy Tutorial)
When we format cells in Excel, we change the appearance of a number without changing the number itself. We can apply a number...
Read more >
Excel 2013: Formatting Cells - GCF Global
Text formats numbers as text, meaning what you enter into the cell will appear exactly as it was entered. Excel defaults to this...
Read more >
What is Format Cells in Excel?
Format Cells : - Excel cell format option is used for changing the appearance of number without any changes in number. We can...
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