Google Apps Script/URLFetchApp并使用返回的数据 [英] Google Apps Script/URLFetchApp and using returned data

查看:110
本文介绍了Google Apps Script/URLFetchApp并使用返回的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很陌生,所以请耐心等待-我目前在从Google Form答案生成的google工作表的后端上有一个可操作的google apps脚本.我实质上是在Google表单中设置一个票证表单,该表单会触发相应工作表中的数据通过api调用发送到我们的票务系统.它的效果很好,但我目前正在尝试对其进行优化.目标是获取我使用的json响应:

I am very new to this, so please bear with me-- I have currently have an operational google apps script on the backend of a google sheet that is generated from Google Form answers. I am essentially setting up a ticket form in google forms that will trigger the data in the corresponding sheet to be sent via api call to our ticketing system. It works great, but I am trying to optimize it currently. The goal is to take the json response I get using:

Logger.log(response.getContentText());

为我提供以下信息:

2020年8月9日,上午11:44:40信息{"_url":"https://testticketingsystem.com/REST/2.0/ticket/123456",类型":票证","id":"123456"}

并发送另一个API调用,以将数据发送到该新票证.

and send another API call to send data to that new ticket.

这是一个代码段:

var payload = { 
    "Subject":  String(su),
    "Content":  String(as),
    "Requestor": String(em),
    "Queue": String(qu),
    "CustomFields": {"CustomField1": String(vn), "CustomField2": String(vb), "CustomField3": 
     String(vg), "CustomField4": String(av), "CustomField5": String(ov), "CustomField6": 
     String(sd)}
     }

var options = {
      'method': 'post',
      "contentType" : "application/json",
      'payload': JSON.stringify(payload),
      'muteHttpExceptions': true
       }

var url = "https://testticketingsystem.com/REST/2.0/ticket?token=****************";

var response = UrlFetchApp.fetch(url,options);

Logger.log(response.getContentText());
  
  } catch (error) {
    Logger.log(error.toString());
  }
  }

创建票证后,如何在下一个api调用中编写将ID号用作变量的脚本?

After the ticket is created, how do I script the use of that ID number as a variable into my next api call?

谢谢!

推荐答案

UrlFetchApp.fetch 返回

UrlFetchApp.fetch returns a HTTPResponse, and if you expect JSON then you should be able to just use JSON.parse() to create an object from the text. (The JSON object is a standard JavaScript global object like Math; it is not Google Apps Script specific.)

如果一切顺利,您应该就可以使用

If all goes well, you should just be able to use

var response = UrlFetchApp.fetch(url,options);
var data = JSON.parse(response.getContentText());
var id = data.id;

,然后在下一个 fetch()中使用该 id .

and then use that id for your next fetch().

如果您的字面回答确实是

If your literal response is indeed

Aug 9, 2020, 11:44:40 AM Info    {"_url":"https://testticketingsystem.com/REST/2.0/ticket/123456","type":"ticket","id":"123456"}

{是无效的JSON之前,您都会遇到麻烦(请使用 linter linter 如果您需要检查一下自己).但是我假设这是控制台在您记录JSON时添加的,而不是在实际响应本身中添加的.

you will run into trouble as everything until the { is invalid JSON (use a linter if you need to check yourself). But I'm assuming that was added by the console when you logged JSON, and not in the actual response itself.

JSON.parse()会使用无效的JSON引发错误,因此您可以根据需要使用 try/catch .

JSON.parse() throws an error with invalid JSON, so you can use try/catch if needed.

您还可以在尝试 JSON.parse()之前检查标头.

You can also check the headers before you try to JSON.parse().

这是一个示例,可以检查和处理出现的问题.

Here's an example that checks and handles issues, should they arise.

var type = response.getHeaders()["Content-Type"];
var text = response.getContentText();
if (type === "application/json") {
  try {
    var data = JSON.parse(text);
  } catch (error) {
    return Logger.log("Invalid JSON: " + response.getContentText(text));
  }
} else {
  return Logger.log("expected JSON, but got response of type: " + type);
}
// if we get to this line, data is an object we can use

这篇关于Google Apps Script/URLFetchApp并使用返回的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆