0

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

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

代码的后半部分似乎很好,但我在简单地引用 Smartsheet 文档中的“行”数组中的元素时遇到了麻烦。我在CoffeeScript(脚本应该在其中)和JavaScript(使用'coffee --c'编译,所以有点乱)都有我的代码。我究竟做错了什么?

咖啡脚本

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 > return undefined' 行。

4

1 回答 1

0

当我用 coffee -c test.coffee 编译代码(没有 else return 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);
于 2015-09-17T13:34:57.373 回答