我有一个谷歌表格列,其中的持续时间列的格式如下。
4w 1d 19h 56m 16s
。我怎样才能将其转换为总分钟/秒?
PS:该列表中的每个值都是可选的。意思是,有时几周会消失,几天会消失等等。
总分钟/秒
=ARRAYFORMULA(IF(A1:A10="",,TEXT(MMULT(IFERROR(REGEXEXTRACT(SPLIT(A1:A10, " "), "\d+")*
VLOOKUP(REGEXEXTRACT(SPLIT(A1:A10, " "), "\d+(.*)"),
{"s",1;"m",60;"h",3600;"d",86400;"w",604800}, 2, 0), 0),
SEQUENCE(COLUMNS(SPLIT(A1:A10, " ")), 1, 1, 0))/86400, "[mm]:ss")))
改进版:
=INDEX(IFERROR(1/(1/BYROW(A3:A15, LAMBDA(i, LET(x, "(\d+)\s*", SUM(IFERROR(
REGEXEXTRACT(i, x&{"w";"d";"h";"m";"s"}), 0)*{604800;86400;3600;60;1})/86400))))))
如果列值是简单字符串并且您正在使用 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`;
}