Skip to content

Instantly share code, notes, and snippets.

@jamesmurdza
Last active February 4, 2024 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmurdza/d767652e5e389cd7e741f78d85bb531b to your computer and use it in GitHub Desktop.
Save jamesmurdza/d767652e5e389cd7e741f78d85bb531b to your computer and use it in GitHub Desktop.
Sticky cells in Google Sheets
// This code takes values put in to column A, and applies a formula to calculate column B.
// The result of the formula, not the formula, is added to column B.
function onEdit(e) {
var range = e.range;
var sheet = range.getSheet();
var row = range.getRow();
var col = range.getColumn();
// Check if the edit is in the right sheet and location.
if (row > 1 && col == 1 && sheet.getName() == 'Sheet1') {
var cell = sheet.getRange(row, col);
var formulaCell = sheet.getRange(row, 2);
// Check if the input cell is empty.
if (cell.getValue() == "") {
formulaCell.setValue("");
return;
}
// Temporarily set the formula.
formulaCell.setFormula('=GPT(A' + row + ')');
// Ensure that the setFormula calculation is completed.
SpreadsheetApp.flush();
// Now set the value directly, removing the formula
var calculatedValue = formulaCell.getValue();
formulaCell.setValue(calculatedValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment