首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google电子表格添加相同的单元格值

Google电子表格添加相同的单元格值
EN

Stack Overflow用户
提问于 2017-03-06 17:00:02
回答 1查看 1K关注 0票数 1

我想添加一个单元格值和它之前的值(同一个单元格的),但是它说有一个循环依赖关系。如何解决?(例如: B2单元格值= 4,现在我输入相同的单元格并键入5,然后将5加到4(以前的值),然后显示它= 9)。

EN

回答 1

Stack Overflow用户

发布于 2017-03-07 00:36:21

您将需要手动“安装”触发器,并使用以下代码:

代码语言:javascript
运行
复制
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“。如果尚未添加触发器,则添加触发器。在下拉列表中找到函数的名称等。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42621360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档