Cannot call Google Script API Functions from Web App (TypeError: Cannot read property 'run' of undefined) -
i had google apps script working fine months , stopped working. i'm wondering if google deprecated part of code or something.
this link file: click here view google spreadsheet file
the google script code apparently failing when trying call google script function html file. line of code fails.
google.script.run.importcsvdata(id);
the lines fails , catches following error:
typeerror: cannot read property 'run' of undefined
like said, had code working fine months , stopped working.
(fyi... purpose of code upload csv file tab "archivo plano adn". run code, 1 must select "importar archivo csv" --> "importar archivo..." upper menu. when prompted select file, select .csv file. csv file must separated ;)
my google spreadsheet file has 3 code files ("code.gs", "codeimport.gs" , "picker.html")
let me give code of each:
code.gs:
function onopen() { var me = session.geteffectiveuser(); if (me.getemail() == "alejandro.sardi@crusardi.net") { var ui = spreadsheetapp.getui(); ui.createmenu('protected ranges') .additem('remove protection', 'menuitem1') .additem('copy protected ranges sheet', 'menuitem2') .additem('copy sm protected ranges sa sheets', 'menuitem3') .addtoui(); ui.createmenu('importar archivo csv') .additem('importar archivo...', 'showpicker') .addtoui(); ui.cre } else { spreadsheetapp.getui() // or documentapp or formapp. .createmenu('importar archivo csv') .additem('importar archivo...', 'showpicker') .addtoui(); } }
codeimport.gs:
function importcsvdata(id) { try { var file = driveapp.getfilebyid(id); var csvdata = utilities.parsecsv(file.getblob().getdataasstring("iso-8859-1"),";"); var sheet = spreadsheetapp.getactivespreadsheet().getsheetbyname("archivo plano adn"); sheet.getrange("a:u").clear(); sheet.getrange("g:g").setnumberformat('@string@'); logger.log("yes"); sheet.getrange(1, 1, csvdata.length, csvdata[0].length).setvalues(csvdata); file.settrashed(true); spreadsheetapp.getui().alert("archivo importado con éxito"); } catch (e) { mailapp.sendemail(session.geteffectiveuser().getemail(), "error report jaja - sonríele la vida!", "\r\nmessage: " + e.message + "\r\nfile: " + e.filename + "\r\nline: " + e.linenumber); } } function showpicker() { var html = htmlservice.createhtmloutputfromfile('picker.html') .setwidth(600) .setheight(425) .setsandboxmode(htmlservice.sandboxmode.iframe); spreadsheetapp.getui().showmodaldialog(html, 'select file'); } function getoauthtoken() { driveapp.getrootfolder(); return scriptapp.getoauthtoken(); } function finishedimport() { spreadsheetapp.getui().alert("archivo importado exitosamente"); } function senderror(e) { mailapp.sendemail(session.geteffectiveuser().getemail(), "error report jaja - sonríele la vida!", "\r\nmessage: " + e.message + "\r\nfile: " + e.filename + "\r\nline: " + e.linenumber); }
and finally, picker.html file:
<!doctype html> <html> <head> <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css"> <script type="text/javascript"> var dialog_dimensions = { width: 600, height: 425 }; var pickerapiloaded = false; function onapiload() { gapi.load('picker', { 'callback': function() { pickerapiloaded = true; } }); google.script.run.withsuccesshandler(createpicker) .withfailurehandler(showerror).getoauthtoken(); } function createpicker(token) { if (pickerapiloaded && token) { //var docsview = new google.picker.docsview() //.setincludefolders(true) //.setmimetypes('application/vnd.google-apps.folder') //.setselectfolderenabled(true); var uploaddocsview = new google.picker.docsuploadview() .setincludefolders(true) //.setmimetypes('application/vnd.google-apps.folder') //.setselectfolderenabled(true); var picker = new google.picker.pickerbuilder() //.addview(docsview) .addview(uploaddocsview) //.setappid("aizasyczda4jkkiov2af3qyrg8dnvoxmz27054o") .enablefeature(google.picker.feature.nav_hidden) .hidetitlebar() .setsize(dialog_dimensions.width - 2, dialog_dimensions.height - 2) .setoauthtoken(token) .setcallback(pickercallback) .setorigin('https://docs.google.com') .build(); picker.setvisible(true); } else { showerror('unable load file picker.'); } } /** * callback function extracts chosen document's metadata * response object. details on response object, see * https://developers.google.com/picker/docs/result * * @param {object} data response object. */ function pickercallback(data) { try { var action = data[google.picker.response.action]; if (action == google.picker.action.picked) { var doc = data[google.picker.response.documents][0]; var id = doc[google.picker.document.id]; // show id of google drive folder //document.getelementbyid('result').innerhtml = id; document.getelementbyid('result').innerhtml = "importando..." google.script.run.importcsvdata(id); //google.script.run.deleteimportedfile(id); google.script.host.close(); } else if (action == google.picker.action.cancel) { //document.getelementbyid('result').innerhtml = "cerrando1..." google.script.host.close(); //document.getelementbyid('result').innerhtml = "cerrando2..." } } catch (e) { document.getelementbyid('result').innerhtml = e; google.script.run.senderror(e); } } function showerror(message) { document.getelementbyid('result').innerhtml = 'error: ' + message; } </script> </head> <body> <div> <p id='result'></p> </div> <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onapiload"></script> </body> </html>
you need load google file picker using google api loader library.
replace:
<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onapiload"></script>
with:
<script type="text/javascript" src="https://www.google.com/jsapi"></script> <script>google.load("picker", "1", {callback:function(){pickerapiloaded =!0}});</script>
remember publish new version of web app after making change.
update: erik google apps script team says " cause of problem when picker api loads google.picker, overwriting google.script, google.script.run() calls start failing."
they have posted alternate solution - manually preserve , restore google.script when loading picker api:
window.script = google.script; gapi.load('picker', '1', {callback: function() { google.script = window.script; // ... }});
Comments
Post a Comment