英文:
Google Sheet Appscript Changelog to Track Deleted Row
问题
我在我的电子表格中有一个变更日志,用于跟踪添加、编辑或删除的信息。问题是,变更日志无法检测到用户"[删除行]"。变更日志是否能够跟踪删除的行,或者我只能听之任之?谢谢 🙏。
function onEdit(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameTracker = ss.getActiveSheet().getName();
if (sheetNameTracker !== "Changelog") {
let sheet = ss.getSheetByName('Changelog');
var range = e.range.getA1Notation();
var user = e.user.getUsername();
var function_source = "onEdit";
var time = new Date();
var changeType = 'EDIT';
console.log(user);
var oldValue = e.oldValue;
var newValue = e.value;
var lastRow = sheet.getLastRow();
sheet.getRange(lastRow+1,1,1,8).setValues([[time.toLocaleString(),function_source,changeType,sheetNameTracker,range,user,oldValue,newValue]]);
}
}
英文:
I have a changelog in my spreadsheet to keep track of information that is added, edited, or removed. The issue is that the changelog cannot detect if a user "Delete row". Is it even possible to have the changelog track deleted row or am I out of luck? Thanks 🙏.
function onEdit(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameTracker = ss.getActiveSheet().getName();
if (sheetNameTracker !== "Changelog") {
let sheet = ss.getSheetByName('Changelog');
var range = e.range.getA1Notation();
var user = e.user.getUsername();
var function_source = "onEdit";
var time = new Date();
var changeType = 'EDIT';
console.log(user);
var oldValue = e.oldValue;
var newValue = e.value;
var lastRow = sheet.getLastRow();
sheet.getRange(lastRow+1,1,1,8).setValues([[time.toLocaleString(),function_source,changeType,sheetNameTracker,range,user,oldValue,newValue]]);
}
}
答案1
得分: 1
关于“我想知道是否有办法让更改日志跟踪已删除的行。还是不可能的?”在当前阶段,OnEdit触发器的事件对象不包括“changeType”属性。因此,在这种情况下,需要使用可安装的OnChangge触发器。当这在您的脚本中反映出来时,可以考虑以下修改。
修改后的脚本:
在复制并粘贴以下修改后的脚本后,请将OnChange触发器安装到函数onChange
中。这样,当删除一行时,e.changeType
的值将返回为REMOVE_ROW
。
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameTracker = ss.getActiveSheet().getName();
if (sheetNameTracker !== "Changelog") {
let sheet = ss.getSheetByName('Changelog');
var range = e.range && e.range.getA1Notation(); // Modified
var user = e.user.getUsername();
var function_source = "onEdit";
var time = new Date();
var changeType = e.changeType || 'EDIT'; // Modified
console.log(user);
var oldValue = e.oldValue;
var newValue = e.value;
var lastRow = sheet.getLastRow();
sheet.getRange(lastRow + 1, 1, 1, 8).setValues([
[time.toLocaleString(), function_source, changeType, sheetNameTracker, range, user, oldValue, newValue]
]);
}
}
// 请将OnChange触发器安装到此函数。
function onChange(e) {
if (e.changeType == "EDIT") return;
onEdit(e);
}
- 在这个修改后的脚本中,当删除一行时,可安装的OnChange触发器会自动运行
onChange
函数。然后,事件对象会发送到onEdit
,并由您的脚本记录日志。
注意:
-
在OnEdit触发器的情况下,即使删除了一行,该触发器也不会被触发,而只有在编辑单元格时触发。另一方面,在可安装的OnChange触发器的情况下,该触发器对编辑单元格和删除行都会触发。在这种修改中,利用了这种情况。
-
在当前阶段,可安装的OnChange触发器的事件对象不包括
range
对象。因此,虽然可以发现已删除一行,但无法知道已删除的行。如果您想知道已删除的行,可以使用一种变通方法,即在编辑之前和之后比较旧表格和新表格。我担心在这种情况下,脚本可能会变得复杂。因此,在这个答案中,我提出了一个简单的修改。
英文:
About I am asking if there is a way I can have changelog track deleted row. Or is it not possible?
, in the current stage, the event object of the OnEdit trigger doesn't include the property of changeType
. So, in this case, it is required to use the installable OnChangge trigger. When this is reflected in your script, how about the following modification?
Modified script:
After you copy and paste the following modified script, please install OnChange trigger to the function onChange
. By this, when a row is removed, the value of REMOVE_ROW
is returned as e.changeType
.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameTracker = ss.getActiveSheet().getName();
if (sheetNameTracker !== "Changelog") {
let sheet = ss.getSheetByName('Changelog');
var range = e.range && e.range.getA1Notation(); // Modified
var user = e.user.getUsername();
var function_source = "onEdit";
var time = new Date();
var changeType = e.changeType || 'EDIT'; // Modified
console.log(user);
var oldValue = e.oldValue;
var newValue = e.value;
var lastRow = sheet.getLastRow();
sheet.getRange(lastRow + 1, 1, 1, 8).setValues([
[time.toLocaleString(), function_source, changeType, sheetNameTracker, range, user, oldValue, newValue]
]);
}
}
// Please install OnChange trigger to this function.
function onChange(e) {
if (e.changeType == "EDIT") return;
onEdit(e);
}
- In this modified script, when a row is removed,
onChange
is automatically run by the installable onChange trigger. And, the event object is sent toonEdit
, and the log is recorded by your script.
Note:
-
In the case of the OnEdit trigger, even when a row is deleted, this trigger is not fired while the trigger is fired when a cell is edited. On the other hand, in the case of the installable OnChange trigger, the trigger is fired for both the editing cell and removing a row. In this modification, this situation is used.
-
In the case of OnChange trigger, in the current stage, the event object of OnChange trigger doesn't include
range
object. By this, the removed row is not known while it can be found that a row is removed. If you want to know the removed row, as a workaround, it is required to compare the old sheet and the new sheet before and after the edit. I'm worried that in this case, the script might be complicated. So, in this answer, I proposed a simple modification.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论