Google Spreadsheet添加相同的单元格值

时间:2017-03-06 09:00:02

标签: google-sheets spreadsheet google-docs

我想添加一个单元格值,其前一个值(同一个单元格)但它表示存在循环依赖关系。怎么解决? (I.E。:B2单元格值= 4,现在我输入相同的单元格并输入5,然后添加5到4(之前的值),然后显示它= 9)。

1 个答案:

答案 0 :(得分:1)

您需要手动"安装"触发器并使用以下代码:

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"触发,点击"资源"在代码编辑器中输入菜单,然后选择"当前项目的触发器"。如果没有添加触发器,请添加触发器。在下拉列表中找到该函数的名称等。