Skip to content

Instantly share code, notes, and snippets.

@dwightmulcahy
Last active January 30, 2025 23:39
Show Gist options
  • Select an option

  • Save dwightmulcahy/fb4d65d18caf7fec73c5fcb8867051a4 to your computer and use it in GitHub Desktop.

Select an option

Save dwightmulcahy/fb4d65d18caf7fec73c5fcb8867051a4 to your computer and use it in GitHub Desktop.
Google Sheet App Script function that will trigger an refresh of cells that are dependent on it.
//
// Trigger function that updates just one cell 'MySheet:$A$1`.
//
// Why? So that it triggers a refresh!
//
// How? Thats the fun part, if you add dependent cells to a
// user formula it causes a refresh.
//
// You can tell the spreadsheet which cells the function is
// dependent on by passing them to the formula at the end of
// its expected parameters.
//
// For Example:
//
// =MyFunction("this", "that", MySheet:$A$1)
//
// This tells the sheet that it is dependent on MySheet:$A$1 and to
// refresh if that cell changes.
//
// You have to create a Trigger to make this work.
//
function triggerRefresh() {
let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Current')
let cell = sheet.getRange("$A$1");
// Make a "change" that will cause cells depend on it recalculate
var currentValue = cell.getValue();
cell.setValue(currentValue);
// Make a comment on the cell what time data was refreshed
const now = new Date();
cell.setComment(`Updated data at ${now}`);
console.log(`Updated data at ${now}`)
}
@dwightmulcahy
Copy link
Author

dwightmulcahy commented Jan 30, 2025

Google Sheets only recalculates custom functions when their explicit arguments change. It does not automatically track dependencies like built-in functions do.

To make sure your custom function updates when a particular cell changes, pass the dependent cell's value as an argument.

@dwightmulcahy
Copy link
Author

To create a trigger in Google Sheets using Apps Script, you can use the script editor to manually create a trigger.

Manual creation

  • Open the Apps Script project
  • Click the Triggers icon, which looks like a clock
  • Click Add Trigger
  • Choose the function to trigger
  • Select the trigger type, such as time-driven or event-driven
  • Configure the trigger settings
  • Click Save

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment