简体   繁体   English

Google 表格 API 只返回不必要的信息

[英]Google Sheets API only returning unecessary information

Here is the goal: To use Google Sheets data into Dialogflow as fulfillmentText.目标如下:将 Google 表格数据用作 Dialogflow 作为 fulfillmentText。 This is the current code (using Google Apps Script):这是当前代码(使用 Google Apps 脚本):

function doPost(e) {
  // This code uses the Sheets Advanced Service, but for most use cases
  // the built-in method SpreadsheetApp.getActiveSpreadsheet()
  //     .getRange(range).getValues(values) is more appropriate.
  var ranges = ['Sheet1!B1'];
  var result = Sheets.Spreadsheets.Values.batchGet('sheetId, {ranges: ranges});
  Logger.log(JSON.parse(result));
  //Transforms the read value from Sheets into JSON format
  return ContentService.createTextOutput(JSON.stringify(result))
            .setMimeType(ContentService.MimeType.JSON);
} 

The first problem is when reading from a spreadsheet, instead of getting only the requested value, this is the output:第一个问题是从电子表格读取时,不是只获取请求的值,而是 output:

 {valueRanges=[{majorDimension=ROWS, values=[[Content here...]], range=Sheet1!B1}], spreadsheetId=XXXYYYZZZ}

The information concerned to the spreadsheet is not necessary.与电子表格相关的信息不是必需的。 Only the range content is needed.只需要范围内容。 So far, its just not possible to clean out the spreadsheet part inside the outputted JSON. I've not found anything about this problem in the documentation.到目前为止,无法清除输出的 JSON 中的电子表格部分。我在文档中没有发现任何关于此问题的信息。

The second problem is: after the content is converted to JSON in return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON) the previous content is wrapped inside the values field: https://developers.google.com/apps-script/guides/content#serving_json_from_scripts第二个问题是:内容转换为 JSON 后return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON)之前的内容包含在values字段中: https://developers。 google.com/apps-script/guides/content#serving_json_from_scripts

{
  "valueRanges": [
    {
      "range": "Sheet1!B1",
      "majorDimension": "ROWS",
      "values": [
        [
          "Note that we link to certain 3rd party products via affiliate or sponsored links"
        ]
      ]
    }
  ],
  "spreadsheetId": "XXXYYYZZZ"
}

Summarizing, the first problem is reading just the content from Sheets API instead of: sheetId , range and majorDimension ... The second is to send the JSON value unwrapped from the values field.总而言之,第一个问题是仅读取 Sheets API 中的内容,而不是: sheetId 、 range 和majorDimension ...第二个问题是发送从values字段解包的 JSON 值。 This the the response format eligible in Dialogflow: https://cloud.google.com/dialogflow/docs/fulfillment-webhook#text_response这是 Dialogflow 中符合条件的响应格式: https://cloud.google.com/dialogflow/docs/fulfillment-webhook#text_response

Thanks in advance!!!提前致谢!!!

I believe your goal as follows.我相信你的目标如下。

  • You want to retrieve the content of "Note that we link to certain 3rd party products via affiliate or sponsored links" from the following object. And you want to return the content like {"values": content} .您想要从以下 object 中检索"Note that we link to certain 3rd party products via affiliate or sponsored links"的内容。您想要返回类似{"values": content}的内容。

     { "valueRanges": [ { "range": "Sheet1,B1": "majorDimension", "ROWS": "values", [ [ "Note that we link to certain 3rd party products via affiliate or sponsored links" ] ] } ]: "spreadsheetId": "XXXYYYZZZ" }

For this, how about this answer?为此,这个答案怎么样?

Modification points:修改点:

  • Sheets.Spreadsheets.Values.batchGet returns the parsed JSON object. So it is not required to use JSON.parse . Sheets.Spreadsheets.Values.batchGet返回解析后的 JSON object。因此不需要使用JSON.parse
  • In order to retrieve the values from the response value, you can use result.valueRanges[0].values .为了从响应值中检索值,您可以使用result.valueRanges[0].values And also, when the 1st element is retrieved, please use result.valueRanges[0].values[0][0] .而且,当检索到第一个元素时,请使用result.valueRanges[0].values[0][0]

Modified script:修改脚本:

When your script is modified, please modify as follows.当您的脚本被修改时,请修改如下。

From: 从:
 Logger.log(JSON.parse(result)); //Transforms the read value from Sheets into JSON format return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON);
To: 到:
 var content = result.valueRanges[0].values[0][0]; var res = {values: content}; return ContentService.createTextOutput(JSON.stringify(res)).setMimeType(ContentService.MimeType.JSON);
  • If you want to retrieve "values", please use result.valueRanges[0].values instead of result.valueRanges[0].values[0][0] .如果您想检索“值”,请使用result.valueRanges[0].values而不是result.valueRanges[0].values[0][0]

Note:笔记:

  • When you modified the script of Web Apps, please redeploy the Web Apps as new version.当您修改Web Apps的脚本时,请重新部署Web Apps为新版本。 By this, the latest script is reflected to the Web Apps.由此,最新的脚本反映到Web Apps。 Please be careful this.请注意这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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