Instead of having timestamp data appear offset a cell, I'd like it to appear in another sheet. I wouldn't like to use .tocopy method because the data may be removed(archived) in the active working sheet; timestamp data should remain in the next sheet when data is removed from working sheet.
Code snippet below:
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() == 1 ) { //checks the column
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
If you know the name of the sheet you want the date to be put into, just get a reference to that sheet, and set the value.
var sheetToWriteTo = s.getSheetByName('sheetName');
sheetToWriteTo.getRange(put range here).setValue(new Date());
Related
so I wrote a script that is working on edit and looking if the active cell in column A or column D. In column D I have a checkbox I want to fill the Column F with current time when the checkbox is true. You can see my script below. Currently it fills when column A is edited but It doesnt do anything if the checkbox is checked.
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, 4);
nextCell.setValue(new Date());
}
else if( r.getColumn() == 4 ) { //checks that the cell being edited is in column A
if(r.getValue() == "TRUE") {
var nextCell = r.offset(0, 2);
nextCell.setValue(new Date());
}
}
}
}
You want to put the date object to the column "F" when the checkbox at the column "D" is checked.
You want to run the script with the OnEdit event trigger.
Modification points:
In your script, I think that the if statement of if(r.getValue() == "TRUE") {} is required to be modified. In this case, when r.getValue() is the boolean type, r.getValue() == "TRUE" is always false. By this, the script in the if statement is not run. I think that this is the reason of your issue.
Modified script 1:
When your script is modified, please modify as follows.
From:
if(r.getValue() == "TRUE") {
To:
if(r.getValue() === true) {
or
if(r.isChecked()) {
In this case, also you can use isChecked().
Modified script 2:
When the event object is used, the process cost of your script can be reduced a little. Ref The sample script using the event object is as follows.
Sample script:
function onEdit(e) {
var range = e.range;
var sheet = range.getSheet();
if(sheet.getSheetName() == "Sheet1") {
if(range.columnStart == 1) {
var nextCell = range.offset(0, 4);
nextCell.setValue(new Date());
} else if(range.columnStart == 4 && range.isChecked()) {
var nextCell = range.offset(0, 2);
nextCell.setValue(new Date());
}
}
}
References:
getValue()
isChecked()
Event Objects
I am trying to create a timestamp for our Google Sheets script. The main goal is to create a script, which would make a timestamp in every "odd" column if something is added to any "even" column.
Right now, I have found this:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) {
var r = s.getActiveCell();
if( r.getColumn() == 13 ) {
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' )
var time = new Date();
time = Utilities.formatDate(time, "GMT", "HH:mm:ss");
nextCell.setValue(time);
};
};
}
It works perfectly, but unfortunately, only for the 13th column. How to make this code work for every even column?
You can use the modulo operator (as explained here) to check if the edited column is 'even' or 'odd'. Asuming you want the code to work on 'Sheet1', see if this code works for you
function onEdit(e) {
if(e.source.getActiveSheet().getName() !== 'Sheet1' ||
e.range.columnStart % 2 > 0) return;
var off = e.range.offset(0, 1);
if(!off.getValue()){
off.setValue(Utilities.formatDate(new Date(), "GMT",
"HH:mm:ss"))
}
}
I'm trying to make a KPI that is dependent on completion of task. And I need to time stamp in order to pull this off.
I can make regular time stamps. But what I need is that the time stamp needs to record in different sheet in the same spreadsheet.
I have a master sheet where my employees record and register all calls that come through. It's called "MASTER SHEET".
And a KPI SHEET where I need to record the time /time stamp here/
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "MASTER SHEET" )
var r = s.getActiveCell();
if( r.getColumn() == 2 )
var nextCell = r.offset(0, 1); <<<<<<<<<<<<<<<<<this is where I'm stuck.
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
This function only stamps in the same sheet so it doesn't work for me.
Try something like this:
function onEdit(e) {
if (e.source.getActiveSheet()
.getName() !== 'MASTER SHEET' || e.range.columnStart !== 2) return;
e.source.getSheetByName('KPI SHEET')
.getRange(e.range.rowStart, 4)
.setValue(e.value ? new Date() : '');
}
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());
}
}
}