google apps script - URL fetch sometimes leads to "Uncaught ScriptError" resp. 404 -
my goal write add-on, converts spreadsheet file xlsx-file , sends given mail distribution list on daily, weekly or monthly basis.
everything works fine, except url fetch. please find function below:
function startwizard () { var addresses = spreadsheetapp.getactive().getsheetbyname("einstellungen").getrange("a1").getvalues(); var filename = spreadsheetapp.getactivespreadsheet().getname(); var ssid = spreadsheetapp.getactivespreadsheet().getid(); var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/export?key=" + ssid + "&exportformat=xlsx"; var token = scriptapp.getoauthtoken(); var response = urlfetchapp.fetch(url, {headers: {'authorization': 'bearer '+ token}}); logger.log(url); var contents = response.getcontent(); mailapp.sendemail(addresses,"subject" ,"body", {attachments:[{filename:filename+".xlsx", content:contents, mimetype:"application//xlsx"}]}); }
most of time works fine , receive e-mail , xlsx-file attached. nevertheless, time time following error message in browser console (translated german english) , not receive e-amil:
"uncaught scripterror: error @ request https://spreadsheets.google.com/feeds/download/spreadsheets/export?key=1ibiny93-y1sssgxwn78_c-_9epjipwmbga1ivn1dnkg&exportformat=xlsx. following code responded: 404. shortened server answer: "google answer in html code" use "mutehttpexceptions", read whole answer.
i have tried out other solutions nothing worked far. in addition tried search answer in forum, in vain.
it seems url correct, can download file. assumption there wrong oauth.
could please give me hint? driving me mad...
thank you!
thank pointnclick help. yes, tried using mutehttpexceptions. result is, xlsx-file gets exported , sent via mail, content of file "google answer in html code" = google docs etc.
in meanwhile have tried else, worked out me , hope won't face new problems. inserted "driveapp.getrootfolder();" here new code in case has same issue:
function startwizard () { var addresses = spreadsheetapp.getactive().getsheetbyname("einstellungen").getrange("a1").getvalues(); var filename = spreadsheetapp.getactivespreadsheet().getname(); var ssid = spreadsheetapp.getactivespreadsheet().getid(); var url ="https://docs.google.com/spreadsheets/d/"+ssid+"/export?format=xlsx&id="+ssid; driveapp.getrootfolder(); var token2 = scriptapp.getoauthtoken(); var response = urlfetchapp.fetch(url, { headers: {'authorization': 'bearer ' + token2}}); var contents = response.getcontent(); mailapp.sendemail(addresses,"subject" ,"body", {attachments:[{filename:filename+".xlsx", content:contents, mimetype:"application//xlsx"}]}); }
as spent lot of time figure out , oauth , running, mention, did not use oauth libraries , drive apis via advanced google services / google developer console. needed in code. hope example other developers.
Comments
Post a Comment