I am creating a sheet that once a job is assigned to someone, the time is automatically input to the cell beside it.... and when complete, the time is automatically put into the cell beside that one.
I have included a picture of the idea I am going for. Any help would be appreciated. Thanks!
If you set the trigger, it should be enough for you to + adjust the condition there.
I don´t know, when you need fixed date?
function insert_timestamp() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
var tz = Session.getScriptTimeZone();
if( r.getColumn() != 0 ) {
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, tz, "dd.MM.yyyy");//"dd.MM.yyyy hh:mm:ss");
SpreadsheetApp.getActiveSheet().getRange('C' + row.toString()).setValue(time);
}
}
There is an onEdit trigger for the SpreadsheetApp which you can use. Basically, every time you edit a cell, the onEdit function runs and passes and event. You can then check that event against some criteria and gof rom there. The documentation below does not seem up to date, but the code did work for me. Finally, you can then select how you want the date displayed using the format settings in the Spreadsheet.
function onEdit( event ) {
const currentRow = event.range.rowStart;
const currentCol = event.range.columnStart;
const value = event.value
// if it is in in Column F (6) or Column H (8)
// you could also check the value here against something
if( currentCol === 6 || currentCol === 8 ){
// put the current time in the column next to the current one
SpreadsheetApp.getRange( currentRow, currentCol + 1).setValue( new Date() )
}
}
See here for reference: onEdit
I have tried this code below to add time stamps just to 6 and 8. but it input a time stamp beside every cell i edit..
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Work Orders" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 6, 8 ) { //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());
}
}
}
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'm trying to track the date at which a particular stage is selected from a dropdown list, so that I can measure the time between stages. Here is the spreadsheet: https://docs.google.com/spreadsheets/d/1GEJKCtLgsIobA0u3_GYqYwA6FQuSi64u4ORgTATn92U/edit?usp=sharing
I've been working with different examples to record the timestamp when a cell is changed, but I can't seem to find a way to record a timestamp in different cells dependent upon the value selected in the dropdown.
Does anyone know if something like this is possible? A function like this will get me the timestamp in a single column/cell, but I want to be able to populate the different timestamps corresponding to the value in the dropdown in different cells:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Tracking" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 2 ) { //checks the column
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //is empty?
var time = new Date();
time = Utilities.formatDate(time, "GMT", "HH:mm:ss");
nextCell.setValue(time);
};
};
}
Solution:
You would need to have some parsing of the cell value to separate the number component, compute the offset cell, and write the timestamp there.
Note that my modified code also uses the event object e, as this lessens the calls to SpreadsheetApp and makes each execution a little bit faster.
Sample Code:
function onEdit(e) {
var r = e.range;
var off = 0;
if (r.getSheet().getName() == "Tracking") { //checks that we're on the correct sheet
if (r.getColumn() == 2) { //checks the column
var arr = e.value.split(" "); // parse the cell value
if (arr[0] == "Start") {
off = 1;
}
else if (arr[0] == "Stage") {
off = parseInt(arr[1]) + 1; // compute offset
}
else return;
var nextCell = r.offset(0, off);
if (nextCell.getValue() === '') { //is empty?
var time = new Date();
time = Utilities.formatDate(time, "GMT", "HH:mm:ss");
nextCell.setValue(time);
}
}
}
}
Sample Output:
References:
Event Objects | onEdit(e)
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
Please help with setting active user in Column N. This script will timestamp column M whenever a cell in a row is updated. However, nothing updates in Column N for the active user.
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Log" ) { //checks that we're on the correct
sheet
var r = s.getActiveCell();
var user = Session.getActiveUser().getEmail();
if( r.getColumn() != 13 ) { //checks the column
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
SpreadsheetApp.getActiveSheet().getRange('M' + row.toString()).setValue(time);
SpreadsheetApp.getActiveSheet().getRange("N" + row.toString()).setValue(user);
}
};
};
Am I using setValue(user) incorrectly?
Your call to setValue() looks fine. You may be encountering an authorization issue.
Can you successfully run this same code outside of the onEdit() trigger?
There are restrictions on the actions that can be performed inside of an onEdit trigger. See https://developers.google.com/apps-script/guides/triggers/
I found this script and am trying to use it in a google sheet. The trouble I am having is that I get an error message
You do not have permission to call setValue (line 8).
and when I change a value it will only create the timestamp for the first entry in a cell. If I edit the cell the timestamp does not change. Could someone take a look at this script to see what is wrong.
Here is the sheet
This is the script I am using:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() != 2 ) { //checks the column
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, "GMT-08:00", "MM/DD/yy, hh:mm:ss");
SpreadsheetApp.getActiveSheet().getRange('M' + row.toString()).setValue(time);
}
}
In order to run the script I typed =onedit(a3:l3) it seemed like the only way to get it to work at all.
Thanks for any help
Try making the following commented changes to your code
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() != 2 ) { //checks the column
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, "GMT-08:00", "MM/DD/yy, hh:mm:ss");
// remove this line below
SpreadsheetApp.getActiveSheet().getRange('M' + row.toString()).setValue(time);
//replace with this line below
return time;
}
}
Reference
You cannot set values to a cell outside of where the custom formula is being set on the spreadsheet, its illogical.
If you want to date/timestamp col M when edits are done in col B, try something like this:
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if(s.getName() !== 'Sheet1' || e.range.columnStart != 2 || e.range.rowStart < 2) return;
e.range.offset(0, 11).setValue(e.value ? new Date() : "");
}
As this is an onEdit there is no need to enter a formula (=onedit()) in the spreadsheet. Note that:
- erasing a value in col B will cause the stamp to disappear (if you don't want this, just let me know).
- format col M as date/time via the 123-button.
See if this helps ?