1

I am trying to run a script that auto-populates the date in column F whenever data is entered or changed in column E in a spreadsheet with multiple sheets.

I currently have a script which was written for me based on what I described but I was getting an error code. I went back to my original question and explained that this script was needed to run on a single sheet within a spreadsheet with multiple sheets. He gave me some additional information but this was his last answer "yes, you will need to replace event.source.getActiveSheet() with something like "getsheetbyname". I have tried everything that is within my limited knowledge to work with what he gave me but I am still getting an error code no matter what I try.

Here is the area that is giving me the error.

var sheet = event.source.getsheetbyname();

Here is the error code I am receiving

TypeError: Cannot read property "source" from undefined. (line 2, file "Code")

I am aware that there needs to be the name of the sheet I am wanting the script to run on but I do not know how to code it to do so. The name of the sheet is "Juvenile weights"

The expected results should be when I add or change the data in column E it should auto-populate the current date into the adjacent cell in the adjacent column.

When I save the script and then run the script to make sure it's working, of course, it gives me the error code I described above. Then when I go back to that sheet after saving the script instead of auto-populating the date, now when I highlight a string of cells in a row it changes all the following cells to the same information I have in the first cell I started highlighting. Very odd!

The script + error code:

enter image description here

The code:

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

  // note: actRng = the cell being updated
  var actRng = event.source.getActiveRange();
  var index = actRng.getRowIndex();
  var cindex = actRng.getColumnIndex();

  if (cindex == 5) { // 1 == Column A, 2 == Column B, 3 == Column C, etc.
   var dateCol = sheet.getLastColumn();  
   var lastCell = sheet.getRange(index,dateCol);
   var date = Utilities.formatDate(new Date(), "EST", "MMM-dd-yyyy");
   lastCell.setValue("'" + date);
  }
}
2
  • You can't run this function from the script editor without supplying the event object. Commented Apr 9, 2019 at 20:38
  • 2
    Possible duplicate of How can I test a trigger function in GAS? Commented Apr 9, 2019 at 21:28

2 Answers 2

1

I believe this does the same thing:

function onEdit(e) { 
  var sheet = e.range.getSheet();
  if (e.range.columnStart == 5) {
    sheet.getRange(e.range.rowStart,sheet.getLastColumn()).setValue("'" + Utilities.formatDate(new Date(), "EST", "MMM-dd-yyyy HH:mm:ss"));
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Here's the issue. He gave me a script that works for one sheet by itself. I wanted to use it within a Spreadsheet with multiple sheets and there's no way I can just copy that script and transfer it from the original sheet to the sheet I want to use it for. Using it alone it works fine but using it with the sheet I need it to work with, It does absolutely nothing. Which is why I am still here asking for help.
If you're talking about the script in your answer under "The Code:" that will work on all sheets and it does about the same thing that mine does except that I like to take advantage of the data in the event block as much as possible to save time because this code has to run in 30 seconds.
And keep in mind that this code only runs when there is a physical editing of content by a user in column E
0

Is there a reason you aren't using SpreadsheetApp.getActiveSheet().getSheetByName("Juvenile weights");

5 Comments

It's still giving me an error code TypeError: Cannot find function getSheetByName in object Sheet. (line 2, file "Code")
How are you running it? And is the code that you're running the same as the code in your question?
I tried it with the code that @J. G. gave me with the ``` SpreadsheetApp.getActiveSheet().getSheetByName("Juvenile weights");
Just wanted to let you know I was able to get it to work @Cooper so thanks for all of your help with this.
Please checkoff one of the answers. Your choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.