Search code examples
google-sheets

How to autofill date in google sheet when i enter value in cell A


If I enter "paid" text in cell A in sheet, I want to auto pull the current date in cell B. If the cell is empty, it should not pull the date. Like:

--------------------------------------
A           |  B
--------------------------------------
Paid        |  2/25/2018
----------------------------------
Paid        | 2/26/2018 
----------------------------------
empty cell  |   
----------------------------------
empty cell  |  




I tried to use =ArrayFormula(IF(A2:A81=paid),"paid",(NOW())) 

But getting error. Is their any way to do it?


Solution

  • Try this:

    =ArrayFormula(IF(A2:A81="paid", NOW(),"")) 
    

    If you never want the date to change, you need to use script like this:

    function onEdit() {
      // writes the current date to the cell to the right on the row when a cell in a specific column is edited
      // adjust the following variables to fit your needs
    
      var sheetNameToWatch = "Sheet1";
      var columnNumberToWatch = /* column A */ 1; // column A = 1, B = 2, etc.
      var valueToWatch="paid";
    
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = SpreadsheetApp.getActiveSheet();
      var range = sheet.getActiveCell();
      var val=sheet.getActiveCell().getValue()
    
      if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && val==valueToWatch) {
        var targetCell = sheet.getRange(range.getRow(), range.getColumn()+1
                                   );
        targetCell.setValue("" + Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy"));
      }
    }
    

    There is not a way to do this with a formula that I know of. It can be made shorter, but script is still required:

    function onEdit(e) {
       if (e.source.getSheetName()=="Sheet1" && e.range.getColumn() == 1 && e.range.getValue() == "paid") {
          e.range.offset(0,1).setValue(new Date());
      }}