Capturing Google Spreadsheet Edits (Change Log/ Audit Trail)

Shlok Mittal
3 min readJul 9, 2023

--

Introduction

Google Spreadsheets is a popular tool for collaborative work, allowing multiple users to edit and update data simultaneously. However, keeping track of changes made to a spreadsheet can be challenging, especially when multiple people are involved. In this post, we’ll explore a solution using Google Apps Script to capture and log edits made to a spreadsheet. This solution can be valuable in scenarios where it’s necessary to maintain an audit trail or monitor data modifications.

Use Case: Audit Trail and Data Monitoring

Imagine a scenario where a team is working on a shared spreadsheet containing sensitive information, such as financial records or customer data. It’s essential to track any changes made to the spreadsheet to maintain accountability, ensure data integrity, and identify potential errors or unauthorized modifications.

By implementing a solution that captures the timestamp, user details, cell, old value, new value, and sheet name for each edit, you can easily maintain an audit trail and monitor data modifications.

Step-by-Step Guidance

To implement the solution, follow these step-by-step instructions:

Step 1: Open the Google Spreadsheet

First, open the Google Spreadsheet where you want to capture the edits. This solution can be applied to any existing spreadsheet or a new one.

Step 2: Access the Apps Script Editor

In the menu bar, click on “Extensions” and select “Apps Script.” This will open the Apps Script editor in a new tab.

Step 3: Replace Existing Code

In the Apps Script editor, remove any existing code and replace it with the following code, and hit save:

function onEdit(e) {
var timestamp = new Date();
var user = e.user;
var cell = e.range.getA1Notation();
var oldValue = e.oldValue;
var newValue = e.value;
var sheetName = e.source.getActiveSheet().getName();

var logSheet = e.source.getSheetByName("Edit Log");

if (logSheet == null) {
logSheet = e.source.insertSheet("Edit Log");
logSheet.appendRow(["Timestamp", "User", "Cell", "Old Value", "New Value", "Sheet Name"]);
}

logSheet.appendRow([timestamp, user, cell, oldValue, newValue, sheetName]);
}

Step 5: Enable the Script

To enable the script and start capturing edits, simply make any edit to the spreadsheet. This will trigger the onEdit function, capturing the relevant information for the edit.

Step 6: View the Edit Log

To view the captured edits, navigate to the spreadsheet and locate the sheet named “Edit Log.” This sheet will contain a log of all the edits made to the spreadsheet, including the timestamp, user details, cell, old value, new value, and sheet name.

Conclusion

By implementing this simple solution using Google Apps Script, you can easily capture and log edits made to a Google Spreadsheet. This can be valuable in various scenarios, such as maintaining an audit trail, monitoring data modifications, or ensuring data integrity. Feel free to customize the script to suit your specific requirements and enhance the logging capabilities.

I hope this post helps you effectively track and monitor edits made to your Google Spreadsheets. Happy learning!

--

--