简体   繁体   中英

Formatting Parameter for Google Scripts

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

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM