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.

Values in cells actually not formatted as well

See original GitHub issue

Hello. I set values for row and set format like this:

    const row = worksheet.getRow(...);
    row.numFmt = 'hh:mm';
    row.values = userRec;

userRec is array of strings that represents time: [“10:51”,“10:56”,“10:46”] When I open created document, I see my data, formatted as I want, but: I want see sum of times, I type formula in cell =SUM(C7:D7) and see 00:00 image In LibreOffice when I look in cell with tiimes I see there is symbol ’ infront of value, and when I remove it and hit enter - formula start to work. I have to do it with every cell. image In MS Office I have no symbol ’ infront of value, but formula still don’t work, I have to click cell and hit enter - then formula works. That is how it looks after: image

In both cases cell has right format “hh:mm” and time type. How can I fix this ?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
tvoloshyncommented, Nov 25, 2021

The only way I managed to get durations working properly is by constructing the following date object as a value:

cell.value = new Date(
    Date.UTC(1899, 11, 30, hours, minutes, seconds, 0)
)

I believe it is something to do with how Excel stores dates (1900 / 1904 mode).

0reactions
frenzymindcommented, Nov 30, 2021

Little summary:

  1. Using new Date(Date.UTC(1899, 11, 30, hours, minutes, seconds, 0)) construction really solve the issue. Thanks @tvoloshyn , but may I ask why 1899-11-30 ? I can’t found any tie to this date. Here is example:
test() {
    this.sheet.addRow([
      new Date(Date.UTC(1899, 11, 30, 16, 32, 0, 0)),
      new Date(Date.UTC(1899, 11, 30, 16, 32, 0, 0)),
    ]);

    const cellA = this.sheet.getCell('A1');
    cellA.numFmt = 'HH:MM';

    const cellB = this.sheet.getCell('B1');
    cellB.numFmt = 'HH:MM';

    const resCell = this.sheet.getCell('C1');
    resCell.value = {
      formula: 'SUM(A1:B1)',
      date1904: false,
    };
    resCell.numFmt = '[HH]:MM';

    this.book.xlsx.writeFile(`/node/app/test001.xlsx`);
  }

It gives perfect result:

  • no weird single quote in front of value (libre office)
  • value at once in right condition (previously I have to remove the single quote sign or clicking every cell in MS Excel)
  • formula works correctly with ‘sum’ function Screenshot from 2021-11-30 10-49-25
  1. It is also possible to use strings as before, but then:
  • you have strange single quote in front of value (libre office)
  • ‘sum’ function is not work in cell formula
  • to make cell value become natural, you have to delete single quote (in libre office) or clicking every of them in ms excel
  • to calculate duration you have to use summary of all cells in formula: ‘A1+B1+C1’ and so on, as @ladaniavadh shows before

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix text-formatted numbers by applying a number format
Technique 1: Convert text-formatted numbers by using Error Checking ; A large range of cells. Click the first cell in the range, and...
Read more >
How to Fix Excel Numbers That Don't Add Up - Contextures
Right-click a blank cell, and click Copy · Select the cells that contain the "text" numbers · Right-click on one of the selected...
Read more >
Excel format for number, text, scientific notation, accounting, etc.
In some cases, Excel may not display the cell value exactly as you've entered it, though the cell format is left as General....
Read more >
Formatting problems – Data Organization in Spreadsheets for ...
Example: merging cells. Solution: If you're not careful, formatting a worksheet to be more aesthetically pleasing can compromise your computer's ability to see ......
Read more >
Excel table not formatting new rows appropriately - Super User
To resolve this click on the header of a column then right click and choose "Format Cells" option and set the desired format....
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