This is the thing: I’m trying to get a formula to sum the time of the cells of a column (the column is a “DB folder” plugin column). The formula can be entered in the footer of the database (the database is a note created as “DB folder” that retrieves the metadata from the notes in certain folder and shows them in columns) and it has to be in javascript.
Basically the formula has to sum the values in the column, the format is HH:MM (text type).
For example if one cell has “2:00” and the other cell has “3:15” the result shown should be “5:15”.
The place to enter the formula is opened once I click on the footer
What I’m trying to do
Creating a javascript formula that sums all the time in format HH:MM (the cells are formatted as text).
In the “Db folder” documentation it says:
Footer Formula
In case you are using a footer formula, you can also use the following variables:
values
: An Array of cell values in the column
Having these in consideration, I started to think with logic how to proceed (since I don’t know at the moment know how to code).
Things I have tried
I’ve been searching for hours online to get a javascript formula that sum time in HH:MM format, and it works here. I’ve been doing some work with chat GPT also, I don’t know nothing about javascript, but I’m using logic to get the answer.
This is the best formula I’ve created so far by prompting the issue to chatGPT. The problem is that it doesn’t return any value.
ChatGPT says it’s correct, after trying with different promts. But still no success in showing the sum of the values after putting these code in the footer. Any idea about how to sum these HH:MM values?, or any suggestion about correcting the code?
let totalSeconds = values.reduce((acc, val) => {
const [hoursPart, minutesPart] = val.split(':');
const hours = parseInt(hoursPart.trim().padStart(2, '0'));
const minutes = parseInt(minutesPart.trim().padStart(2, '0'));
return acc + hours * 3600 + minutes * 60;
}, 0);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`;
}
Best regards