3

In Google-Apps-Script I have created a function which returns a string containing the id of a spreadsheet file (this id will vary from user to user, as it is a new file copied from a template).

function findSheet(){ 
//Opens User's Google Drive and searches files for a spreadsheet called "blahblah" and returns the active spreadsheet
var files = DriveApp.getFilesByName("blahblah");
var file = files.next();
var newID = file.getId();
Logger.log(newID);
return newID;
}

The Logger.log(newID) shows the id name of the file in my testing. However when I try to initiate a javascript variable to that returning id string it becomes undefined.

<script>
var idname;
idname = google.script.run.findSheet();
alert(idname);

I have another google scripts app function (which is called from javascript )which uses that id string variable as a parameter in order to write user input from my html page into their google spreadsheet as part as a web app, but the id name from the javascript goes in as undefined and the function does not work.

Is there something I do not know on why the apps script string is undefined when called on by javascript?

I tried using idname.toString() to fix but it still shows as undefined when i use an alert(idname) or document.write(idname) or rewriting a paragraph

<p idname="showmeID"></p>
<script>
document.showmeID.innerHTML.idname
C Tracey
  • 33
  • 1
  • 6
  • In addition to the great answer below, remember that variables do not keep their values between or across execution instances. These kind of IDs are good candidates for storing in the User Properties (IMO). Lots of Q&A on here and in GAS documentation re: how to do that if you need it. – ballenf Aug 16 '16 at 17:05

1 Answers1

4

google.script.run is an asynchronous client-side JavaScript API available in HTML-service pages that can call server-side Apps Script functions.

According to the documentation this API is asynchronous and does not return the value directly. Instead it returns the value in a callback function.

google.script.run
    .withFailureHandler(function(err){
        // callback function that will be executed when some error will occur.
        // Error object is passed as the first argument.
        console.log("failure handler", err)
    })
    .withSuccessHandler(function(res){
        // here withSuccessHandler runs when your findSheet function runs successfully.
        // value returned from google app script will be passed as the first argument of this callback function.
        // So you will receive the value returned from GAS in variable `res`.
        console.log("response from findSheet function", res)
    })
    .findSheet();
m5khan
  • 2,667
  • 1
  • 27
  • 37
  • Would I be able to return the variable res if I were to define a function as: variable idNameOfFile = google.script.run.withFailureHandler(function(err){}).withSuccessHandler(function(res){ return res; }).findSheet(); – C Tracey Aug 24 '16 at 17:37
  • if you will see the documentation https://developers.google.com/apps-script/guides/html/reference/run it says : void — this method is asynchronous and does not return directly; however, the server-side function can can return a value to the client as a parameter passed to a success handler; also, return types are subject to the same restrictions as parameter types, except that a form element is not a legal return type – m5khan Aug 24 '16 at 18:06
  • you should get yourself familiarize with javascript asynchronous api and promises... promises can be use to tackle the problem of "callback hell". async js: https://blog.risingstack.com/asynchronous-javascript/ callback hell: http://callbackhell.com/ promises: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise you can find better resources on the internet... these are the ones I found quickly via googling – m5khan Aug 24 '16 at 18:13
  • whatever (string/object/array) you will return from google app script function (in your case from findSheet function), you will get that value in .withSuccessHandler(function(res){ // res is your value }) . Here function(res){ } runs asynchronously i.e later after the execution of your findSheet() is completed and you cannot return it directly. you can pass it to some other function for further processing. or you can use javascript promise – m5khan Aug 24 '16 at 18:17