简体   繁体   English

循环递归公式

[英]Loop recurring formula

I want to add a recurring formula to my script for a timesheet wherein when I punch out it will calculate the duration of Time In and Time out, then it would lock the values of time in and out and the total duration.我想在我的时间表脚本中添加一个循环公式,其中当我打卡时,它将计算时间和超时的持续时间,然后它会锁定时间和总持续时间的值。 Thank you in advance if you would help me out.如果您愿意帮助我,请提前致谢。

function setValue(cellName, value) {
  SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).setValue(value);
}

function getValue(cellName) {
  return SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).getValue();
}

function getNextRow() {
  return SpreadsheetApp.getActiveSpreadsheet().getLastRow() + 1;
}

function getNextRow2() {
  return SpreadsheetApp.getActiveSpreadsheet().getLastRow() + 2;
}

function setUser1(x) {
  setValue('I1', 'User 1');
}

function setUser2() {
  setValue('I1', 'User 2');
} 

function addRecord(a, b, c) {
  var row = getNextRow();
  setValue('A' + row, a);
  setValue('B' + row, b);
  setValue('C' + row, c);
}

function isUserIn(user) {
  var lastRow = SpreadsheetApp.getActiveSheet().getLastRow();
  for (var i = lastRow; i >= 1; i--) {
    if (getValue("A" + i) == user) {
      if (getValue("C" + i) > getValue("B" + i)) {
        return false;
      }
      return true;
    }
  }
}

function punchIN() {
  var user = getValue("I1");
  var lastRow = SpreadsheetApp.getActiveSheet().getLastRow();
  addRecord(getValue('I1'), new Date());
}

function punchOut() {
  var user = getValue("I1");
  var lastRow = SpreadsheetApp.getActiveSheet().getLastRow();
  if (isUserIn(user)) {
    for (var i = lastRow; i >= 1; i--) {
      if (getValue("A" + i) == user) {
        if (getValue("C" + i) == "") {
          setValue("C" + i, new Date(), "IN");
        }
      }
    }
  }
}

it should basically look like this enter image description here它应该基本上看起来像这样在这里输入图像描述

Please help me do this thanks.请帮我做这个谢谢。 I actually have no background in coding it would be a big help if someone could help me with this.我实际上没有编码背景,如果有人可以帮助我,那将是一个很大的帮助。

Modify your punchout() function as follow:修改你的 punchout() function 如下:

function punchOut() {
var user = getValue("I1");
  var lastRow = SpreadsheetApp.getActiveSheet().getLastRow();
  if (isUserIn(user)) {
    for (var i = lastRow; i >= 1; i--) {
      if (getValue("A" + i) == user) {
        if (getValue("C" + i) == "") {
          setValue("C" + i, new Date());
          var duration = (getValue("C" + i) - getValue("B" + i)) / (60 * 60 * 1000);
          setValue("D" + i, duration);
          SpreadsheetApp.getActiveSheet().getRange("B" + i + ":C" + i).setLocked(true);
          break;
        }
      }
    }
  }
}

Also you can modify your getNextRow is follow:您也可以修改您的 getNextRow 如下:

function getNextNRow(n) {
  return SpreadsheetApp.getActiveSpreadsheet().getLastRow() + n;
}

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

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