Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsgoogle-sheets-formula

Date timestamp that doesn't refresh


I'm currently working on a sheet that transfer lines of data when you check a checkbox.

So far, when the line of data is moved to the new page of the spreadsheet, it should return a timestamp with the date at the last column. The problem is that it refreshes every time I open the sheet up.

    function setTimestamp(x) { // Função criada para registro de modificação
    if(x != "TRUE"){ // Quando X(célula indicada) mudar
        return  new Date(); // Retorna a data
    }

This is the code I'm using just for the timestamp, but it would be great if I could implement the timestamp thing(without refreshing) on the onEdit moving function that I'm using, which is this one

    function onEdit(event) { // COD ATUALIZADO 22/11/22

  var ss = SpreadsheetApp.getActiveSpreadsheet(); // Ativa planilha
  var s = event.source.getActiveSheet(); // Página origem do evento
  var r = event.source.getActiveRange(); // Página fim do evento

  if(s.getName() == "EQUIPAMENTOS NO LABORATÓRIO" && r.getColumn() == 1 && r.getValue() == true) { // Se condições OK(IDA)
    var row = r.getRow(); // Pega a linha
    var numColumns = s.getLastColumn(); // Número de colunas
    var targetSheet = ss.getSheetByName("EQUIPAMENTOS LIBERADOS"); // Planilha alvo
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1); // Local alvo
    s.getRange(row, 1, 1, numColumns).moveTo(target); // Insere no local alvo
    s.deleteRow(row); // Exclui a origem

  } else if(s.getName() == "EQUIPAMENTOS LIBERADOS" && r.getColumn() == 1 && r.getValue() == false) { // Se condições OK(VOLTA)
    var row = r.getRow(); // Pega a linha
    var numColumns = s.getLastColumn(); // Número de colunas
    var targetSheet = ss.getSheetByName("EQUIPAMENTOS NO LABORATÓRIO"); // Planilha alvo
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1); // Local alvo
    s.getRange(row, 1, 1, numColumns).moveTo(target); // Insere no local alvo
    s.deleteRow(row); // Exclui a origem
  }
}

Solution

  • Try this at the end of your onEdit(event) function:

      target.offset(0, numColumns).setValue(new Date());