有没有办法走出了成功处理程序的值,而无需调用其他功能?(Is there a way to get

2019-10-28 14:20发布

好了,所以现在我这样做:

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

接着

  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 ;  
  }

有什么办法,以切出需要调用另一个函数? 并直接与交互google.script.run第一函数内部结果对象?

编辑,这也不管用,返回的数字是空白:

var object = google.script.run
  .withSuccessHandler(function (number) {
    document.getElementById('bugLink').href = "https://bug.com/issues/" + number;
    document.getElementById('time').innerHTML = number;
  })
  .finish();

Answer 1:

你的意思呢?

这是客户端:

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

服务器端:

function getHtml() {
  return '<h1>Hello World</h1>';
}


Answer 2:

处理程序通过每当其他代码完成是异步通信的要求的其他代码调用。 如果你愿意,你可以定义处理器内联:

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

或者,如果你鄙视回调,您可以在客户端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(...);
}

(显然,如果客户端浏览器不支持的承诺或“其他参数”蔓延的语法,你需要填充工具/适当的修改。)

参考

  • Function#apply
  • Promises
  • 其余的参数
  • Google Apps脚本客户端-服务器通信


文章来源: Is there a way to get the values out of a success handler without calling another function?