sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

define Cell Data Format

Open akoskm opened this issue 4 years ago • 10 comments

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

akoskm avatar Nov 10 '20 13:11 akoskm

That is probably an issue with Numbers XLSX import codec. To be sure, can you construct something Numbers would accept? To do this:

  1. after reading the documentation, do something to make Numbers treat the default format as Text

  2. save as XLSX and confirm that reopening the file shows that the default format is text

  3. either email the file to us ([email protected]) or post it to this issue (you should be able to click-drag XLSX files into the text box in the GH website)

SheetJSDev avatar Nov 11 '20 04:11 SheetJSDev

@akoskm did you find a solution for this? I am having the same issue here, both Excel and Numbers softwares consider all cells the 'Automatic' type...

leodavinci1 avatar Apr 05 '21 13:04 leodavinci1

@leodavinci1 I did not find a solution to this. I was able to verify that in Excel you can save sheets with pre-set cell types that on opening keep their types.

akoskm avatar Apr 05 '21 14:04 akoskm

same question, mark

z695101385 avatar Jul 19 '21 05:07 z695101385

Same question, did anyone find a solution in the meanwhile?

plotka avatar Jan 04 '22 17:01 plotka

Same question...

lakca avatar Apr 28 '22 10:04 lakca

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) {

SheetJSDev avatar Jun 11 '22 21:06 SheetJSDev

Nice to know I wasn't going crazy.

Workaround: have cell stub with empty string value v and cell type t of text to pass the filter, apply formatting to z:

cell = { v: '', t: 's', z: '@' }

@SheetJSDev the line you show is already fixed in commit warn on codepage override in mini build [ci skip], but there hasn't been a new release for 4 months. When can we expect the next release please? :)

laurelgr avatar Jul 18 '22 03:07 laurelgr

https://docs.sheetjs.com/docs/installation/ 0.18.9 (which does not include the commit) was pushed on Jun 9. We are aiming for 0.19.0 with some much-needed date changes, but could release a 0.18.10 if Numbers is significant enough to be prioritized

SheetJSDev avatar Jul 18 '22 17:07 SheetJSDev

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.

laurelgr avatar Jul 19 '22 02:07 laurelgr

This was released and the example works with the latest version

SheetJSDev avatar Jan 03 '23 02:01 SheetJSDev