How can I find the field names in Smartsheet for an Apps Script Integration?

Viewed 40

I'm trying to connect a smartsheet table to Googlesheets using apps script. I'm able to connect to the smartsheet but pulling the data based on the field names isn't working.

function getSpeakers() {
  var speakers = requestsmartsheet();
  //console.log("speakers: ", speakers)
  var speaker_info = [];

  //Here is where I'm having trouble with the field names. Field names are the exact names from the table
  for(i=0; i<speakers.length; i++) {
    var fields = speakers[i].fields;
    speaker_info.push([
      fields.FirstName,
      fields.LastName,
      fields.Email,
      fields.E-Number
    ])
  }
  console.log("speaker_info: ", speaker_info);
}

function requestsmartsheet() {
  var url = "https://api.smartsheet.com/2.0/sheets/[link ID]"
  var headers = {
    "Authorization": "Bearer [token]",
    "Content-Type": "application/json"
  }
  var options = {
    headers: headers,
    method: "GET"
  }

  var response = UrlFetchApp.fetch(url, options);
  var result = JSON.parse(response);
  //console.log("result: ", result)

  return result.records
}
1 Answers

Try replacing fields.E-Number by fields.['E-Number']. If that doesn't work, use the debugger or add console.log(JSON.stringify(speakers, null, ' ') after var speakers = requestsmartsheet(); to review the records property that your script is receiving.

You might also spend some time reviewing the Smartsheet API docs.

Related

Related