GOOGLE SHEET APP SCRIPT DOES NOT WORK

65 views
Skip to first unread message

VCAD Dental

unread,
May 30, 2025, 7:49:44 AMMay 30
to Google Apps Script Community
Hi there,
I add my code to run pick a time date automatically. And it only runs in sheet "QT5". It does not work in others. May I have any mistake? Thanks all.



function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "VNT5" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 4 ) { //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());
}
}
}

function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "QT5" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 3 ) { //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());
}
}
}

function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "QT6" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 3 ) { //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());
}
}
}

Andrew Roberts

unread,
May 30, 2025, 8:08:47 AMMay 30
to google-apps-sc...@googlegroups.com
You can only have one onEdit(). It'll just use the last one it sees.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/adfcb3e9-033c-41ab-b00b-b9d379dd0464n%40googlegroups.com.

VCAD Dental

unread,
May 30, 2025, 10:51:36 AMMay 30
to Google Apps Script Community
Thank you, it works to me.

SAHIN TIMUCIN Ozturk

unread,
May 30, 2025, 12:54:39 PMMay 30
to google-apps-sc...@googlegroups.com
I can help you with code (let me double check)


Keith Andersen

unread,
May 30, 2025, 2:21:04 PMMay 30
to google-apps-sc...@googlegroups.com

Have one function named onEdit() then if statements within it to cover the scenarios.

You cannot have multiple functions with the same name.



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

Emerson Maia Paula

unread,
May 30, 2025, 5:07:40 PMMay 30
to google-apps-sc...@googlegroups.com
Good afternoon, can you try this.

function onEdit(e) {
  const range = e.range;
  const sheet = range.getSheet();
  const sheetName = sheet.getName();
  const editedColumn = range.getColumn();

  const config = {
    "VNT5": { column: 4 },
    "QT5": { column: 3 },
    "QT6": { column: 3 }
  };

  if (config[sheetName]) {
    const { column: targetColumn } = config[sheetName];

    if (editedColumn === targetColumn) {
      const nextCell = range.offset(0, 1);
      if (nextCell.getValue() === '') {
        nextCell.setValue(new Date());
      }
    }
  }
}

Keith Andersen

unread,
May 30, 2025, 8:01:20 PMMay 30
to google-apps-sc...@googlegroups.com
Here's a template I always use (copy&paste):
Has everything you need

/**
* --- ✏️ ON EDIT ✏️ ---
* --WARNING!! ONEDIT FUNCTIONS HAVE A QUOTA ON NUMBER OF TRIGGERS AND QUANTITY OF TRIGGER EXECUTIONS PER DAY
* --AND HAS SOME FUNCTIONS IT CANNOT PERFORM
* --ONEDIT FUNCTIONS ARE NOT ALWAYS RELIABLE
*/
function onEdit(e){
// e is the event object that contains information about the edit
let workbook = e.source;//-- gets Spreadsheet event object (workbook file)
let cell = e.range;// edited range object
let cellData = e.value;//value inside cell
let cellOldData = e.oldValue;//previous cell value
let user = e.user;// gets email of Gmail user

let editedSheetSource = e.range.getSheet();//-- gets edited sheet object
let editedSheetName = editedSheetSource.getSheetName();//--- gets edited sheet name
let sheetGID = editedSheetSource.getSheetId();//---gets edited sheet GID
let col = e.range.getColumn();//gets column number of edited cell
let row = e.range.getRow();//gets row number of edited cell

//-- ⬇️ onEdIt parameters go here ⬇️ --
//EXAMPLE OF IF STATEMENT TO TARGET CERTAIN PARAMETERS TO CALL CERTAIN FUNCTIONS
if( editedSheetName === "what-ever-sheet" && col === 1 && row === 4 ){
do_this_function();
}

} //+++++++++++++++++++ END OF FUNCTION ++++++++++++++++++++++++++++++++++++++++





--

Passions: God, Family, Friends, Scripture, Data Management, Google Sheets + App Script, MS Access, Programing, sharing and much more.

VCAD Dental

unread,
May 31, 2025, 2:13:31 AMMay 31
to Google Apps Script Community
Thank you mate ! 

VCAD Dental

unread,
May 31, 2025, 2:14:32 AMMay 31
to Google Apps Script Community
Thanks my mate! OMG

Keith Andersen

unread,
May 31, 2025, 3:17:27 AMMay 31
to google-apps-sc...@googlegroups.com

👍👍



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

Reply all
Reply to author
Forward
0 new messages