Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pascalwacker/cbdd569a0560a60101f9aef64b16e5c9 to your computer and use it in GitHub Desktop.
Save pascalwacker/cbdd569a0560a60101f9aef64b16e5c9 to your computer and use it in GitHub Desktop.
Google Sheets Script to update a timestamp if a field in the row gets updated
function onEdit() {
updateSwissMarchesCharacterReceivedXP();
}
function updateSwissMarchesCharacterReceivedXP() {
// enable/disable debug mode
var debugMode = false;
// definitions
var sheetName = "Player Characters";
var startRow = 2;
var xpColumn = 13; // "M" column
var updatedColumn = 17; // "Q" column
// get the current active sheet
var activeSheet = SpreadsheetApp.getActiveSheet();
// check the current sheet
debugMode && Logger.log("active sheet: "+activeSheet.getName());
if (activeSheet.getName() !== sheetName) {
debugMode && Logger.log("doesn't match our sheetName: " + sheetName);
return;
}
// get the current cell (and row index)
var cell = activeSheet.getActiveCell();
var curRow = cell.getRowIndex();
// check the current row
debugMode && Logger.log("current Row: "+curRow);
if (curRow < startRow) {
debugMode && Logger.log("curRow < startRow -> exit");
return;
}
// check if we're in the XP column
debugMode && Logger.log("current Column: "+cell.getColumn());
if (cell.getColumn() != xpColumn) {
debugMode && Logger.log("cell.getColumn() != xpColumn -> exit");
return;
}
// set updated at
debugMode && Logger.log("set updated at");
activeSheet.getRange(curRow, updatedColumn).setValue(new Date());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment