INTRO: Using onChange to Trigger Specific Events

This is an introduction to the concept of using onChange to trigger specific events as opposed to onEdit.
The challenge: onEdit() doesn’t fire when the change is the result of a formula, including IMPORTRANGE(), GOOGLEFINANCE(), etc. and onChange() doesn’t include the source, range, or value properties.
Important notes:
* onChange only runs when the Sheet is open; the Sheet will not update “in the background”
* You can call the function whatever you want; onChange is an installed trigger and must be installed from the Triggers section, which means it can be applied to any function
* You must use the PropertiesService before the onChange to set each key-value pair so the onChange script has a starting value
* Using the cell A1 notation as the key name makes it easier to process since the SpreadsheetApp can access the range via the key name

IMPORTRANGE Source Sheet (to copy from File : Make a copy)

Testing sheet with script (to copy from File : Make a copy)
* Remember to update the IMPORTRANGE sheet ID

function onChange(e){
if(e.changeType != “OTHER”) return;
let props = PropertiesService.getScriptProperties().getProperties();
const sh = SpreadsheetApp.getActive();
const ss = sh.getActiveSheet();

let r, val;

for (let p in props){
r = ss.getRange(p);
val = r.getValue();

if(val != props[p]){
PropertiesService.getScriptProperties().setProperty(“A2”,SpreadsheetApp.getActive().getActiveSheet().getRange(“A2”).getValue()); // run for each relevant cell

function setupProperties(){ // run for each relevant cell

function retrieveProps(){

Connect with me:
• Twitter @FarrisSpencer
• Google Product Expert

Leave a Reply

Your email address will not be published. Required fields are marked *