我想添加一个单元格值和它之前的值(同一个单元格的),但是它说有一个循环依赖关系。如何解决?(例如: B2单元格值= 4,现在我输入相同的单元格并键入5,然后将5加到4(以前的值),然后显示它= 9)。
发布于 2017-03-07 00:36:21
您将需要手动“安装”触发器,并使用以下代码:
function editSameCell(e) {
  var editedCell,calculatedValue,enteredValue,oldCellValue;
  //Currently the last part of this code runs for every cell edited
  //You should check the column and/or the row to only run the remaining
  //lines of code if the cell being edited is the cell that you want
  //changed
  oldCellValue = e.oldValue;
  Logger.log('oldCellValue: ' + oldCellValue);
  Logger.log('isNaN(oldCellValue): ' + isNaN(oldCellValue));
  if (isNaN(oldCellValue) || !oldCellValue) {
    return; //Quit here because if the user is entering text there should not
    //be a calculation done
  }
  editedCell = SpreadsheetApp.getActiveRange().getCell(1,1);
  enteredValue = editedCell.getValue();
  Logger.log('enteredValue: ' + enteredValue);
  Logger.log('typeof enteredValue: ' + typeof enteredValue);
  if (!enteredValue) {//If for example the cell value was deleted do not enter a new value
    return;  
  };
  calculatedValue = Number(oldCellValue) + Number(enteredValue);
  editedCell.setValue(calculatedValue);
}要安装"On Edit“触发器,请单击代码编辑器中的"Resources”菜单,然后选择"Current project's triggers“。如果尚未添加触发器,则添加触发器。在下拉列表中找到函数的名称等。
https://stackoverflow.com/questions/42621360
复制相似问题