0

I am building out a script on my Google Sheet that will catch a Webhook POST from my CRM and update a row on the worksheet. Everything is working perfectly, except I can't figure out how to format this one parameter.

It comes over in the webhook payload like this: ... "Dinner Seminar Session Choice":"Thursday, Feb 27th at 5:30pm", ...

The Google Sheet Script is this:

//this is a function that fires when the webapp receives a GET request
function doGet(e) {
  return HtmlService.createHtmlOutput("request received 11:40");
}

//this is a function that fires when the webapp receives a POST request
function doPost(e) {
  var params = JSON.stringify(e.postData.contents);
  params = JSON.parse(params);
  var myData = JSON.parse(e.postData.contents);
  var FirstName = myData.first_name;
  var LastName = myData.last_name;
  var Phone = myData.phone;
  var Session = myData.DinnerSeminarSessionChoice;
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = Math.max(sheet.getLastRow(),1);
  sheet.insertRowAfter(lastRow);
  var timestamp = new Date();
  sheet.getRange(lastRow + 1, 1).setValue(timestamp);
  sheet.getRange(lastRow + 1, 2).setValue(FirstName);
  sheet.getRange(lastRow + 1, 3).setValue(LastName);
  sheet.getRange(lastRow + 1, 4).setValue(Phone);
  sheet.getRange(lastRow + 1, 5).setValue(Session);
  sheet.getRange(lastRow + 1, 6).setValue(params);
  SpreadsheetApp.flush();
  return HtmlService.createHtmlOutput("post request received");
}

function myFunction() {

}

You can see on line 14, I'm trying to set the content of that param as var Session. But it doesn't seem to work with or without the spaces.

I'm sure this is a simple formatting error, but I just don't know how to do it. I appreciate any help.

Jason

  • Does this answer your question? [Get Json specific variable data from Coinmarketcap API](https://stackoverflow.com/questions/56448428/get-json-specific-variable-data-from-coinmarketcap-api) – TheMaster Feb 23 '20 at 05:55
  • Unfortunately, I didn't really see anything there that would help me format this variable. Maybe it does, but I don't understand it. – Jason Denniston Feb 23 '20 at 06:05
  • 1
    Read and practice [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors) and [this](https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) – TheMaster Feb 23 '20 at 06:07
  • I was able to figure it out from your resources. Thank You. – Jason Denniston Feb 23 '20 at 06:16

1 Answers1

0

As pointed to by @TheMaster's comments.

Property accessors can be used with "[ ]" to read or write property names with spaces.


Example:

const json_response = {
  "oldValue": "false",
  "trigger Uid": "30023847",
  "user": {
    "nick name": "fat.mike",
  }
}

console.log(json_response.oldValue)
json_response.oldValue = null;
console.log(json_response['oldValue'])

console.log(json_response['trigger Uid'])

console.log(json_response.user);
console.log(json_response.user['nick name'])
Aerials
  • 4,231
  • 1
  • 16
  • 20