Home > Mobile >  Datetime Formatting in Google Apps Script onEdit()
Datetime Formatting in Google Apps Script onEdit()

Time:10-27

I want to format datetime input value on onEdit event.

ex) 2021/10/25 15:00 -> Oct 25, 2021 3:00 PM

I put the same value (2021/10/25 15:00) on the sheet.

  1. When I get value by using SpreadsheetApp Class, it comes in with normal datetime value.

     SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(6, 6).getValue()
    

    result : Oct 25 15:00:00 GMT 09:00 2021

  2. But when using event object on onEdit, it comes in Num type value.

     onEdit(e){Logger.log(e.value)}
    

    result : 44494.625

Is there any way to convert Number to Datetime?

CodePudding user response:

Solution:

Using e.value cannot return Date data type, so it gets converted into Number.

You could use e.range.getValue() instead.

function onEdit(e) {
  Logger.log(e.value);
  Logger.log(e.range.getValue());
}

Execution Log:

enter image description here

  • Related