Date Stamp in multiple Columns Google Sheets - google-apps-script

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

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:

add timestamp when specific cell = "complete"

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") {.....

What is the right way to use OR and AND in appscript?

Is this the right way to write OR condition to combine the 2 if statements into 1?
if (activeSheet.getName() == ("2 Grade" || "3 Grade") && Cell.getRow() >= 53 && Cell.getColumn() >= 9 && Cell.getRow() <= 57 && Cell.getColumn() <= 13) {
supplierSeal2G.copyTo(supplierSeal2GData, SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
surveyorSeal2G.copyTo(surveyorSeal2GData, SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
remarks2G.copyTo(remarks2GData, SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
}
How about this modification? Please think of this as just one of several answers.
Pattern 1:
In this pattern, sheetName is declared and used for the if statement.
From:
if (activeSheet.getName() == ("2 Grade" || "3 Grade") && Cell.getRow() >= 53 && Cell.getColumn() >= 9 && Cell.getRow() <= 57 && Cell.getColumn() <= 13) {
To:
var sheetName = activeSheet.getName();
if ((sheetName == "2 Grade" || sheetName == "3 Grade") && Cell.getRow() >= 53 && Cell.getColumn() >= 9 && Cell.getRow() <= 57 && Cell.getColumn() <= 13) {
Pattern 2:
In this pattern, ["2 Grade", "3 Grade"] is prepared as an array and used for the if statement with indexOf().
From:
if (activeSheet.getName() == ("2 Grade" || "3 Grade") && Cell.getRow() >= 53 && Cell.getColumn() >= 9 && Cell.getRow() <= 57 && Cell.getColumn() <= 13) {
To:
var sheetNames = ["2 Grade", "3 Grade"];
if (sheetNames.indexOf(activeSheet.getName()) > -1 && Cell.getRow() >= 53 && Cell.getColumn() >= 9 && Cell.getRow() <= 57 && Cell.getColumn() <= 13) {
References:
if...else
indexOf()
If this was not the result you want, I apologize.

Google Sheets Automatic Timestamp Script for Multiple Tabs

I am making a large google spreadsheet with multiple sheet tabs. When I edit Column 6, I need a timestamp to automatically be entered into Column 1. I need this to happen independently in every sheet. Here is the script that I have so far, and it works well. However, I am going to add at least 10 more tabs, and I was wondering if there was an easier way to do this with less room for error.
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "A" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 6 ) { //checks the column
var nextCell = r.offset(0, -5);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
if( s.getName() == "B" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 6 ) { //checks the column
var nextCell = r.offset(0, -5);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
if( s.getName() == "C" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 6 ) { //checks the column
var nextCell = r.offset(0, -5);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
}
Yes, there is. Just put all the sheet names in an array and check if the currently edited sheet is in that array (using indexOf). If it is not in the array it will return -1. In that case the script exits.
See if something like this works:
function onEdit(e) {
var s = e.source.getActiveSheet(),
sheets = ["A", "B", "C"],
stampCell = e.range.offset(0, -5);
if (sheets.indexOf(s.getName()) == -1 || e.range.columnStart !== 6 || !e.value || stampCell.getValue()) return;
stampCell.setValue(new Date()).setNumberFormat("MM/dd HH:mm:ss")
}

(f) script works (e) doesnt

i have these two scripts that do the same thing just on different pages, but the f script works and the e script doesnt, any help??
function onEdit(e) {
if (e.source.getActiveSheet().getName() !== "RS3 Points" ||
e.range.columnStart < 9 || e.range.columnStart > 35 ||
e.range.rowStart < 4 || e.range
.columnStart < e.range.columnEnd) return;
e.range.offset(0, 37 - e.range.columnStart).setValue(new Date());
}
function onEdit(f) {
if (f.source.getActiveSheet().getName() !== "OS Points" ||
f.range.columnStart < 6 || f.range.columnStart > 37 ||
f.range.rowStart < 4 || f.range
.columnStart < f.range.columnEnd) return;
f.range.offset(0, 39 - f.range.columnStart).setValue(new Date());
}
Munkey's intuition is right. You can't have more than one onEdit() in the same project. Moreover 'e' is not a random letter (that can be replaced by any other letter) but stands for 'event object'. You can read more about that here. The solution would indeed consist in 'merging' the two scripts into one. Give this a try:
function onEdit(e) {
var ind = ["RS3 Points", "OS Points"].indexOf(e.source.getActiveSheet()
.getName()),
startCol = [9, 6],
endCol = [35, 37],
offset = [37, 39];
if (ind === -1 || e.range.columnStart < startCol[ind] || e.range.columnStart > endCol[ind] ||
e.range.rowStart < 4 || e.range.columnStart < e.range.columnEnd) return;
e.range.offset(0, offset[ind] - e.range.columnStart)
.setValue(new Date());
}
I don't think you can have 2 onEdits in google scripts.
If you comment out the f function, does the e one work?
cant you put them in one onedit function as a solution.
function onEdit(e) {
if (e.source.getActiveSheet().getName() !== "RS3 Points" ||
e.range.columnStart < 9 || e.range.columnStart > 35 ||
e.range.rowStart < 4 || e.range
.columnStart < e.range.columnEnd) return;
e.range.offset(0, 37 - e.range.columnStart).setValue(new Date());
if (e.source.getActiveSheet().getName() !== "OS Points" ||
e.range.columnStart < 6 || e.range.columnStart > 37 ||
e.range.rowStart < 4 || e.range
.columnStart < e.range.columnEnd) return;
e.range.offset(0, 39 - e.range.columnStart).setValue(new Date());
}
Something like that, but its untested.
I think If i'm reading your code correctly, you're looking for Not equals to sheet name, it might be better to change it to equals instead.