Json 在Google脚本项目中使用JIRA webhook数据

Json 在Google脚本项目中使用JIRA webhook数据,json,jira,spreadsheet,Json,Jira,Spreadsheet,我正在努力实现以下功能:一个JSON被发送到我的Google脚本——现在我想从JSON消息中获取特定值,并将它们存储到特定Google电子表格的特定行中。到目前为止,很好,这就是我所拥有的: function doPost(response) { var sheets = SpreadsheetApp.openById('MY SHEET ID'); var dataAll = JSON.parse(response.getContentText()); var nR = get

我正在努力实现以下功能:一个JSON被发送到我的Google脚本——现在我想从JSON消息中获取特定值,并将它们存储到特定Google电子表格的特定行中。到目前为止,很好,这就是我所拥有的:

function doPost(response) {

 var sheets = SpreadsheetApp.openById('MY SHEET ID');

  var dataAll = JSON.parse(response.getContentText());

  var nR = getNextRow(sheets) + 1;

// RECORD DATA IN SPREADSHEET
 sheets.getRangeByName('timestamp').getCell(nR,1).setValue(new Date());
 sheets.getRangeByName('ticket_id').getCell(nR,1).setValue(dataAll);

}

function getNextRow(sheets) {
  var timestamps = sheets.getRangeByName("timestamp").getValues();
  for (i in timestamps) {
    if(timestamps[i][0] == "") {
    return Number(i);
  break;
 }}}
它应该存储响应并将其放入范围为“timestamp”的空白单元格中。但在这一点上什么也没有发生

这是来自JIRA的JSON(正文):

{"timestamp":1483576902984,"webhookEvent":"jira:issue_created","issue_event_type_name":"issue_created","user":{"self":"https://xxx.atlassian.net/rest/api/2/user?username=admin","name":"admin","key":"admin","emailAddress":"test@mail.at","avatarUrls":{"48x48":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=48","24x24":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=24","16x16":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=16","32x32":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=32"},"displayName":"Max Mustermann  [Administrator]","active":true,"timeZone":"Europe/Berlin"},"issue":{"id":"10057","self":"https://xxx.atlassian.net/rest/api/2/issue/10057","key":"TA-58","fields":{"issuetype":{"self":"https://xxx.atlassian.net/rest/api/2/issuetype/10104","id":"10104","description":"A problem which impairs or prevents the functions of the product.","iconUrl":"https://xxx.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10303&avatarType=issuetype","name":"Bug","subtask":false,"avatarId":10303},"timespent":null,"project":{"self":"https://xxx.atlassian.net/rest/api/2/project/10000","id":"10000","key":"TA","name":"Test Area","avatarUrls":{"48x48":"https://xxx.atlassian.net/secure/projectavatar?avatarId=10324","24x24":"https://xxx.atlassian.net/secure/projectavatar?size=small&avatarId=10324","16x16":"https://xxx.atlassian.net/secure/projectavatar?size=xsmall&avatarId=10324","32x32":"https://xxx.atlassian.net/secure/projectavatar?size=medium&avatarId=10324"}},"customfield_10110":null,"fixVersions":[],"customfield_10111":null,"aggregatetimespent":null,"customfield_10112":"Not started","resolution":null,"customfield_10113":null,"customfield_10114":null,"customfield_10104":null,"customfield_10105":null,"customfield_10106":null,"customfield_10107":null,"customfield_10108":null,"customfield_10109":null,"resolutiondate":null,"workratio":-1,"lastViewed":null,"watches":{"self":"https://xxx.atlassian.net/rest/api/2/issue/TA-58/watchers","watchCount":0,"isWatching":false},"created":"2017-01-05T01:41:42.903+0100","priority":{"self":"https://xxx.atlassian.net/rest/api/2/priority/3","iconUrl":"https://xxx.atlassian.net/images/icons/priorities/medium.svg","name":"Medium","id":"3"},"customfield_10100":null,"customfield_10101":null,"customfield_10102":null,"customfield_10103":null,"labels":[],"timeestimate":null,"aggregatetimeoriginalestimate":null,"versions":[],"issuelinks":[],"assignee":null,"updated":"2017-01-05T01:41:42.903+0100","status":{"self":"https://xxx.atlassian.net/rest/api/2/status/10000","description":"","iconUrl":"https://xxx.atlassian.net/","name":"To Do","id":"10000","statusCategory":{"self":"https://xxx.atlassian.net/rest/api/2/statuscategory/2","id":2,"key":"new","colorName":"blue-gray","name":"To Do"}},"components":[],"timeoriginalestimate":null,"description":"super alles neu","timetracking":{},"customfield_10005":null,"attachment":[],"aggregatetimeestimate":null,"summary":"super alles neu","creator":{"self":"https://xxx.atlassian.net/rest/api/2/user?username=admin","name":"admin","key":"admin","emailAddress":"test@mail.at","avatarUrls":{"48x48":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=48","24x24":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=24","16x16":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=16","32x32":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=32"},"displayName":"Max Mustermann  [Administrator]","active":true,"timeZone":"Europe/Berlin"},"subtasks":[],"reporter":{"self":"https://xxx.atlassian.net/rest/api/2/user?username=admin","name":"admin","key":"admin","emailAddress":"test@mail.at","avatarUrls":{"48x48":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=48","24x24":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=24","16x16":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=16","32x32":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=32"},"displayName":"Max Mustermann  [Administrator]","active":true,"timeZone":"Europe/Berlin"},"customfield_10000":"{}","aggregateprogress":{"progress":0,"total":0},"customfield_10001":null,"customfield_10115":null,"customfield_10116":"0|i0005r:","environment":null,"duedate":null,"progress":{"progress":0,"total":0},"comment":{"comments":[],"maxResults":0,"total":0,"startAt":0},"votes":{"self":"https://xxx.atlassian.net/rest/api/2/issue/TA-58/votes","votes":0,"hasVoted":false},"worklog":{"startAt":0,"maxResults":20,"total":0,"worklogs":[]}}}}

但是,我不希望整个JSON都在我的单元格中,我只希望JSON中有特定的obejcts/id。我怎么称呼他们

这里有一个例子:

/*
 * webhookHandler: JIRA webhook callback function
 */
function webhookHandler(response) {
    var data = response.getAs("application/json");
    //logs out data in dev console
    console.log(data);

    var spreadsheet = SpreadsheetApp.openById("<spreadsheet id>");
    var cellRange = spreadsheet.getRangeByName("<some range name>");
    var cell = cellRange.getCell(0 /*row index*/, 0/*column index*/);

    cell.setValue(data.ticket_id/*index the JSON object returned by response*/);
}
/*
*webhookHandler:JIRA webhook回调函数
*/
函数webhookHandler(响应){
var data=response.getAs(“application/json”);
//在开发人员控制台中注销数据
控制台日志(数据);
var电子表格=SpreadsheetApp.openById(“”);
var cellRange=spreadsheet.getRangeByName(“”);
var cell=cellRange.getCell(0/*行索引*/,0/*列索引*/);
cell.setValue(data.ticket_id/*索引响应返回的JSON对象*);
}


经过大量研究,这是一个适合我的解决方案(以我为例):


这两条线:

var json = response.postData.contents;
var data = JSON.parse(json);

可以读取
正文
并索引我想要的所有特定参数

这篇文章需要大量的编辑;这里有太多的行话,没有人关心JSON来自何处以及您对什么属性感兴趣。让我们从说明存储JSON的变量开始…
getCell()
应获得您选择的单元格,
setValue()
将相应的单元格设置为您想要的任何数据;这就是它的全部。谢谢@Rafael-getCell和setValue是我已经拥有的,它可以工作,但是我如何从JSON中获取一个特定对象并将其设置为单元格的值呢。。。JSON存储在req中,对吗?我还尝试了
var dataAll=JSON.parse(req.getContentText());var data=dataAll.timestamp;sheets.getRangeByName('ticket_id').getCell(nR,1).setValue(data);}它应该在服务器响应中,而不是在您的请求中。如果协商
application/json
,js引擎将为您调用
json.parse()
,您将得到一个常规对象,可以像任何其他js对象一样对其进行索引。如果
doPost()
函数正在处理服务器响应,则不应调用参数
req
;它应该被称为
response
,因为这就是服务器对请求的响应。太棒了!但在我的例子中,我没有
,我使用JIRA webhooks在我使用JIRA时自动将JSON发送到Google脚本。您的代码示例如何处理这个问题?我真的很感激你的想法:)谢谢@Rafael!这对我有用
var json=response.postData.contents;var data=JSON.parse(JSON)
var json = response.postData.contents;
var data = JSON.parse(json);