Nathaniel MacIver
Nathaniel MacIver

Reputation: 397

Run a Function on a timer from a Document-bound Google Apps Script

So, I've been tasked with periodically processing some data on a Google Sheet. I thought I would create a function to handle the process once every 15 minutes. As a test, I created a sort function that runs two commands:

function sort()
{
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(2, 1, sheet.getMaxRows()-1, 
  sheet.getMaxColumns()-2).sort([{column: 1, ascending: false}]);
}

I've tested it manually, and it works fine. I have created a trigger in the apps-script project to run this function once every 15 minutes. Checking back an hour later, though (and after putting something out of order first ;-) ), and nothing happened. Can I set triggers to run document-bound apps script functions unattended, or do I have to have the document open?

Upvotes: 2

Views: 409

Answers (1)

Wicket
Wicket

Reputation: 38150

getActiveSheet doesn't work on time-driven triggered scripts because there isn't a user that makes a sheet to be active, so you should have to replace this method by another way to set the sheet to work with.

You could try getSheetByName among others.

Upvotes: 2

Related Questions