Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

javascript - Google sheets automatic Notes [with onEdit() function] on cells' values resulting from formulas

Is it possible to create automatic Notes based on cells variable values resulting from formulas ?

For example A1=stxt(B1;1;4) gives "some" with B1=sometext (variable value), so that A1's Note would be "some" And a second question : how to add at least a third condition for exceptions treatment in

mySelection.getValues().flat().map(v=>[v=="/" || ""?null:v]); // or #N/A or #VALUE!

I didn't neither succeed having many OR conditions in one single code line so that I have to put many if statements like here :

     var excludedCols = [2, 4, 6, 8, 10, 12, 14];
     // because if(cellule.substring(0,1) = "C"||"E"||"G"||"I"||"K"||"M"){ is not working
     if (col > 2 && col < 23){
       if (e.range.getRow() > 1){
         if (excludedCols.indexOf(col) == -1){ }
       }
     }

PS : These questions come from my previous one answered here : Google sheets - Optimisation of function to create notes in a range (very slow)

Here's an illustrating sheet : https://docs.google.com/spreadsheets/d/1xj5PMIQxvTNhq1gNiFf5SOwzxTH5LtY-NkHydU8Ptuk/edit?usp=sharing

question from:https://stackoverflow.com/questions/65641294/google-sheets-automatic-notes-with-onedit-function-on-cells-values-resultin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...