0
votes

I have this code that I found online and it works as needed, but it works for all my sheets or the tabs at the bottom if you want to call them. I want it to work

function onEdit(event) 
{
    var sheet = event.source.getActiveSheet();
    var eventRange = event.source.getActiveRange();
    var eventColumn = eventRange.getLastColumn();

    if (eventColumn == 1) 
    {
      var stampRange = eventRange.offset(0,10);
      stampRange.setValue(new Date());

    }
} 

This is the original code, I tried adding in line 4 the following but i can't get it to work. I'm not experienced with javascript but I need your help as i'm trying my best. Thank you.

if(sheet.match(/*.13/)){

This is the line I added. Based on my reading online, the script should works only if the sheet name ends with 13. But it's not working.

1

1 Answers

0
votes

You've got the right idea, but sheet is a Sheet Object, while .match() is a String method. Use the Sheet.getSheetName() method to get the name of the sheet (the words on the tab).

In an onEdit(), you usually want to bail out without investing much processing time, so you should put the test for the sheet name as early as possible.

If you want to match "Sheet13" exactly, you should test for just that - because your regex will also match "Apollo13" and "a13a", for example.

function onEdit(event) 
{
    var sheetName = event.source.getActiveSheet().getSheetName();

    if (sheetName.match(/.13/) == null)
      // These aren't the droids you're looking for...
      return;

    var eventRange = event.source.getActiveRange();
    var eventColumn = eventRange.getLastColumn();

    if (eventColumn == 1) 
    {
      var stampRange = eventRange.offset(0,10);
      stampRange.setValue(new Date());

    }
}