1

Ok so right now I'm doing this:

google.script.run
  .withSuccessHandler(updateOutput)
  .withFailureHandler(errorOutput)
  .finish();

And then

  function updateOutput(info) 
  {
    var br='<br />';
    var outputDiv = document.getElementById('status');
    outputDiv.innerHTML = 'First Logic Compete' + br +   br +'GotoLogic: ' +info.slide+ br + 'Copy text: ' + info.text + br ;  
  }

Is there any way to cut out the need to call another function? and directly interact with the google.script.run result object inside the first function?

Edit, this doesn't work either, the number returned is blank:

var object = google.script.run
  .withSuccessHandler(function (number) {
    document.getElementById('bugLink').href = "https://bug.com/issues/" + number;
    document.getElementById('time').innerHTML = number;
  })
  .finish();
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
J. G.
  • 1,922
  • 1
  • 11
  • 21

2 Answers2

4

A handler to be called by the other code whenever the other code is done is a requirement of asynchronous communication. If you want, you can define the handler inline:

const TASK = google.script.run.withFailureHandler(errorOutput);
TASK
  .withSuccessHandler((info, userObj) => {
    ...
  })
  .foo();
TASK
  .withSuccessHandler((otherInfo, userObj) => {
    ...
  })
  .otherFoo();
...

Or if you despise callbacks, you can use Promises in your client side HTML:

const makeAppsScriptCall = (fnName, obj, ...args) => {
  return new Promise((resolve, reject) => {
    let TASK = google.script.run
      .withSuccessHandler(resolve)
      .withFailureHandler(reject);
    if (obj) {
      TASK = TASK.withUserObject(obj);
    }
    if (TASK[fnName] === undefined) {
      reject("'" + fnName + "' is not a global function in your Apps Script project");
    } else {
      TASK[fnName].apply(null, args);
    }
  });
};

function doStuffAsPromises(userObjBtn) {
  makeAppsScriptCall("finish", userObjBtn, myarg1, myarg2, myarg3, ...)
    .then(...)
    .catch(...);
}

(Obviously if the client browser doesn't support Promises or the spread syntax for "rest parameters", you will need to polyfill / modify as appropriate.)

References

tehhowch
  • 9,645
  • 4
  • 24
  • 42
2

Do you mean this?

This is client side:

google.script.run
.withSuccessHandler(function(html){
     document.getElementById('id').innerHTML=html;
   })
.getHtml();

Server Side:

function getHtml() {
  return '<h1>Hello World</h1>';
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Maybe? I want access to the object passed back from "finish" without having to pass that object to a success function. It kind of looks like you are writing a success function into the handler? – J. G. Mar 19 '19 at 19:04
  • That will do it. The object passed back by `getHtml()` in this case is `html`; It's okay to use and anonymous function in this situation. – Cooper Mar 19 '19 at 19:05
  • Strangely this isn't working: the number returned is blank (SEE ABOVE EDITS) – J. G. Mar 19 '19 at 19:20
  • You have to write the getHtml() functon in google script. – Cooper Mar 19 '19 at 19:22