[英]How can I convert a duration in minutes/seconds in google sheets
我有一个 google sheet 列,其持续时间列的格式如下所示。 4w 1d 19h 56m 16s
。 如何将其转换为总分钟/秒?
PS:该列表中的每个值都是可选的。 意思是,有时几周会丢失,几天会丢失等。
如果列值是简单的字符串并且您使用的是 Apps 脚本,您可以执行以下操作:
function transformDurationString(dString) {
const multipliers = {
s: 1,
m: 60,
h: 60 * 60,
d: 60 * 60 * 24,
w: 60 * 60 * 24 * 7,
};
let totalSeconds = 0;
let durations = dString.split(" ");
for (const duration of durations) {
const durationValue = Number(duration.slice(0, -1));
const durationType = duration.slice(-1);
totalSeconds += durationValue * multipliers[durationType];
}
const totalMinutes = Math.floor(totalSeconds / 60);
const remainderSeconds = totalSeconds % 60;
return `${totalMinutes}m ${totalSeconds}s`;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.