Last active
February 4, 2024 15:41
-
-
Save jamesmurdza/d767652e5e389cd7e741f78d85bb531b to your computer and use it in GitHub Desktop.
Sticky cells in Google Sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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