add timestamp when specific cell = "complete" - google-apps-script

I have the following script that will add a timestamp to column 11 when column 4 in the same row and sheet is changed.
I now want to charge it so that it only charges when the text "Complete" is what is added to column 4
function onEdit(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
if(col === 4 && row > 4 && e.source.getActiveSheet().getName() ==="Sheet1" ) {
e.source.getActiveSheet().getRange(row,11).setValue(new Date());
}
}

var value = e.range.getValue();
if(col === 4 && row > 4 && e.source.getActiveSheet().getName() ==="Sheet1" && value === "Complete") {.....

Related

TImestamp Apps First DateEntered freeze

Hi I want to show Onlt The first date entered, so whenever someone change it
the timestamp WONT change
what can i use for it? like filter?
my code is like this
if C&E&F is filled
then timestamp
the timestamp should be freezed once someone input it, when someone change it the timestamp WONT CHANGET
function onEdit(e) {
const editedRange = e.range
const sheet = editedRange.getSheet()
var row = editedRange.getRow();
if (
sheet.getName() === 'Data' && ///cari sheet yang dipakai
(editedRange.getColumn() == 4 || editedRange.getColumn() == 5 || editedRange.getColumn() == 6) && ///jika keisi
(sheet.getRange(row, 4).getValue() !== "" && sheet.getRange(row, 5).getValue() !== "" && sheet.getRange(row, 6).getValue() !=="") ///jika tidak diisi
)
{
const timestampRange = sheet.getRange(editedRange.getRow(), 1, editedRange.getNumRows(), 1)
timestampRange.setValue(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "d/MM/yyyy HH:mm:ss")); /// memunculkan timestamp pada colum ke1/A yg dituju
}
}
This version requires that all three of d,e and f must be filled in order to get a time stamp and once you get a timestamp then it does not change unless the user changes it manually
function onEdit(e) {
//e.source.toast("Entry1");
const sh = e.range.getSheet()
if (sh.getName() == 'Sheet0' && e.range.columnStart > 3 && e.range.columnStart < 7 && e.value) {
let x = sh.getRange(e.range.rowStart,4,1,3).getValues()[0].every(e => e );//This requires d,e and f to be filled or truthy
//e.source.toast("outer gate " + x);
if (sh.getRange(e.range.rowStart, 1).getValue() == '' && x) {
//e.source.toast("inner gate")
sh.getRange(e.range.rowStart, 1).setValue(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "d/MM/yyy HH:mm:ss"))
}
}//if column is null the set the timestamp otherwise do nothing
}
Demo:

Date Stamp in multiple Columns Google Sheets

I have started using this script to auto populate the date when data is entered into the column next to it for Google Sheets. It needs to do this on multiple columns as new data is entered. This script works on all of the columns but the last paired: 13-14 and 15-16. It was working before but now it has stopped. Any help with what I need to do? I'm not versed in code and I learned this much from YouTube! Thanks!
function onEdit(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
if(col == 5 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" ) {
e.source.getActiveSheet().getRange(row,6).setValue(new Date())
if(col == 7 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" )
e.source.getActiveSheet().getRange(row,8).setValue(new Date())
if(col == 9 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" )
e.source.getActiveSheet().getRange(row,10).setValue(new Date())
if(col == 11 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" )
e.source.getActiveSheet().getRange(row,12).setValue(new Date())
if(col == 13 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" )
e.source.getActiveSheet().getRange(row,14).setValue(new Date())
if(col == 15 && row > 1 && e.source.getActiveSheet().getName() === "IRLA Level" )
e.source.getActiveSheet().getRange(row,16).setValue(new Date())
}
}
Try it this way
function onEdit(e) {
const sh = e.range.getSheet();
const idx = [5,7,9,11,13,15].indexOf(e.range.columnStart);
if(sh.getName() == "IRLA Level" && e.range.rowStart > 1 && ~idx) {
e.range.offset(0,1).setValue(new Date());
}
}
Bitwise Not

Apps Script IF columns are filled THEN timestamp, IF columns are Empty then CLEAR

I have column A which is supposed to hold time. If any data is entered in columns B through G then a timestamp should be entered into Column A and then remain static. If ALL of the data in those columns (Column B through G is empty) then the timestamp in A (if it was there) should clear.
Currently the script I have is working except for clearing the row in column A. I know I'm probably missing something silly, but I can't get it right!
function checkinTimeStamp(e){
var s = SpreadsheetApp.getActive();
var range = e.range;
var column = range.getColumn();
if (column == 2 || column == 3 || column == 4 || column == 5 || column == 6 || column == 7){
var timestamp = Utilities.formatDate(new Date, s.getSpreadsheetTimeZone(), "hh:mm");
var row = range.getRow();
var value = e.value || '';
var sheet = e.source.getActiveSheet();
if(sheet.getRange(row,1).getValue() ==""){
if(value !== ""){
sheet.getRange(row, 1).setValue(timestamp);
}
//if(sheet.getRange(row,1).getValue() !==""){
// sheet.getRange(row, 1).setValue(currentvalue);
}
// else{
if(sheet.getRange(column == 2 && column == 3 && column == 4 && column == 5 && column == 6 && column == 7).getValue() ==""){
sheet.getRange(row, 1).setValue('');
}
}
}
In your script, for example, as a simple modification, how about following modification?
From:
if(sheet.getRange(column == 2 && column == 3 && column == 4 && column == 5 && column == 6 && column == 7).getValue() ==""){
sheet.getRange(row, 1).setValue('');
}
To:
if (sheet.getRange(row, 2, 1, 6).getValues()[0].join("") == "") {
sheet.getRange(row, 1).setValue('');
}
In this modification, the row value is retrieved and compare it with "". By this, when the columns "B:G" of the row are empty, sheet.getRange(row, 1).setValue('') is run.
Hey I tried a different approach using the forEach loop to go through each row to first look at if ColB is empty (clear timestamp if empty) then print a timestamp if there is items filled in ColB. I have shared the image of the before and after running code.
function checkinTimeStamp(){
var s = SpreadsheetApp.getActiveSpreadsheet();
var sheet=s.getSheetByName('Sheet1');
var data=sheet.getDataRange().getValues();
var timezone = s.getSpreadsheetTimeZone();
var timestamp = Utilities.formatDate(new Date(), timezone, "hh:mm");
// Go through each row and if ColB is empty but ColA isn't, clear the timestamp in ColA
data.forEach(function(row,col){
if (col == '') return; //skip row 1 as title
if (row[0] == '') return; //skip if ColA is empty
if (row[1] != '') return //skip if ColB is NOT empty
sheet.getRange(col + 1,1).clearContent();
});
// Go through each row and if ColB is not empty put a timestamp in ColA
data.forEach(function(row,col){
if (col == '') return; //skip row 1 as title
if (row[1]== '') return; //skip if ColB is empty
sheet.getRange(col + 1,1).setValue(timestamp);
});
}
Before Running Code
After Running Code
My sample sheet here: https://docs.google.com/spreadsheets/d/1IrEG_YNoUnesg78CDjlEwsVqssElZZ8znciqJC63D1Y/edit#gid=0

Automatic timestamp when a cell is modified, but that clears when the cell is empty

I have this code and i am trying to add some code at the bottom saying that if the column is empty, the date column will clear.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSheet();
var r = ss.getActiveCell();
//1.Change 'Ark 1' to be matching your sheet name
if (r.getColumn() == 4 && ss.getName()=='Ark 1') { // 2. If Edit is done in column 4 (D) And sheet name is Ark 1 then:
var celladdress ='L'+ r.getRowIndex()
ss.getRange(celladdress).setValue(new Date()).setNumberFormat("dd/MM/yyyy hh:mm");}
};
L is the column i have my date timestamp in, while column 4 (D) is the column the modification is done.
You don't explain in your question either you mean the whole column being empty, or the active cell.
In any case:
To verify that the cell is not empty or contains only a space you have to query for if(r.getValue() !="" && r.getValue() !=" ")
Depending on the outcome - either you set a timestmap in the active row of column L or you clear the content of the respective cell in column L with ss.getRange(celladdress).clear();
Below is a sample:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSheet();
var r = ss.getActiveCell();
//1.Change 'Ark 1' to be matching your sheet name
if (r.getColumn() == 4 && ss.getName()=='Ark 1') { // 2. If Edit is done in column 4 (D) And sheet name is Ark 1 then:
var celladdress ='L'+ r.getRowIndex();
if(r.getValue() !="" && r.getValue() !=" "){
ss.getRange(celladdress).setValue(new Date()).setNumberFormat("dd/MM/yyyy hh:mm");
} else{
ss.getRange(celladdress).clear();
}
}
}

Date Stamp for Cell update

Using a Google App Script to show when a Stock Take cell has been entered or updated.
using the following simple script:
function onEdit(ss) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("StockTake"); // StockTake is the sheet where data is entered
var activeCell = sheet.getActiveCell();
var col = activeCell.getColumn(); // numeric value of column, ColA = 1
var row = activeCell.getRow(); // numeric value of row
if (col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7) {
sheet.getRange(row, col+6).setValue(new Date()).setNumberFormat('DDD, MMM dd');
}
}
Problem is that sometimes the Column Header will also Date Stamp. I would like to know how to go about keeping the first row from getting a date stamp.
Thank you!
To avoid stamping the header row, you want to make sure that row > 1. So, add that to the if-statement.
While doing that, also simplify the condition for column: it's 2 <= col <= 7, which is expressed as
if (row > 1 && col >= 2 && col <= 7) {
sheet.getRange(row, col+6).setValue(new Date()).setNumberFormat('DDD, MMM dd');
}