I'm very new to scripts and trying to get the best out of and onEdit() function to track changes made on a collaborative spreadsheet.
The script seems to run well given that a line is added to the Changelog sheet every time a change is made by any user at all on the sheet. However, it only sometimes registers the data i'm seeking, and most of the times registers a line which is a copy of the heading: see here
Do I have something wrong with authorizations here? Or is it something else?
Thank you so, so much!
--
Here is the detail of the script:
function onEdit(e) {
var changelogSheetName = "Changelog";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = SpreadsheetApp.getActiveRange();
var timestamp = new Date();
var currentSheet = ss.getActiveSheet();
var currentSheetName = currentSheet.getName();
var previousValue = e.oldValue;
var newValue = cell.getValue();
var typeChange = "Edit";
if (currentSheetName == changelogSheetName) return;
var changelogSheet = ss.getSheetByName(changelogSheetName);
if (changelogSheet == null) {changelogSheet = ss.insertSheet(changelogSheetName, ss.getNumSheets());}
changelogSheet.getRange('A1:G1').setBackground('#E0E0E0');
changelogSheet.appendRow(["Timestamp", "Sheet", "Cell", "Type", "Old Value", "New Value", "User"]);
changelogSheet.deleteColumns(8,19);
changelogSheet.setFrozenRows(1);
changelogSheet.setColumnWidth(1, 170);
changelogSheet.setColumnWidth(7, 170);
changelogSheet.protect();
var user = Session.getEffectiveUser().getEmail();
if (previousValue == null){typeChange = "Add";} else if (newValue == "") {typeChange = "Remove";}
changelogSheet.appendRow([timestamp, currentSheetName, cell.getA1Notation(), typeChange, previousValue, newValue, user]);
}
However, it only sometimes registers the data i'm seeking, and most of the times registers a line which is a copy of the heading
andDo I have something wrong with authorizations here? Or is it something else?
, unfortunately, I cannot understand about your goal. I apologize for this. Can I ask you about the detail of your goal? - Tanaike