首页 > 解决方案 > 计算时差并将其与 Google Apps 脚本进行比较

问题描述

让我解释一下上下文:

托盘行进路径并在时间和日期“T”进入,它们可能在“随机”时间退出。

随机,我的意思是一个托盘很可能会在 8 月 30 日到达画廊并在 8 月 31 日离开。

这是谷歌表格上当前结果的摘录。(20k 行)。 https://docs.google.com/spreadsheets/d/1AboVGsyN4vyApwsnIt8KfWv0bWf0GnZCNeF5eBEI0Js/edit?usp=sharing

首先按托盘号排序。

我想遍历所有行,检查行+1 是否具有相同的托盘号和相同的“类型”。如果是这种情况,我想计算这两行之间的时间差。例如,如果托盘在早上 7h58m00s 进入,并在第二天 7h59m00s 离开,我想获取一个值(这里是 24:01:00)。如果超过 00:00:30 (s),则将检查此差异。

假设第一个托盘在 8 小时 20 分 26 秒进入,第二个托盘在 8 小时 20 分 45 秒进入,第二个将被删除。

起初,我曾尝试通过浏览包含工作表值的 2D 列表并应用我的条件,但不幸的是,将小时(小时格式的日期)与持续时间进行比较很复杂。

正因为如此,我无法获得一个高效的脚本,通过各种方式,脚本花了很多时间才停止。

在 excel 上,我的宏工作为:

    lig = 2
   nb_col = (last column of data in the sheet)
   nb_lig = (last row of data in the sheet).
loop:
    If Cells(lig, 1).Text = Cells(lig + 1, 1).Text And _
    Cells(lig, 4).Value = Cells(lig + 1, 4).Value Then
        diff= Cells(lig + 1, 2).Value - Cells(lig, 2).Value
        diff= diff+ Cells(lig + 1, 3).Value - Cells(lig, 3).Value
        If diff< "00:00:30" Then
            Rows(lig + 1).Delete Shift:=xlUp
            nb_lig = nb_lig - 1
            GoTo loop
        End If
    End If
    If Cells(lig, 4).Value = "E" Then
        If Cells(lig + 1, 4).Value = "S" Then
            If Cells(lig, 1).Text = Cells(lig + 1, 1).Text Then
                Cells(lig, nb_col - 2).Value = Cells(lig + 1, 5).Value
                Cells(lig, nb_col - 1).Value = Cells(lig + 1, 3).Value
                Cells(lig + 1, nb_col - 2).value = Cells(lig, 5).value
                Cells(lig + 1, nb_col - 1).value = Cells(lig, 3).value
                diff= Cells(lig + 1, 2).value - Cells(lig, 2).value
                diff= diff+ Cells(lig + 1, 3).Value - Cells(lig, 3).Value
                Cells(lig + 1, nb_col).Value = diff
            End If
        End If
    End If
    lig = lig + 1
    If Cells(lig, 1).Text <> "" Then GoTo loop

所以,我认为在 Google Apps Script 上,我应该编写一个列公式来完成这项工作,但我真的找不到它,特别是因为我之前完成的算法执行时间太长并且没有领先达到我想要的结果。

如果有人至少能给我一些关于如何解决这个问题的指导,那真的是一种善意的姿态。

标签: excelgoogle-apps-scriptgoogle-sheets

解决方案


第一步是创建一个日期对象——包含日期和时间。将 B 列和 C 列组合成一个新的 Date()。然后您可以将 date.getTime() 值与下一行进行比较。如果相同,则用颜色标记特定列中的单元格或在右侧的空闲列中写入值。

如果表未排序,则必须运行循环的次数与未标记的行一样多。如果表已排序,则只能运行一次。

第二步,从底部开始,删除向上移动的彩色单元格或标记单元格。

如果您在第一步中删除了不需要的行,行引用将更改并且循环将失败。


推荐阅读