Date/Time stamp script not working

116 views
Skip to first unread message

Sarah Powell-Underhill

unread,
Feb 22, 2021, 12:15:25 PM2/22/21
to Google Apps Script Community
Here is the script I am using. After I run it and then go back to the sheet to test it nothing happens. I will add I am new to scripting so it is probably an easy fix.

function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks that the cell being edited is in column A
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //checks if the adjacent cell is empty or not?
nextCell.setValue(new Date());
}
}
}

Alan Wells

unread,
Feb 22, 2021, 1:09:40 PM2/22/21
to Google Apps Script Community
You can test for whether the function is running or not by putting either a
Logger.log('it ran')
or
console.log('it ran')
statement at the top of the function.

function onEdit() {
  Logger.log('it ran') 

Then you can view the log print out.

If you are using the new code editor, then click the Execution Log toggle button to show
or hide the execution log.

For the legacy editor, use "View" "Logs"
For information about troubleshooting see the documentation at:

If the code is failing either of your tests
if( s.getName() == "Sheet1" ) {
Then it won't set the value.

You can check what the code is getting for the sheet name.
Logger.log(' s.getName() ' +  s.getName())

Code often gives unexpected results even when you think the outcome would be obvious.

cbmserv...@gmail.com

unread,
Feb 22, 2021, 2:57:07 PM2/22/21
to google-apps-sc...@googlegroups.com

I did a quick test on this script and it seems to be working fine as far as I can tell. But may not be working to how you expect it.

 

The way it works now if if a cell in column A is edited, then it will stick today’s date in the same row in Column B.

 

Is this not what you want?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/18808b23-991a-475d-a97c-6fc413705e48o%40googlegroups.com.

Sarah Powell-Underhill

unread,
Feb 23, 2021, 8:27:45 AM2/23/21
to google-apps-sc...@googlegroups.com
I need the columns to be B and C

CBM Services

unread,
Feb 24, 2021, 12:13:39 PM2/24/21
to google-apps-sc...@googlegroups.com
Then just change the if statement to check for column 2 instead of column 1.

Nothing else needs changing.

From: Sarah Powell-Underhill
Sent: ‎2021-‎02-‎23 5:27 AM
To: google-apps-sc...@googlegroups.com
Subject: Re: [Apps-Script] Date/Time stamp script not working

Reply all
Reply to author
Forward
0 new messages