-
-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to format a column with a currency when using json_to_sheet? #885
Comments
The If you just want to format a specific column, after generating the worksheet with var C = XLSX.utils.decode_col("B"); // 1
var fmt = '$0.00'; // or '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)' or any Excel number format
/* get worksheet range */
var range = XLSX.utils.decode_range(ws['!ref']);
for(var i = range.s.r + 1; i <= range.e.r; ++i) {
/* find the data cell (range.s.r + 1 skips the header row of the worksheet) */
var ref = XLSX.utils.encode_cell({r:i, c:C});
/* if the particular row did not contain data for the column, the cell will not be generated */
if(!ws[ref]) continue;
/* `.t == "n"` for number cells */
if(ws[ref].t != 'n') continue;
/* assign the `.z` number format */
ws[ref].z = fmt;
} |
@SheetJSDev |
This doesn't work for ODS files - #1569. |
Here's a function - function formatColumn(worksheet, col, fmt) {
const range = XLSX.utils.decode_range(worksheet['!ref'])
// note: range.s.r + 1 skips the header row
for (let row = range.s.r + 1; row <= range.e.r; ++row) {
const ref = XLSX.utils.encode_cell({ r: row, c: col })
if (worksheet[ref] && worksheet[ref].t === 'n') {
worksheet[ref].z = fmt
}
}
} Example: const rows = [
['name', 'cost', 'price'],
['dino', 3.45, 7.95]
]
const workbook = XLSX.utils.book_new()
const worksheet = XLSX.utils.aoa_to_sheet(rows) // array of arrays
const currency = '$0.00'
for (let col of [1, 2]) {
formatColumn(worksheet, col, currency)
}
XLSX.utils.book_append_sheet(workbook, worksheet, 'Details') |
If this is not supported, what is the best mechanism for creating a sheet that has formatted columns?
The text was updated successfully, but these errors were encountered: