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
Related
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 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());
}
}
}
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 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 ?
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('')};
}
}
}