onEdit() function which is only effective when cells values are
manually edited but not by relative values changed by a function
inside that cells
- You are trying to trigger an
onEdit
function via a formula but that's not how triggers work. The official documentation states the following:
The onEdit(e)
trigger runs automatically when a user changes the
value of any cell in a spreadsheet.
Namely, onEdit
triggers are activated only by user actions, not by formulas nor scripts.
The workaround would be to modify the current onEdit
code a little and include a code which will allow you to edit the formulas part when you change the value of the cells that the formula depends on. For example, you will set a note in a cell in column C
of the Feuille1
sheet when you edit a cell in the same row in column A
of Header 3
:
else if(NomFeuilleActive=="Header 3"){
if(col==1 && row>1){
e.source.getSheetByName("Feuille1").getRange(row,3).setNote(e.range.getValue());
}
}
Solution:
function onEdit(e){
var classeur = SpreadsheetApp.getActiveSheet();
var NomFeuilleActive = classeur.getName();
var mySelection = SpreadsheetApp.getActiveRange();
var excludedCols = [2, 4, 6, 8, 10];
var cellule = mySelection.getA1Notation();
var col = e.range.getColumn();
var row = e.range.getRow(); //new code
if (NomFeuilleActive == "Feuille1"){ // new code
if (col > 2 && col < 11){
if (e.range.getRow() > 1){ // if is not 1st line headers (why doesn't it work with substring() == "1" ?!
if (excludedCols.indexOf(col) == -1){
var note = mySelection.getDisplayValue();
mySelection.setNote(note); //SpreadsheetApp.getUi().alert(e.oldValue);
}
}
}
}
//new code
else if(NomFeuilleActive=="Header 3"){
if(col==1 && row>1){
e.source.getSheetByName("Feuille1").getRange(row,3).setNote(e.range.getValue());
}
}
//
var Plage = SpreadsheetApp.getActiveSheet().getRange("C2:I");
var valeurs = Plage.getValues().flat().map(v=>[v=="#VALUE!" || ""?null:v]); // not working with "#VALUE!" or "#VALEUR!"
var notes = Plage.getNotes().map(v=>[v=="* %" || ""?null:v]);
var Tab = [[],[],[]];
var ToCorrect = [];
for (i=0; i<notes.length; i++){
// Tab[1].push([valeurs[i]]);
// Tab[2].push([notes[i]]);
if (e.range.getNumberFormat() != "0.###############"){
if (valeurs[i] != notes[i]){
ToCorrect.push(valeurs[i]); // SpreadsheetApp.getUi().alert(valeurs[i]);
// SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange("A1") } SpreadsheetApp.getRange(
}
}
}
}
Also in your code you had if (NomFeuilleActive = "Feuille1")
with one =
(assignment operation) but this evaluates always to true
and your code would be executed for any sheet name. I adjusted it to ==
which is the equality operator and the proper way to compare two variables.
how to add at least a third condition for exceptions treatment?
If you want to exclude many values and have multiple exceptions, then do that:
mySelection.getValues().flat().map(v=>[["/","","#N/A","#VALUE!","#VALEUR!"].includes(v)?null:v]);
where you can list in the inner array all the values you want to exclude from setting a note.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…