如何从HTTP GET请求引用数组?

时间:2015-07-24 21:27:28

标签: javascript http coffeescript hubot smartsheet-api

我正在为我的hubot制作一个脚本,该脚本应打印出Smartsheet文档中列的所有项目。不幸的是,人们不能简单地打印列的所有元素,因为单元格主要存储在行中。为了引用API中的单元格,必须调用https://api.smartsheet.com/2.0/sheets/[SHEET-ID]/rows/[ROW-ID]/columns/[COLUMN-ID]。行ID和列ID不仅仅是它们在工作表中的位置,而是具有唯一的多位数标识符。

我打印列项目的计划是收集数组中给定文档的所有行ID,rowNums;获取必要的列ID(目标是打印一个名称列表,所以我们正在寻找一个名为'Name'的列)并将其存储在colNum中;然后,对于rowNums中的每个元素,使用rowNums和colNum中的元素向单元格发出HTTP GET请求,并将其存储在我将打印给用户的数组中。

代码的后半部分似乎很好,但是我很容易从Smartsheet文档中引用'rows'数组中的元素。我在下面的代码中都有CoffeeScript(脚本应该在其中)和JavaScript(使用'coffee --c'编译,因此它有点乱)。我做错了什么?

的CoffeeScript

robot.http(url)
  .headers(Authorization: auth, Accept: 'application/json')
  .get() (err, res, body) ->
    data = JSON.parse(body)
    if res.statusCode isnt 200
      msg.send "An error occurred when processing your request:
                #{res.statusCode}. The list of error codes can be found at
                http://bit.ly/ss-errors. Talk to the nearest code nerd for
                assistance."
    else
      # Populate 'rows' with all rowId's from default sheet.
      rowNums = (row.id for row in data.rows)
      # Parses 'columns' for column titled 'Name'. Stops when it finds it.
      for column in data.columns
        if column.title.toLowerCase() == "name"
          colNum = column.id
          break
        else
          return undefined

JAVASCRIPT

robot.http(url).headers({
  Authorization: auth,
  Accept: 'application/json'
}).get()(function(err, res, body) {
  var column, data, i, len, ref, row;
  data = JSON.parse(body);
  if (res.statusCode !== 200) {
    return msg.send("An error occurred when processing your request: " + res.statusCode + ". The list of error codes can be found at http://bit.ly/ss-errors. Talk to the nearest code nerd for assistance.");
  } else {
    rowNums = (function() {
      var i, len, ref, results;
      ref = data.rows;
      results = [];
      for (i = 0, len = ref.length; i < len; i++) {
        row = ref[i];
        results.push(row.id);
      }
      return results;
    })();
    ref = data.columns;
    for (i = 0, len = ref.length; i < len; i++) {
      column = ref[i];
      if (column.title.toLowerCase() === "name") {
        colNum = column.id;
        break;
      } else {
        return void 0;
      }
    }
  }
});

另外,如何从我的CoffeeScript中删除'return void 0'行?它有或没有'else&gt;返回undefined'lines。

1 个答案:

答案 0 :(得分:0)

当我用coffee -c test.coffee编译代码时(没有返回undefined)我得到了这个:

// Generated by CoffeeScript 1.9.3
(function() {
  robot.http(url).headers({
    Authorization: auth,
    Accept: 'application/json'
  }).get()(function(err, res, body) {
    var colNum, column, data, i, len, ref, results, row, rowNums;
    data = JSON.parse(body);
    if (res.statusCode !== 200) {
      return msg.send("An error occurred when processing your request: " + res.statusCode + ". The list of error codes can be found at http://bit.ly/ss-errors. Talk to the nearest code nerd for assistance
    } else {
      rowNums = (function() {
        var i, len, ref, results;
        ref = data.rows;
        results = [];
        for (i = 0, len = ref.length; i < len; i++) {
          row = ref[i];
          results.push(row.id);
        }
        return results;
      })();
      ref = data.columns;
      results = [];
      for (i = 0, len = ref.length; i < len; i++) {
        column = ref[i];
        if (column.title.toLowerCase() === "name") {
          colNum = column.id;
          break;
        } else {
          results.push(void 0);
        }
      }
      return results;
    }
  });

}).call(this);