简体   繁体   中英

Timestamp to specific sheet in google sheets

 function onEdit() { var s = SpreadsheetApp.getActiveSheet(); if (s.getName() == 'Wykaz poselski') { var rng = SpreadsheetApp.getActiveSheet().getRange('C7'); rng.setValue(new Date());} }

That code returns only hour in my spreadsheet, but I need full date (YYYY:MM:DD HH:MM:SS) Could you help me?

Adding DateTime to sheet on every edit

function onEdit(e) {
  var sh=e.range.getSheet();
  if (sh.getName()=='Wykaz poselski') {
    var ts=Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "yyyy:MM:dd HH:mm:ss")
    sh.getRange('C7').setValue(ts);
  }
}

Solution

To get the full date value you will need to use the formatDate method of Utilities . Here is the sample code with the right modification:

 function onEdit() { var s = SpreadsheetApp.getActiveSheet(); if (s.getName() == 'Wykaz poselski') { var rng = SpreadsheetApp.getActiveSheet().getRange('C7'); rng.setValue(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss"));} }

I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

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