简体   繁体   English

谷歌表格自动注释 [使用 onEdit() 函数] 由公式产生的单元格值

[英]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例如 A1=stxt(B1;1;4) 给出 B1=sometext (变量值)的“some”,因此 A1 的 Note 将是“some” 第二个问题:如何为异常处理添加至少第三个条件在

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:我没有成功地在一个代码行中包含许多 OR 条件,因此我必须在此处放置许多 if 语句:

     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) PS:这些问题来自我之前在这里回答的问题: Google sheet - Optimization of function to create notes in a range(非常慢)

Here's an illustrating sheet: https://docs.google.com/spreadsheets/d/1xj5PMIQxvTNhq1gNiFf5SOwzxTH5LtY-NkHydU8Ptuk/edit?usp=sharing这是一个说明表: https://docs.google.com/spreadsheets/d/1xj5PMIQxvTNhq1gNiFf5SOwzxTH5LtY-NkHydU8Ptuk/edit?usp=sharing

onEdit() function which is only effective when cells values are manually edited but not by relative values changed by a function inside that cells onEdit() function 仅在手动编辑单元格值时有效,在该单元格内由 function 更改的相对值无效

  • You are trying to trigger an onEdit function via a formula but that's not how triggers work.您正在尝试通过公式触发onEdit function ,但这不是触发器的工作方式。 The official documentation states the following: 官方文档声明如下:

The onEdit(e) trigger runs automatically when a user changes the value of any cell in a spreadsheet.用户更改电子表格中任何单元格的值时, onEdit(e)触发器会自动运行。

Namely, onEdit triggers are activated only by user actions , not by formulas nor scripts.也就是说, onEdit触发器仅由用户操作激活,而不是由公式或脚本激活。

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.解决方法是稍微修改当前的onEdit代码并包含一个代码,当您更改公式所依赖的单元格的值时,该代码将允许您编辑公式部分。 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 :例如,当您在Header 3 column A的同一行中编辑单元格时,您将在Feuille1 column C的单元格中设置注释:

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.同样在您的代码中,您有if (NomFeuilleActive = "Feuille1")和 one = (assignment operation) 但这始终评估为true ,并且您的代码将针对任何工作表名称执行。 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.您可以在内部数组中列出要从设置注释中排除的所有值。

I have figured it out by putting into onEdit() the call to the insertNotes() function我已经通过在 onEdit() 中调用 insertNotes() function

function onEdit(e){
  var Header3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Header3");
  var Feuille1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Feuille1");
  var valTemp = Feuille1.getRange("N4").getValue();
  Header3.getRange("A2").setValue(valTemp);
 insertnotes(e);
}


 
function insertnotes(e){
   var mySelection = e.range; 
   var excludedCols = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22];
   
   var col = e.range.getColumn(); //SpreadsheetApp.getUi().alert(col);
  
  var DestRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Feuille1").getRange("C2:C5");
                                                       
   var notes = DestRange.getValues();
   DestRange.setNotes(notes); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM