0

I have been messing around to build a google worksheet with script. So basically, in this file whenever there is input on column 1 and 4, there will be a timestamp shown in the next cell. And when the input is removed, the timestamp is clear. Everything runs fine so far, however as some weird issue happen. If instead of input in column 1 & 4, I paste values to the range, no timestamp appear or if I paste with the timestamp, the pasted timestamp will disappear. I don't know what happened or if you are clear on what I'm trying to ask. Anyway, here the script I'm using:

function onEdit(e) {
  var s = SpreadsheetApp.getActiveSheet();
  var r = e.range;
  var nextCell = r.offset(0, 1);
  if( s.getName() == "Nhap" && (r.getColumn() == 1 || r.getColumn() == 3) ){ 
    if( nextCell.getValue() === '' && e.value !=null)
    {
       var time = Utilities.formatDate(new Date(), "GMT+7", "MM/dd/yyyy HH:mm:ss");
       nextCell.setValue(time);
    }  
     else{ nextCell.clearContent()}
   };

}

Thanks

  • CAn you explain by what you mean with "I paste values to the range"? Do you mean paste values into more than one cell at once? Or setting values programamtically? – ziganotschka Jan 13 '21 at 08:36

1 Answers1

0

e.value undefined when copy/pasting range:

When a range is copied, instead of manually inputting a value on a cell, the value parameter from the corresponding event object (e.value) is undefined. This is a known behaviour and it has been reported several times in Issue Tracker before:

While this is still under investigation, this doesn't seem to be intended behaviour. While the description for the value parameter says it will only be populated when the edited range is a single cell,

value: New cell value after the edit. Only available if the edited range is a single cell.

This behaviour also happens when the copied/pasted range is a single cell. See this related answer from Diego.

Issue:

Since e.value is undefined when pasting the range, the following condition is never true:

if( nextCell.getValue() === '' && e.value !=null)

So the code goes to:

else{ nextCell.clearContent()}

That is, the timestamp cell is cleared of content.

Solution:

You can avoid this by changing e.value to r.getValue() (see Range.getValue()), which will be populated even when the range is copy/pasted:

if( nextCell.getValue() === '' && r.getValue() !=null)

Another issue:

Also, in your current code, when the timestamp cell is not empty, this condition is always false, so the timestamp cell is cleared:

if( nextCell.getValue() === '' && e.value !=null)

This is disregarding whether the range is pasted or the cell is inputted manually. If you don't want this to happen, remove nextCell.getValue() === '' from your condition.

Edit:

If you want to remove timestamp when cell is emptied, check that cell value is not an empty string ("" !== null):

if (r.getValue() !="")
Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • Wow! Really appreciate your comprehensive response. I will add these suggested change to my script! – Nguyễn Thành Long Jan 13 '21 at 09:58
  • I made the changes but now when I empty columns 1 and 4, the timestamp not removing itself. If I keep nextCell.getValue() === '', I can now paste value but as you said, if I was to paste into the timestamp columns they will get deleted. – Nguyễn Thành Long Jan 13 '21 at 10:30
  • @NguyễnThànhLong This is expected since empty string is not the same as null. Check `Edit` section at the bottom of my answer. – Iamblichus Jan 13 '21 at 10:38
  • Ok, I don't want to bother but now when I tried to paste to columns 1 & 2, a timestamp appears on column 3... Really appreciate your help so far! – Nguyễn Thành Long Jan 13 '21 at 11:14
  • @NguyễnThànhLong Why would you paste to both columns? This kind of breaks your original setting. And since you are using `offset(0,1)`, it's expected that you'll get a timestamp at column 3. What should happen when pasting columns 1&2? – Iamblichus Jan 13 '21 at 11:31
  • Yeah, you're right. The pasting is because I try to get data from old files but i guess that not really necessary. Thanks for your help lamblichus! – Nguyễn Thành Long Jan 13 '21 at 13:13
  • Say I don't want to use offset(0,1) but rather if there is input in column 1, a timestamp will appear on column 2, and if there is input in column 3, a timestamp will appear on column 4. Is there a way to do this? I try to search online but only able to find a solution for 1 pair not 2 pairs simultaneously in 1 function. Hope you could help me with this. Thanks – Nguyễn Thành Long Jan 15 '21 at 04:46
  • @NguyễnThànhLong Yes that's perfectly possible. I'd suggest you to post a new question if you have problems doing that. – Iamblichus Jan 15 '21 at 09:04