M
M
Marina01234567892020-10-05 19:51:47
Google Apps Script
Marina0123456789, 2020-10-05 19:51:47

Why doesn't the second similar script work on a Google spreadsheet sheet, but with different conditions?

I inserted a script to fill in the date according to the condition of changing the data in the adjacent column.
You need to do exactly the same, also insert the date into another cell, and when the data changes, already in another column.
Only one of the two scripts works, I delete the second one - the first one works. Why don't they work at the same time?

Script 1:

function onEdit(e) {
if (e.range.columnStart == 12 && !!~["Выполнено", "выполнено"].indexOf(e.value))
    e.range.offset(0, 1).setValue(new Date())
}


Script 2:
function onEdit(e)
{
var range = e.range
if(range.getColumn()==3 && e.value!=null){
range.offset(0,-2).setValue(new Date())
}
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2020-10-06
@dimonchik2013

I delete the second one - the first one works.

name the variables differently

M
Marina0123456789, 2020-10-05
@Marina0123456789

The initial scripts were:
Script 1:
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var idCol = e.range.getColumn();
var idRow = e.range.getRow();
if ( idCol == 3 && sheet.getName() =='Sheet1' ) {
var Value = e.range.offset(0, 0).getValues();
if ( Value != null )
var vartoday = getDate();
sheet.getRange(idRow, 1).setValue(vartoday);
}
}
// Returns YYYYMMDD-formatted date.
function getDate() {
var today = new Date();
today.setDate(today.getDate());
return Utilities.formatDate(today, 'PST', 'dd.MM.yyyy');
//return Utilities.formatDate(today, 'GMT+03:00', 'dd.
}
Script 2
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var idCol = e.range.getColumn();
var idRow = e.range.getRow();
if ( idCol == 12 && sheet.getName() =='Sheet1' ) {
var Value = e.range.offset(0, 0).getValues();
if ( Value == "Done")
var vartoday = getDate();
sheet.getRange(idRow, 13).setValue(vartoday);
}
}
// Returns YYYYMMDD-formatted date.
function getDate() {
var today = new Date();
today.setDate(today.getDate());
return Utilities.formatDate(today, 'PST', 'dd.MM.yyyy');
//return Utilities.formatDate(today, 'GMT+03:00', 'dd.MM.yyyy');

M
mityayka1, 2020-10-09
@mityayka1

Because the two functions are named the same. onEdit is a special Google function that is fired when a table is modified by a human. You need to write all the necessary actions in the body of a single onEdit function.
Roughly speaking, combine the code of them. But when concatenating, pay attention to the names of the variables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question