sheetrock js callback function

2019-08-24 13:00发布

问题:

How to use callback option in sheetrock.js? I'm trying to get the output from the sheetrock as a variable to be used as input for another function, but having difficulty due to my limited JS knowledge. Any help please?

    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?

}

回答1:

Here is how sheetrock callback works :

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

EDIT : To put it into a global variable :

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

Full working example : https://codepen.io/andreds/pen/baomVq