var SHEET_NAME = ["1", "2", "3", "연습시트", "복사용"];
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "1" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 19);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
if( s.getName() == "2" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 19);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
if( s.getName() == "3" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 19);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
if( s.getName() == "연습시트" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 19);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
if( s.getName() == "복사용" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 19);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
}
with this script, to add more sheet, I need to add new sheet name and one bunch of code on the script.
So I wanna change them like, If sheet had Specific text of alphabet in his name, they automatically applied by this script.
There's no need to repeat the same block of code over and over again. Just check if the edited sheet is in the array of 'sheetNames'. See if this helps
function onEdit(e) {
const sheetNames = ["1", "2", "3", "연습시트", "복사용"];
const sheet = e.source.getActiveSheet();
if (sheetNames.includes(sheet.getName()) && e.range.columnStart === 1) {
const offset = e.range.offset(0, 19)
if (!offset.getValue()) {
offset.setValue(new Date()).setNumberFormat("MM/dd HH:mm:ss")
}
}
}
Related
I want to add a Timestamp in my Column X when the Column O changes its Value to "Y"..
At the moment I am able to get the timestamp into Column X when Column O is updated.
But there is already a formula in the column O, which waits for an event and then automatically inserts a "Y", which Google does not seem to recognize as an update of the column. Now I want my script to recognize when a "Y" is in Column O and then set the timestamp in Column X.
I hope that I have expressed myself understandably.
function onEdit(e) {
const s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "März/2023" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 15 ) { //checks that the cell being edited is in column O
var nextCell = r.offset(0, 9);
if( nextCell.getValue() === '' ) //checks if the adjacent cell is empty or not?
nextCell.setValue(new Date());
}
}
}
I have rethought and i update the Column A manually.... Column A can have several values, but the timestamp in X should only be set if the value in Column A is "Bezahlt". I have tried this as in the script below, but that does not work.
function onEdit(e) {
const s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "März/2023" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 1) { //checks that the cell being edited is in column O
var valueO = r.offset(0,0);
var nextCell = r.offset(0, 23);
if( nextCell.getValue() === '' && valueO.getValue === 'Bezahlt' ) //checks if the adjacent cell is empty or not?
nextCell.setValue(new Date());
}
}
}
Sometimes you just need to reread your own script :) the () behind .getValue() where missing..
Got the working code!
function onEdit(e) {
const s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "März/2023" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 1) { //checks that the cell being edited is in column O
var valueO = r.offset(0,0);
var nextCell = r.offset(0, 23);
if( nextCell.getValue() === '' && valueO.getValue() === 'Bezahlt' ) //checks if the adjacent cell is empty or not?
nextCell.setValue(new Date());
}
}
}
I'm looking for help with this code:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Trip Overview" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 12 ) { //checks the column
var nextCell = r.offset(0, 15);
//if( nextCell.getValue() !== ) //is empty?
nextCell.setValue(new Date());
}
}
}
The code is currently adding a date if column 12 contains ANY value, 1,2,3 or "Yes", "adf", "No" etc.
How to modify this code to only add a date if cell 12 contains "YES"?
After checking if you are in the correct sheet and column, you need to check if the value of the cell is "YES"
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Trip Overview" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 12 ) { //checks the column
if(r.getValue() == "YES") { //checksthe value of the cell
var nextCell = r.offset(0, 15);
nextCell.setValue(new Date());
}
}
}
}
I'm trying to add a timestamp to a column when other cells in the same row are completed or changed.
The below script works if just one column is changed, but not multiple columns.
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Allocation Requests" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 7) { //checks the column
var nextCell = r.offset(0, 7);
nextCell.setValue(new Date());
}
}
}
I'm trying to get it so that if columns E,F,G or H are changed, the timestamp in Column M is updated. Thanks
Something like this?
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Allocation Requests" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() >= 5 && r.getColumn() <= 8) { //checks the column
s.getRange(r.getRow(),13).setValue(new Date());
}
}
}
Perhaps this will work
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() == 5) { //checks the column
var nextCell = r.offset(0, 8);
nextCell.setValue(new Date());
}
if( r.getColumn() == 6) { //checks the column
var nextCell = r.offset(0, 7);
nextCell.setValue(new Date());
}
if( r.getColumn() == 7) { //checks the column
var nextCell = r.offset(0, 6);
nextCell.setValue(new Date());
}
if( r.getColumn() == 8) { //checks the column
var nextCell = r.offset(0, 5);
nextCell.setValue(new Date());
}
}
}
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")
}
I'm very new to scripts so I'm just trying to get this script working for my sheet.
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() == 5 ) { //checks the column
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
At the moment it seems it will enter a timestamp in column F if data is inserted into E.
I'd like the time stamp to go into A; Instead of nextCell, what would correspond to column A?
Thanks and sorry for the newbie question!
Updated
Thanks! That worked great and helped with my understanding too : )
If I want this to apply to multiple sheets but not all.
I tried adding additional lines but it doesn't seem to have worked.. where have I gone wrong?
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sanshiro" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
if( s.getName() == "Josh" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
if( s.getName() == "Suil" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
Your code uses the offset method to get the cell to be updated:
offset(rowOffset, columnOffset)
Returns a new range that is offset from this range by the given number
of rows and columns (which can be negative). The new range will be the
same size as the original range.
So, if you want to write 4 columns to the left (column A) instead of one to the right (column F), just replace this line:
var nextCell = r.offset(0, 1);
with:
var nextCell = r.offset(0, -4);
function onEdit(e) {
var sheetName = 'Sheet1'; //name of the sheet the script should work on
var colToWatch = 5 // watches for edits made in col E
if (e.range.columnStart !== colToWatch || e.source.getActiveSheet()
.getName() !== sheetName) return;
e.range.offset(0, -4) //offset the currently edited cell by 0 rows, -4 columns
.setValue(e.value ? new Date() : null);
}
See if this works for you ?