1
votes

i am stuck to writing a google sheet script to combine two column and update column automatically.

i have two column "DATE" and "TIME" . which are manual entry(cannot be automatically insert ). so i want to try if both column are filled than another column "DateTime" will be filled automatically.

DATE                Time              DateTime
---------------------------------------------------------
2020-03-31          1:00PM           2020-03-31 13:00:00

i used this function> =concatenate(text(A2,"yyyy-mm-dd")&" "&text(B2,"hh:mm:ss"))

but how i can write this under script! this is my googlesheet link: https://docs.google.com/spreadsheets/d/1acvlfNbhh-nU_nTb-9J1vZfGobStQohJSuMTynS6vJM/edit?usp=sharing

here is my function:

function onEdit(event)
{ 
  var timezone = "IST";
  var timestamp_format = "MM-dd-yyyy hh:mm:ss a"; // Timestamp Format. 
  var updateColName = "DATE";
  var updateColName2 = "Time";
  var timeStampColName = "DateTime";
  var sheet = event.source.getSheetByName('Sheet1'); //Name of the sheet where you want to run this script.


  var actRng = event.source.getActiveRange();
  var editColumn = actRng.getColumn();
  var index = actRng.getRowIndex();
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();

  var dateCol = headers[0].indexOf(timeStampColName);
  var updateCol = headers[0].indexOf(updateColName); 
  var updateCol2 = headers[0].indexOf(updateColName2);

  updateCol = updateCol+1;
  updateCol2 = updateCol2+1;


  if (dateCol > -1 && index > 1 &&  editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
    if (dateCol > -1 && index > 1 && editColumn == updateCol2){

       var cell = sheet.getRange(index, dateCol + 1);
       var date = concatenate(text(A2,"yyyy-mm-dd")&" "&text(B2,"hh:mm:ss"))

       cell.setValue(date);
    }
  }
}

this function not working. please anyone help me.

1
why would you need a script? You can simply use and if statement to see if there any entry both date and time columns and use the Concatenate formula. I have added this in your sheet. Please have a lookGangula
thanks man. working fine.Dhurjati Riyan

1 Answers

2
votes

If you need to add a time value to a date value you just need to add them together (not concatenate) as formatting aside they are internally just numbers.

=ARRAYFORMULA(IF((A:A <> "") * (B:B <> ""), A:A + B:B,))

enter image description here

On the column D:D I have set formatting to Date time.