简体   繁体   中英

Apps Script : Timestamp is not copying properly

When I am copying timestamp from one sheet to another or in the same sheet only then it is not copying the time from the timestamp. It is only copying date. See the below image -

在此处输入图片说明

Here is my code -

function myFunction() {

  var files = DriveApp.getFilesByName('myfile');
  while (files.hasNext()) {
  var file = files.next();
  var ss = SpreadsheetApp.open(file); 
  var sheet = ss.getSheets()[0];
  var lastRow = sheet.getLastRow();

  var range = sheet.getRange(1,1,lastRow,2);
  var values = range.getValues();

  Logger.log(values);

  var range = sheet.getRange(sheet.getLastRow()+1,1,lastRow, 2);
  range.setValues(values); 
  }
}

The logger contains this information -

[18-10-20 11:18:17:743 PDT] [[TIMESTAMP, EVENT], [Mon Oct 01 05:10:13 GMT+05:30 2018, Event 1], [Mon Oct 01 05:10:16 GMT+05:30 2018, Event 2], [Mon Oct 01 05:10:17 GMT+05:30 2018, Event 3], [Mon Oct 01 05:10:24 GMT+05:30 2018, Event 4], [Mon Oct 01 05:10:26 GMT+05:30 2018, Event 5], [Mon Oct 01 05:10:32 GMT+05:30 2018, Event 6], [Mon Oct 01 05:10:38 GMT+05:30 2018, Event 7], [Mon Oct 01 05:10:38 GMT+05:30 2018, Event 8], [Mon Oct 01 05:10:45 GMT+05:30 2018, Event 9], [Mon Oct 01 05:11:07 GMT+05:30 2018, Event 10]]

What could be the reason?

Ok. Answering my own question because I found the answer before someone answered. Just add this formatting code at the bottom to change the format of that cell to your specified format as suggested by TheMaster

sheet.getRange("A:A").activate();
sheet.getActiveRangeList().setNumberFormat('M/d/yyyy H:mm:ss');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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