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
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 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());
}
}
}
We have a shared google sheet with multiple sheets that need the same functionality per page. (additional sheets are being added a few times a year)
Short goal, check a box in column 1 and get a static timestamp in column 2. Uncheck a box in column 1 and column 2 contents are cleared.
I added a static timestamp to the cell to the right of the edited cell if it is blank. This way when a box is checked in column 1, the cell in column 2 receives a timestamp. This is working well (but might work better if based on the value of true for the checkbox)
The issue I am having is with creating a more reliable script to clear contents. Right now, if someone manually deletes the contents in column 2, the checkbox will start behaving backward because it is updating based on edit, not value.
Currently, if column 1 is edited, and the cell in column 2 is not blank, the contents are cleared. I would like this to clear only if the checkbox is unchecked (value changed to false). I have not figured out a way to make it work.
I have not been able to figure out how to make a script based on edit AND checkbox value. Is it possible?
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //is empty?
{
var time = new Date();
time = Utilities.formatDate(time, "GMT-6", "MM/dd/yy' - 'hh:mm a");
nextCell.setValue(time);
}
else{ nextCell.clearContent()}
};
}
All you need to do is add a check if the box is ticked or not, this can be achieved by using isChecked() on your cell.
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' && r.isChecked() )
{
var time = new Date();
time = Utilities.formatDate(time, "GMT-6", "MM/dd/yy' - 'hh:mm a");
nextCell.setValue(time);
}
else{ nextCell.clearContent()}
};
}
As you can see, all I've added is a check to make sure your checkbox is ticked. Here are your options for ticked/unticked:
//check if box is ticked
if( nextCell.getValue() === '' && r.isChecked() )
//check if box is unticked
if( nextCell.getValue() === '' && !r.isChecked() )
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());