繁体   English   中英

如何在谷歌表格中转换以分钟/秒为单位的持续时间

[英]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`;
}

总分钟/秒

=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")))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM