I've a problem when I'm reading a Date value from columns in a excel file using exceljs package, I can't get the truth value in the correct format like a Date data type, instead I get a number.
My project is build with Nest, and I use exceljs to write/read xlsx files, the code is:
worksheet.eachRow({ includeEmpty: true }, (row, rowNumber) => {
row.eachCell((cell, colNumber) => {
...
const header = headers[colNumber - 1];
switch (header) {
...
case 'dateColumn':
// Date format is dd/mm/yyyy
// Original value in excel is '16/06/2020'
const cellValue: Date = cell.value as Date; // When i get value from cell is 43998
console.log(cellValue); // print 43998
break;
...
}
}
The cell format is 'dd/mm/yyyy'
You will need to convert Excel Date Serial Number to JS Date.
Here is a very basic example:
const date0 = new Date(0);
const utcOffset = date0.getTimezoneOffset();
const cellValue = new Date(0, 0, cell.value - 1, 0, -utcOffset, 0);
To convert Excel Serial Date to normal you can use
new Date(Date.UTC(0, 0, cell.value - 1, 0, 0, 0))
Related
I am parsing an excel document in excel using the excel: ^1.1.5 package. In my sheet, i have a Date column and this Date is being received in my Flutter code in the following format
"44663"
rather than:
"2022/04/12"
How do I parse this to a format such as YY-MM-DD.
I have tried DateTime.parse(), but it throws an error that my date format is invalid.
I found the answer :
const gsDateBase = 2209161600 / 86400;
const gsDateFactor = 86400000;
final date = double.tryParse("44663");
if (date == null) return null;
final millis = (date - gsDateBase) * gsDateFactor;
print(DateTime.fromMillisecondsSinceEpoch(millis.toInt(), isUtc: true));
I am creating an excel file by using Excel4node package.
by using this code
// Require library
var excel = require('excel4node');
// Create a new instance of a Workbook class
var workbook = new excel.Workbook();
// Add Worksheets to the workbook
var worksheet = workbook.addWorksheet('Sheet 1');
var worksheet2 = workbook.addWorksheet('Sheet 2');
// Create a reusable style
var style = workbook.createStyle({
font: {
color: '#FF0800',
size: 12
},
numberFormat: '$#,##0.00; ($#,##0.00); -'
});
// Set value of cell A1 to 100 as a number type styled with paramaters of style
worksheet.cell(1,1).number(100).style(style);
// Set value of cell B1 to 300 as a number type styled with paramaters of style
worksheet.cell(1,2).number(200).style(style);
// Set value of cell C1 to a formula styled with paramaters of style
worksheet.cell(1,3).formula('A1 + B1').style(style);
// Set value of cell A2 to 'string' styled with paramaters of style
worksheet.cell(2,1).string('string').style(style);
// Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size.
worksheet.cell(3,1).bool(true).style(style).style({font: {size: 14}});
workbook.write('Excel.xlsx');
by using this code creating an excel sheet now what I want is.
I want to write the array in the excel sheet.
worksheet.getCell('A1').value = 's.no';
by using the code. it is writing the data to the sheet but it is writing the data by cell by cell.
it takes to much of time to write the array in excel sheet
data=[{s.no:1,Name:'xxx',Age:'22'},
{s.no:2,Name:'yyy',Age:'12'},
{s.no:3,Name:'zzz',Age:'32'}]
I want to write the array in the excel sheet.
workbook.write('Excel.xlsx',data);
I given like this but this also not working.
can anyone resolve this.
why you don't use
sheet.cell(row , col ).string(`your value`).style(`your style`);
What I would do is create an excel4node layer. Something to the effect of:
// the type arg is the cell write method you want to use such as string, number, or formula
const writeCell(wb, ws, row, col, value, type, style){
ws.cell(row, col)[type](value).style(wb.createStyle(style))
}
const xl = require('excel4node')
const wb = xl.createWorkbook()
// TODO: track your row and column indices with arrays. let's assume that we are
// in the outer array and the inner array or row array is called "data"
// this would write out a row of data
data.forEach((d, idx) => {
writeCell(wb, ws, rowIdx, idx + 1, d.value, d.type, d.style)
}
const array_elements = [{
fullname: "name 1",
game1point: 10,
game2point: 12
},
{
fullname: "name 2",
game1point: 15,
game2point: 17
},
]
let startRow = 3;
for (let i = 0; i < array_elements.length; i++) {
// FULLNAME - FIRST COLUMN
worksheet.cell(startRow + i, 1).string(array_elements[i].fullname);
// SECOND COLUMN
worksheet.cell(startRow + i, 2).number(array_elements[i].game1point);
// THIRD COLUMN
worksheet.cell(startRow + i, 3).number(array_elements[i].game2point);
gameCol++;
}
Selenium is incorrectly reading the date as 43095 when I enter 26-12-2017. How to get Selenium to read the correct date?
for (int i=0;i<=TcRow;i++)
{
for (int j=0;j<TcCol;j++)
{
Cell Cell=TcSheet.getRow(i).getCell(j);
}
}
Am I reading the format incorrectly?
TcSheet.getRow(i).getCell(j).setCellType(Cell.CELL_TYPE_STRING);
What changes do I need to do here to make sure they read both the string and the date field?
data[i][j]=TcSheet.getRow(i).getCell(j).getStringCellValue();
}
I also faced the same issue during reading the excel file where I'm fetching date is formatted in dd/mm/yyyy format and selenium fetching wrong value.
For that, I have used DataFormatter. It will returns Excel cell value with format e.g. Date format 15-04-208 in excellent then it will returns date with same format. Look below code that I used in my Framework. Hope it will also work for you.
FileInputStream fis = new FileInputStream("path\\to\\file.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(worksheet);
DataFormatter formatter = new DataFormatter();
Cell cell = sheet.getRow(rowNum).getCell(cellNum);
String cellValue = formatter.formatCellValue(cell);
System.out.println(cellValue);
return cellValue;
Let me know if you have any query.
I am trying to create date validation on an xlsx column so far I have:
DataValidation dataValidation = new DataValidation
{
Type = DataValidationValues.Date,
AllowBlank = false,
ShowErrorMessage = true,
ErrorTitle = "Invalid value entered",
Error = "Please enter a valid date in dd/mm/yyyy format",
SequenceOfReferences = new ListValue<StringValue> { InnerText = "A2:A10000" }
};
if I open an xlsx document and select the date validation type there are fields for the following:
Data: Between, Greater than, less than
Minimum
Maximum
How can I set these programmatically?
This gives you a date time validation for column A rows 2 to 10,000, set your start date with formula1 (the number of days past 1900) and end date with formula2 which is the number of days after the start date (in this case 01/01/2021)
DataValidation dataValidation = new DataValidation
{
Type = DataValidationValues.Date,
AllowBlank = false,
ShowErrorMessage = true,
ShowInputMessage = true,
ErrorTitle = "Invalid value entered",
Error = "Please enter a valid date in dd/mm/yyyy format",
SequenceOfReferences = new ListValue<StringValue> { InnerText = "A2:A10000" },
Formula1 = new Formula1("1");
Formula2 = new Formula2("44196");
};
I am want to read excel file having phone numbers stored as numbers but when I read the file using SheetJS/js-xlsx (npm install xlsx), All the large phone numbers are converted to strings like
9.19972E+11
919971692474 --> 9.19972E+11
My code is
var workbook = XLSX.readFile(req.files.fileName.path);
var sheet_name_list = workbook.SheetNames;
var csvFile = XLSX.utils.sheet_to_csv(workbook.Sheets[sheet_name_list[0]]);
console.log(csvFile2);
console output is
customer_phone,product_name
9.19972E+13,"Red Belly Shoes,"
Is there any way I can avoid such conversion?
The number 919971692474 is normally displayed as 9.19972E+11 in Excel. To force it to display the full number you have to set the number format to 0 (right click, format cell, choose custom type '0'). And when you do that, the full number is displayed. If you don't set a format in excel, the xlsx module uses the "General" format and that number format displays the phone number as an exponential.
If the file is incorrect, you can override the CSV formatting by deleting the w key and adding a z key corresponding to the desired number format. For example, to change cell A2:
var sheet = workbook.Sheets[workbook.SheetNames[0]];
delete sheet.A2.w;
sheet.A2.z = '0';
If you want to do this for all number cells, just loop:
Object.keys(sheet).forEach(function(s) {
if(sheet[s].w) {
delete sheet[s].w;
sheet[s].z = '0';
}
});
By default sheet_to_csv take the formatted numbers.
- To avoid the formatted value and to take raw inputs (original values) you have to add parameter in sheet_to_csv method you have to set rawNumbers to true
Try this code
var csvFile = XLSX.utils.sheet_to_csv(workbook.Sheets[sheet_name_list[0]], { rawNumbers: true });
It seems in later versions w is not there. That's how it could be done in recent versions.
const ws = XLSX.utils.json_to_sheet(data);
Object.keys(ws).forEach(function(s) {
if(ws[s].t === 'n') {
ws[s].z = '0';
ws[s].t = 's';
}
});
const csv = XLSX.utils.sheet_to_csv(ws);
Using sheet[s].z = '0'; works in removing the scientific notation, but it also removes any decimal places you might want to retain. From the readme:
The cell.w formatted text for each cell is produced from cell.v and cell.z format.
I was able to remove the scientific notation by explicitly setting the value of w instead of letting xlsx calculate it for me:
if (cell.t === 'n') {
cell.w = cell.v;
}