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() : '');
}
Related
I currently have a script written (from help of others on forums) that has worked great for years. I'm now trying to use on another spreadsheet and just need to make a minor tweak in how it works. Here is current script:
function onEdit() {
var sheetName = "Master List";
var s = SpreadsheetApp.getActiveSheet();
if (s.getName() !== sheetName) return;
var r = s.getActiveCell();
if( r.getColumn() != 5 ) { //checks the column
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, "GMT-05:00", "MM/dd/yyyy");
SpreadsheetApp.getActiveSheet().getRange('E' + row.toString()).setValue(time);
};
};
Its function previously was to put a date & timestamp in a specified column anytime an edit was made within a particular row. Basically it would tell me when the last edit was made on any given row.
Now I want column E (column 5) to be date stamped when only Column D (column 4) is edited. My use now is for price tracking. Column D will have the price and Column E will tell me the last time I updated the price in the spreadsheet.
I believe the edit needs made on the 5th line of code where it gets ActiveCell it needs a specified cell instead of any cell within the row. Please let me know the appropriate changes I should make. Thanks, Silas
function onEdit(e) {
const s = e.range.getSheet();
if (s.getName() == "Master List" && e.range.columnStart == 4 ) {
s.getRange(e.range.rowStart, 5).setValue(Utilities.formatDate(new Date(), "GMT-05:00", "MM/dd/yyyy"));
};
}
Just replace
if( r.getColumn() != 5 ) { //checks the column
with
if( r.getColumn() == 4 ) { //checks the column
I have a script I currently use to add a date stamp when the low inventory is changed on an item. I want it to remove the date stamp when the inventory cell is cleared (if the item went out of stock).
Currently, even when I delete the date stamp and inventory, it automatically puts the date stamp back in.
Basically, I only want a date stamp if there is information in the previous cell & want the date to update every time I change the info in the previous cell.
I am very new to scripts. Is that possible? Here is the script I am using that I found online:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Data" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 4 ) { //checks the column
var nextCell = r.offset(0, 1);
nextCell.setValue(new Date());
}
}
}
You want to take some action based on whether the current cell contains information or not. You can get the current value in the cell with .getActiveCell().getValue() and use that with if else to conditionally take some actions. In the example below, we set a timestamp if the value is truthy and clear the timestamp cell if the value is falsy.
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if (s.getName() !== "Data") return; // exit if not in "Data"
var r = s.getActiveCell();
if (r.getColumn() !== 4 ) return; // exit if not editing col D
var nextCell = r.offset(0, 1);
// make a stamp or remove the stamp based on current cell value
if (r.getValue())
nextCell.setValue(new Date())
else nextCell.clear();
}
Try this:
function onEdit(e) {
const sh=e.range.getSheet();
if(sh.getName()=="Data" && e.range.columnStart==4) {
const ts=Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"E MMM dd, yyyy HH:mm:ss");
if(e.value) {
e.range.offset(0,1).setValue(ts);//if D is not blank
}else{
e.range.offset(0,1).setValue('');//if D is blank
}
}
}
Format Date
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"))
}
}
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());
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('')};
}
}
}