简体   繁体   English

计算列中的更改次数 - Google Script / Sheets

[英]Count the number of changes in a column - Google Script / Sheets

First of all, I ask you to excuse me if I make some language-mistake (I'm Italian!).首先,如果我犯了一些语言错误,请原谅我(我是意大利人!)。 I'm trying to write a script for a Google Sheet that can help me to track the number of changes of a column.我正在尝试为 Google Sheet 编写一个脚本,它可以帮助我跟踪列的更改次数。 I would like a counter that grows everytime a value of a cell changes.我想要一个每次单元格值更改时都会增长的计数器。 Ex:前任:

-Column A: the cell A3 changes from "2020" to "2021" -A列:单元格A3从“2020”变为“2021”
-Column B: the cell B3 changes from 0 to 1 (o from 2 to 3, a simple +1 on the value). -B 列:单元格 B3 从 0 变为 1(o 从 2 变为 3,对值进行简单的 +1)。

I wrote this code but I cannot understand where is the error.我写了这段代码,但我不明白错误在哪里。

  function onEdit(e) {
  incrementCounter_(e);
}

function incrementCounter_(e) {
  
  var stw = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Foglio2");
  var c1 = stw.getRange(2, 1);
  var c2 = stw.getRange(2, 2);


  if (!e || !e.range) {
    return;
  }
  
  var sheet = e.range.getSheet();
  for (var i=2; i<=6; i++){
  if (sheet.getName() === stw && e.range.getA1Notation() === stw.getrange().getvalue(i,1)) {
   var cell = stw.getrange().getvalue(i,2);
   cell.setValue((Number(cell.getValue()) || 0) + 1);
  }
}
}

Thanks for the help!谢谢您的帮助!

Do you mean this?你是这个意思吗?

function onEdit(e) {
  if (e.range.getA1Notation() === 'A3') {
    var range = SpreadsheetApp.getActiveSheet().getRange('B3');
    var value = range.getValue();
    value++;
    range.setValue(value);
  }
}

There is no need to use two functions for this.没有必要为此使用两个函数。

Code代码

// Copyright 2020 Google LLC.
// SPDX-License-Identifier: Apache-2.0

function onEdit(e) {
  var sheet = e.range.getSheet();
  var sheetName = sheet.getName(); 
  if (sheetName == "Foglio2" && e.range.getColumn() == 1) {
    var row = e.range.getRow();
    var val = sheet.getRange(row, 2).getValue();
   sheet.getRange(row, 2).setValue(val + 1);
  }
}

Explanation解释

What you want can be achieved by using the onEdit(e) trigger.您可以通过使用onEdit(e)触发器来实现您想要的。 The above function makes use of the e event object and checks where exactly the edit is being made.上面的函数使用e事件对象并检查编辑的确切位置。 As it can be seen, the for loop is not needed in this situation as in order to get the column and row needed the getColumn() and getRow() methods have been used.可以看出,在这种情况下不需要for循环,因为为了获取所需的列和行,已经使用了getColumn()getRow()方法。

In this situation, the script checks if the sheet in which the edit has been made is Foglio2 and if an edit has been made on the A column.在这种情况下,脚本会检查进行编辑的工作表是否为Foglio2以及是否对A列进行了编辑。 If the condition checks, it increments the corresponding value from the B column.如果条件检查,它会增加B列中的相应值。

Note笔记

Please note that getValue(value1, value2) is not a valid method and if you want to get the value for that specific range, you must pass the two parameters to the getRange() method.请注意getValue(value1, value2)不是有效方法,如果您想获取该特定范围的值,您必须将两个参数传递给getRange()方法。

Reference参考

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

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