javascript 如何自动添加天数差异的计算?

idfiyjo8  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(75)

在一个谷歌电子表格我有2个标签,我试图自动输入尽可能多。
当一行输入了信息,并且一个带有tickbox = true的列将行从选项卡1移动到选项卡2时,日期会自动输入选项卡1。
在选项卡2中,我有两个额外的列复制信息-它被移动的日期,然后一列计算从选项卡1中的日期1和选项卡2中的日期2的天数差。
除了计算天数之外,该脚本对于大多数操作都运行良好
我使用以下脚本

function onEdit(e) {
  var sheet = e.source.getActiveSheet();
  var range = e.range;

  var ss = e.source;
  var tickedSheet = ss.getSheetByName("Restricted soon"); // Name of the source sheet
  var verifiedSheet = ss.getSheetByName("Verified"); // Name of the destination sheet

  var tickedRange = tickedSheet.getRange("C2:C"); // Assuming the tick boxes start from row 2 in column C
  var tickedValues = tickedRange.getValues();

  var targetRange = verifiedSheet.getRange(verifiedSheet.getLastRow() + 1, 1); // Destination sheet will append rows at the bottom
  var currentDate = new Date();

  for (var i = 0; i < tickedValues.length; i++) {
    if (tickedValues[i][0] === true) { // Check if tick box is ticked
      var rowToMove = tickedSheet.getRange(i + 2, 1, 1, tickedSheet.getLastColumn());
      rowToMove.copyTo(targetRange);

      // Set the date in column F in Sheet 2
      var targetDateCell = targetRange.offset(0, 5);
      targetDateCell.setValue(currentDate);

      tickedSheet.deleteRow(i + 2);
    }
  }

  // Check if the edited column is A and the row is not the header row
  if (range.getColumn() == 1 && range.getRow() > 1) {
    var row = range.getRow();
    var dateCell = sheet.getRange(row, 5);

    // Check if the date cell is empty before filling in the date
    if (dateCell.getValue() === "") {
      dateCell.setValue(new Date());
    }

    // Compare the date in column E with the date in column F and calculate the difference
    // Compare the date in column E with the date in column F and calculate the difference
    var startDate = new Date(sheet.getRange(row, 5).getValue());
    var endDate = new Date(sheet.getRange(row, 6).getValue());

    var difference = Math.floor((endDate - startDate) / (1000 * 60 * 60 * 24)); // Difference in days

    // Set the calculated difference in column G of the "Verified" sheet
    verifiedSheet.getRange(verifiedSheet.getLastRow(), 7).setValue(difference);
  }
}

上面的代码会出现#NUM!错误。我已经尝试了像下面这样的错误处理,但我得到空白单元格时使用这个:

if (!isNaN(startDate) && !isNaN(endDate)) {
      var difference = Math.floor((endDate - startDate) / (1000 * 60 * 60 * 24));

数学方程似乎是问题所在,我想是因为计算出来的数字不能打印在谷歌表单上?
会感谢任何人的指导,而不是chatgpt,我是一个初学者,并试图真正学习尽可能多。TIA

llew8vvj

llew8vvj1#

您面临的问题似乎与使用Math.floor函数计算日期差异有关。您可以使用Math.round函数将结果四舍五入到最接近的整数,而不是使用Math.floor。这将确保计算出的天数差异在Google表格中正确显示。
下面是代码的更新部分:

// Compare the date in column E with the date in column F and calculate the difference
var startDate = new Date(sheet.getRange(row, 5).getValue());
var endDate = new Date(sheet.getRange(row, 6).getValue());

var difference = Math.round((endDate - startDate) / (1000 * 60 * 60 * 24)); // Difference in days

// Set the calculated difference in column G of the "Verified" sheet
verifiedSheet.getRange(verifiedSheet.getLastRow(), 7).setValue(difference);

通过使用Math.round而不是Math.floor,计算出的差值将四舍五入到最接近的整数。这应该有助于解决#NUM!您遇到的错误
请给予这个更新的代码一个尝试,让我知道如果你有任何进一步的问题或问题!

相关问题