I have a function that request for an URL and if i use that function with a return in it the entire html code will be in the cell with the function name request "=testFormula()". Why if i use the formula function it will work but if i use the event one it will not work ?
function onEdit(event){
testCom();
}
// This one will trigger automaticaly when you edit any cell in the sheet
function testCom(){
var doc = SpreadsheetApp.getActiveSpreadsheet();
doc.getRange('a1').setValue("Before fetching");
var response = UrlFetchApp.fetch("http://www.google.com/");
doc.getRange('a1').setValue(response);
}
// Use in a cell "=testFormula()" to run this function
function testFormula(){
var response = UrlFetchApp.fetch("http://www.google.com/");
return response;
}
The onEdit() is a simple trigger and there are restrictions on what you can do inside simple triggers.
To overcome your problem, do the following
a. Rename your function to something else, say onEdit1()
b. In the script editor, click Resources --> Current project's triggers and add a new on edit trigger pointing to this function.
Authorize your script if needed and you should be good to go