I need some help in making an auto updating timestamp script for my google spreadsheet. What I exactly need is that, when someone edits column A or B, a date and time will appear on column D.
I tried using this script
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 4 ) { //checks the column
var nextCell = r.offset(0, 3);
//if( nextCell.getValue() !== '' ) //is empty?
nextCell.setValue(new Date());
}
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, 2);
//if( nextCell.getValue() !== '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
It worked fine for me, but when I share it, it won't work others. I have close to zero experience with codes/scripts, so can someone please help me?
Related
I have some difficulty searching and creating a script. What I am trying to achieve is getting the user's email/name when they edited a cell in my spreadsheet. I tried getactiveuser but its not showing anything. I read that some ask to use geteffectiveuser but all I get is still my username back and not the other editor.
Here is my script so far:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "TEST" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
var email = Session.getActiveUser().getEmail();
if( r.getColumn() == 4 ) { //checks the column for HUB side
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(email());
}
}
}
Logger.log(Session.getActiveUser().getEmail());
As of the above, nothing is showing up.
You may perform a test on the following code, it work for me :)
function onEdit(e) {
var s = e.source.getActiveSheet();
if( s.getName() == "TEST" ) { //checks that we're on the correct sheet
var r = e.range;
if( r.getColumn() == 4 ) { //checks the column for HUB side
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(Session.getActiveUser().getEmail());
}
}
}
Hi I am using this script to timestamp my entries in google sheet but its only giving me date when data is entered manually , its not working when data is pasted in multiple cell or is when query is used . how can i make it work when data is pasted or queried .
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "data02" ) {
var r = s.getActiveCell();
if( r.getColumn() == 1 ) {
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' )
nextCell.setValue(new Date());
}
}
}
Try this:
function onEdit(e) {
//e.source.toast('entry');
const sh = e.range.getSheet();
if (sh.getName() == "data02" && e.range.columnStart == 1 && e.range.columnEnd == 1) {
//e.source.toast('cond');
sh.getRange(e.range.rowStart, 2, e.range.rowEnd - e.range.rowStart + 1, 1).setValue(new Date());
}
}
Working on a sheet that when the user edits a cell (example C3), it will insert his/her email (name). Have a scrip that does work for only one column ("C"), but when moved to another sheet or column ("5 or E") it gives “Error – you do not have permission to call getActiveUser (line 41)".
Because it does work once, not sure what I am missing or why the same script wont work at all on another sheet. Thanks for any assistance.
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
var email = Session.getActiveUser().getEmail();
Logger.log(email);
if( r.getColumn() == 3 ) { //checks the C column
var nextCell = r.offset(0, 1);
nextCell.setValue(email);
if( r.getColumn() == 5 ) { //checks the E column
var nextCell = r.offset(0, 1);
if( nextCell.getValue() !== '' ) //is empty?
nextCell.setValue(email);
}
}
}
Make sure you are the owner of the spreadsheet and you have granted the necessary permission to run the script.
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if (s.getName() == "Sheet2") { //checks that we're on the correct sheet
var r = s.getActiveCell();
var email = Session.getActiveUser().getEmail();
if (r.getColumn() == 3) { //checks the C column
var nextCell = r.offset(0, 1);
nextCell.setValue(email);
}
if (r.getColumn() == 5) { //checks the E column
var nextCell = r.offset(0, 1);
Logger.log(nextCell.getValue());
if (nextCell.getValue() == '') //is empty?
nextCell.setValue(email);
}
}
}
I have a script for Google Docs spreadsheet where, when any cell in a specific column is populated it automatically places a timestamp in a different column on that row:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 9 ) { //checks the column
var nextCell = r.offset(0, 3);
if( nextCell.getValue() === '' ) //is empty?
var time = new Date();
time = Utilities.formatDate(time, "Canada/Eastern", "M/dd/Y, h:mm:ss");
nextCell.setValue(time);
};
};
}
The script works great. However, I'd like the script to delete that timestamp if said cell has been cleared.
Or perhaps I can put the question a bit differently: How can I clear a specific cell when another specific cell in that row has been cleared?
the code is almost the same, I simplified it a bit to make it easier to understand what it's doing.
Edit :
better code to prevent issues when just modifying a cell in col 9.
I used the parameter coming in the onEdit trigger (called e in this example), see docs for details on that.
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
Logger.log(JSON.stringify(e)+' ------------------> value = '+e.value);
var r = e.range;
var nextCell = r.offset(0, 3);
if( s.getName() == "Sheet1" && r.getColumn() == 9 ){ //checks that we're on the correct sheet & column
if( nextCell.getValue() === '' && e.value !=null){ //is empty?
var time = new Date();
time = Utilities.formatDate(time, "Canada/Eastern", "M/dd/Y, h:mm:ss");
nextCell.setValue(time);
}else{
if(e.value==null){ nextCell.setValue('')};
}
}
}
I'm not sure what my issue is here. I have an office visitor sign in/out Google spreadsheet. This is how I'm trying to get it to work. When a visitor selects a checkbox in the "in" column a timestamp will display in the next column. When a visitor selects a checkbox in the "out" column a timestamp will display in the next column. Currently this is how it's working: When I have only the first function in the code editor a timestamp displays in column 4 - like it's supposed to. When I add the second function a timestamp displays in column 6, but not in column 4. It's like the second function is overriding the first. What would correct the issue?
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "SignInOut" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 3 ) { //checks the column
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "SignInOut" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
Both functions have the same name, so it will run the last.
Perhaps you can avoid having to define 2 times the same function, try something like:
function onEdit(e) {
var s = e.source.getActiveSheet(), r, colCell, nextCell;
if(s.getName() === 'SignInOut') { //checks that we're on the correct sheet
r = e.range; //s.getActiveCell();
colCell = r.getColumn();
if(colCell === 3 || colCell === 5) { //checks the column
nextCell = r.offset(0, 1);
if(nextCell.getValue() === '') //is empty?
nextCell.setValue(new Date());
}
}
}