【问题标题】:sheetrock js callback functionsheetrock js回调函数
【发布时间】:2018-01-05 09:35:06
【问题描述】:

如何在 sheetrock.js 中使用回调选项?我正在尝试将 sheetrock 的输出作为变量用作另一个函数的输入,但由于我的 JS 知识有限,因此遇到了困难。有什么帮助吗?

    var d = new Date();
var month = new Array();
month[0] = "Jan";
month[1] = "Feb";
month[2] = "Mar";
month[3] = "Apr";
month[4] = "May";
month[5] = "Jun";
month[6] = "Jul";
month[7] = "Aug";
month[8] = "Sep";
month[9] = "Oct";
month[10] = "Nov";
month[11] = "Dec";
var n = month[d.getMonth()];
var y = d.getFullYear();
$("#value").sheetrock({
  url: 'https://docs.google.com/spreadsheets/d/1XSVP75fJkq34gLHhwIsEWOC3My-6xnbVURk4U_wotD0/edit#gid=0',
  query: "select C where B = "+ y +" and A = '"+ n +"'",
  callback: draw
});

function draw(data, sheetrock,response) {

   console.log(); // how to get the output from sheetrock as a variable?

}

【问题讨论】:

    标签: javascript callback sheetrock


    【解决方案1】:

    Sheetrock 回调的工作原理如下:

    function draw(error, options, response) {
      if (error) {
        console.log('fail');
      }
      else{
        console.log(response.html);
      }
    }
    

    编辑:将其放入全局变量:

    var globalVar = {};
    function draw(data, sheetrock,response) {
        globalVar.response = response.html;
    }
    function use(){
        $('#test').html(globalVar.response);
    }
    

    完整的工作示例: https://codepen.io/andreds/pen/baomVq

    【讨论】:

    • 感谢您的回复。但是,问题是我在 JS 方面没有太多专业知识。您能否展示如何将 console.log 的输出转换为变量(稍后在函数外部使用)?此外,我在控制台中得到“未定义”,而不是预期值。
    • 哦,你想把它放在一个全局变量中吗?您可以使用:window.yourGlobalVariable = response.data;
    • 对不起安德烈,但我仍然在控制台中得到“未定义”。有什么建议吗?
    • 你可以尝试做:console.log(response) 并给我输出吗?
    • 这里是响应:e {request: e, options: e, raw: {…}, attributes: {…}, rows: Array(2), …} attributes: {last: 1 , rowNumberOffset: 0, 标签: Array(1)} html : "26.68" 选项: e {user: {…}, request: {…}, requestIndex: " 1XSVP75fJkq34gLHhwIsEWOC3My-6xnbVURk4U_wotD0_0_select C where B = 2018 and A = 'Jan'"} raw : {version: "0.6", reqId: "0", status: "ok", sig: "601120954", table: {…}} request : e {options: e, index: "1XSVP75fJkq34gLHhwIsEWOC3My-6xnbVURk4U_wotD0_0_select C where B = 2018 and A = 'Jan'"} 行:(2) [e, e] proto : Object
    猜你喜欢
    • 2021-02-09
    • 1970-01-01
    • 2012-07-27
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 2014-05-05
    相关资源
    最近更新 更多