Ricardo Fumachi
Ricardo Fumachi

Reputation: 77

How to trigger a google spreadsheet function onEdit?

function oneWeek() {

  var sheetURL = SpreadsheetApp.getActive().getUrl()
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
  var sheet = spreadsheet.getActiveSheet();

  // do stuff with sheet

}

How do I get this function to trigger onEdit?

I've tried this for the sheet,

function onEdit(event) { 

  var sheet = event.source.getSheetByName('x'); 
  // do stuff with sheet

}

EDIT: I forgot to mention that my script did https request so that's the reason onEdit didn't work. The solution is here: https://productforums.google.com/forum/#!topic/docs/0nxLYWXVo6Y

Because this script does other things, like a HTTP request, it cannot be assigned to the default onEdit() functions, you need to create it as a custom function and assign it in the Edit menu. Edit->Current Project Triggers and create a new one for onEdit. Just point it at your function and you’re done.

Upvotes: -1

Views: 3997

Answers (1)

Cooper
Cooper

Reputation: 64032

function onEdit(e){
if(e.range.getSheet().getName()!='x'){return;}
//do stuff on sheet x here
}

Upvotes: 1

Related Questions