google-sheetsgoogle-apps-script

How to add plus 5 hour to the copy Cell and past in other with plus time


I have a script that copies and pastes the value to the blank cell.

Now I have one more issue, the script is the timestamp copy-paste.

How can I add 5 Hours to the time that is copied from Cell?

The date format is 31.01.2024 13:02:34.

enter image description here

This is the code I use for the copy-paste if the cell is blank.

function fill() {
  var tss = SpreadsheetApp.getActiveSpreadsheet();
  var values = tss.getRange("data!C:C").getValues();

  for(i=0; i<values.length; i++) {
    var value = values[i][0];

    if (value == "") {

      var rangeA = tss.getSheetByName("data").getRange(i+1,2);
      var rangeB = tss.getSheetByName("data").getRange(i+1,3,+'0,125');

      if(!rangeA.isBlank()) {
        rangeA.copyTo(rangeB,{contentsOnly:true})
      }
    }
  }
}

Solution

  • From the date format is 31.01.2024 13:02:34, I guessed that your actual cell values of columns "B" and "C" might be the date object. In this case, how about the following modification?

    Modified script:

    function fill() {
      var tss = SpreadsheetApp.getActiveSpreadsheet();
    
      // I modified the below script.
      const sheet = tss.getSheetByName("data");
      const range = sheet.getRange("B1:C" + sheet.getLastRow());
      const timeZone = tss.getSpreadsheetTimeZone();
      const values = range.getValues().map(([b, c]) => [b, c || Utilities.formatDate(new Date(b.getTime() + (5 * 60 * 60 * 1000)), timeZone, "HH:mm:ss")]);
      range.setValues(values);
    }
    

    When this script is run, the value of HH:mm:ss is put into column "C" when column "C" is empty. In this case, the output value uses the format HH:mm:ss.

    If you want to use dd.MM.yyyy HH:mm:ss, how about the following modified script?

    function fill() {
      var tss = SpreadsheetApp.getActiveSpreadsheet();
    
      // I modified the below script.
      const sheet = tss.getSheetByName("data");
      const range = sheet.getRange("B1:C" + sheet.getLastRow());
      const timeZone = tss.getSpreadsheetTimeZone();
      const values = range.getValues().map(([b, c]) => [b, c ? Utilities.formatDate(c, timeZone, "dd.MM.yyyy HH:mm:ss") : Utilities.formatDate(new Date(b.getTime() + (5 * 60 * 60 * 1000)), timeZone, "dd.MM.yyyy HH:mm:ss")]);
      range.setValues(values);
    }
    

    When this script is run, the values are put into column "C" with the format dd.MM.yyyy HH:mm:ss.

    If your column "B" includes both date object and string, how about the following modification?

    function fill() {
      var tss = SpreadsheetApp.getActiveSpreadsheet();
    
      // I modified the below script.
      const sheet = tss.getSheetByName("data");
      const range = sheet.getRange("B1:C" + sheet.getLastRow());
      const timeZone = tss.getSpreadsheetTimeZone();
      const values = range.getValues().map(([b, c]) => {
        if (!c && b instanceof Date) {
          return [b, Utilities.formatDate(new Date(b.getTime() + (5 * 60 * 60 * 1000)), timeZone, "HH:mm:ss")]
        } else if (!c && typeof b == "string") {
          return [b, Utilities.formatDate(new Date(Utilities.parseDate(b, timeZone, "HH:mm:ss") + (5 * 60 * 60 * 1000)), timeZone, "HH:mm:ss")]
        }
        return [b, c];
      });
      range.setValues(values);
    }
    

    If you want to use the format dd.MM.yyyy HH:mm:ss, please modify the above script.