xls => json => manipulation => xlsx #586
Labels
No Label
DBF
Dates
Defined Names
Features
Formula
HTML
Images
Infrastructure
Integration
International
ODS
Operations
Performance
PivotTables
Pro
Protection
Read Bug
SSF
SYLK
Style
Write Bug
good first issue
No Milestone
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: sheetjs/sheetjs#586
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
So, I've been digging around this repo a bit and it seems like the go-to method for filtering etc is to convert things to json using XLSX.utils.sheet_to_json(wb.Sheets.SheetName), however I can't find any documentation on then turning that manipulated json back into a workbook. Please advise! (thanks in advance)
@reidblomquist at a high level, sheet_to_json takes an options argument. The header field of the options argument controls the output format: default is an array of JSON objects whose keys are determined by the first row,
"A"
will use the column labels rather than the first row, and1
will generate an array of arrays. By default it will emit the formatted strings, but also setting raw:true will give you the underlying values. Using the write.xlsx file from the test files repo :https://github.com/SheetJS/js-xlsx/blob/master/tests/write.js includes an example of how to convert an array of arrays back into a worksheet. The essence of it is to walk the array and generate a cell object for each value based on its position.
This will eventually have to be merged into the codebase when we add in support from js-harb -- for formats like DIF and PRN, which effectively represent everything as text, it's structurally easier to generate an array of arrays.
PS: The date/number confusion is due to how Excel stores dates -- they are actually date codes with a special number format. This will be addressed when we settle on the correct behavior for cellDates option.
Keep this open until we add all of this to the documentation and have a proper roundtrip demo
@SheetJSDev thank you very much for the prompt response! I wanted to leave my json a bit more structured for my filtering purposes (so, an array of objects instead of an array of array) - but (just in case anyone else wants the sauce there) I managed to walk the resulting data as an array of arrays like so:
@reidblomquist here's an example: https://runkit.com/sheetjs/58dc6ea1eae8b800146359eb Let us know if that makes any sense
@SheetJSDev I'm having the same issue with the Dates.. What's the best approach to get back the "date" format on the XLS file and not a number?
If you are starting from a file, pass the option
cellDates:true
:@SheetJSDev cool. Now it reads the Date. But when I write back to file i see just the numbers instead of the Dates.
aoa_to_sheet also needs the argument:
The relevant part of the docs is https://sheetjs.gitbooks.io/docs/#array-of-arrays-input -- how should it be changed to explain that behavior? Currently it says:
I guess it should just say "raw dates" rather than "type d"?
Thank you!
Yeah "raw dates" would be better.
Btw, I changed the code this way:
But now the Date columns are blank.
PS. my date format is "mm/gg/yyyy"
Excel does some weird things with localization. Can you try with the date format
mm/dd/yyyy
? Also, if you can share an input file I'd love to take a look@SheetJSDev sorry i made a typo in my previous post, date is already
mm/dd/yyyy
. I'm using a standard XLS file.. Are you able to reproduce the issue?Just tried:
source file: write.xlsx
output file: writenew.xlsx
The loss of the original format is expected (since the JSON loses the format info) but setting the format to
mm/dd/yyyy
doesn't result in a blank cell:Really weird. Still having that issue. Btw I'm using XLS files not XLSX.
@roccomuso yes I can reproduce with the XLS write, there are two issues:
I saw the
3a310bd
commit. When will it be released? 👍@roccomuso it should be in 0.9.9 (the latest version) already. As mentioned, that is only the first half of the fix (to ensure that the date cells aren't blank).
@SheetJSDev alright, what's missing exactly? will the v0.9.9 output the data cells as expected?
The dates will show up as numbers. forcing dates isn't particularly hard, but we're trying to rebuild the formatting library before attacking those issues
@SheetJSDev thanks for being so responsive here.
Excuse me for asking another question but i've reached the point where I cant find the answer and I need to ask somebody.
In my excel sheet the date is inserted as: "03/12/2015", but excel formats it to: "03-Dec-15" .
When I turn the sheet into json, the date comes through as the formatted version "03-Dec-15".. I want the unformatted version "03/12/2015". I have added {raw:true} and {cellDates:true} but this isnt producing the result im looking for.
Any help or advice? :)
@GreggOD the specific input format is not preserved in the file. For example, if you type
Jan 1
in US English Excel the input bar will show1/1/2017
and the cell will show1-Jan
. So where'sJan 1
? it was lost.If you want to see
03/12/2015
, you can force the output to render using the format codemm/dd/yyyy
:cellDates:true
option in the input function (read
,readFile
,aoa_to_sheet
, ... however you are building the worksheet)dateNF:"mm/dd/yyyy"
insheet_to_json
Alternatively you can change the output date format manually by editing the
z
property of the relevant cells tomm/dd/yyyy
.@reidblomquist there are new utility functions
sheet_add_aoa
andsheet_add_json
that can directly append to the worksheet without having to convert to JSON.@SheetJSDev
How we can join multiple array in one object.
Suppose i have two xlsx file.
var url = ['assets/sample-dataset.xlsx', 'assets/sheet2.xlsx'];
XLSX.utils.sheet_to_json(worksheet, { raw: true });
In that i am getting two JSON data.
Now how we can merge in one object ? is there any method to merge multiple object in to one ?
I feel like that's a JavaScript question and not a sheetjs question @rahulpathak
Merge your two arrays using the spread operator:
And then if you want to sort the data use sorting methods from JavaScript.
Hi @SheetJSDev / @GreggOD
I am using angular 7, I have to read multiple excel file. so i created one function to read data and after read data i have to merge all data in one object.
But here urldata not getting any value. In datalog getting value but not returning any value.
Please help me out why i am not getting any value he saying 'undefined'.
You need to return your callback in your
filereader
function:after return callback() still getting 'undefined'
@rahulpathak
Does the
datalog
in here have data?But 'urldata' variable doesn't have any value. getting 'undefined' value. I guess filereader function not returning any value. I check with setTimeout() also but no still same output. Is there any logic need to change ?
Im a little confused why you have double brackets on your function
Shorthand would actually look like this
Yes i have done changes. But still not working.
I have attached sample excel sheet and use above code.
sheet2.xlsx;