0

I'm making a script for my hubot that's supposed to print out all of the items of a column in a Smartsheet document. Unfortunately, one cannot simply print all elements of a column, as the cells are primarily stored in rows. In order to reference a cell in the API, one has to make a call to https://api.smartsheet.com/2.0/sheets/[SHEET-ID]/rows/[ROW-ID]/columns/[COLUMN-ID]. The row and column ID's are not simply their placement in the sheet, but rather they have unique multi-digit identifiers.

My plan to print the items of a column was to collect all of the row ID's from a given document in an array, rowNums; get the necessary column ID (the goal is to print a list of names, so we're looking for a column titled 'Name') and store it in colNum; then, for each element in rowNums, make an HTTP GET request to the cell with the element from rowNums and colNum and store that in an array that I would print to the user.

The latter part of the code seems fine, but I'm having troubles with simply referencing elements from the 'rows' array from a Smartsheet document. I have my code below in both CoffeeScript (which the script should be in) and JavaScript (compiled using 'coffee --c', so it's a bit messy). What am I doing wrong?

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

Also, how do I get rid of the 'return void 0' line from my CoffeeScript? It's there with or without the 'else > return undefined' lines.

jwilso48
  • 1
  • 2

1 Answers1

0

when I compile the code with coffee -c test.coffee (without the else return undefined) I get this:

// 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);
d4v3y0rk
  • 345
  • 1
  • 9