【问题标题】:Google Spreadsheet add same cell value谷歌电子表格添加相同的单元格值
【发布时间】:2017-03-06 09:00:01
【问题描述】:

我想添加一个单元格值及其先前的值(同一个单元格),但它表示存在循环依赖关系。如何解决? (即:B2 单元格值 = 4,现在我在同一个单元格中输入并输入 5,然后将 5 添加到 4(之前的值)然后显示它 = 9)。

【问题讨论】:

  • 单元格的公式不能引用自身,所以B2不能在单元格B2的公式中
  • 那么我该如何进行这种操作呢?
  • 您需要在不同的单元格中进行计算或编写 GoogleScript 来为您自动执行该过程
  • 好的,在不同的单元格中。但是怎么做?请写一个真实的例子。
  • 您只需将这两个值存储在单独的单元格中,然后将它们汇总在第三个单元格中,例如B2 = 4C2 = 5D2 = SUM(B2:C2)

标签: google-sheets spreadsheet google-docs


【解决方案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);

}

要安装“编辑时”触发器,请单击代码编辑器中的“资源”菜单,然后选择“当前项目的触发器”。如果尚未添加触发器,请添加触发器。在下拉列表等中查找函数的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多