首页 > 解决方案 > 如何将我的输出格式化为彼此重叠而不是并排?

问题描述

我在 Apps Scripts 中使用 Googles NLP 并且数据正在通过。但是,我的输出是水平显示的,而不是相互重叠的。可能是一个简单的更改,但我无法弄清楚。在我分享的屏幕截图中,我希望数字 0.3 低于指标 2.10(黄色)。任何意见将是有益的。

在此处输入图像描述

function SentimentAnalysis(text)
{
  if (text == undefined || text == null || text == "")
  {
    throw "No text was specified for performing sentiment analysis."
  }
  
  var URL_PREFIX = "https://language.googleapis.com/v1/documents:analyzeSentiment?fields=documentSentiment&key=";
  
  // retrieve api key;
  var apiKey = PropertiesService.getScriptProperties().getProperty("GOOGLE_CLOUD_API_KEY");
  if (apiKey == null || apiKey == "REPLACEME" || apiKey == "") {
    PropertiesService.getScriptProperties().setProperty("GOOGLE_CLOUD_API_KEY", "REPLACEME");
    throw "Specify your GOOGLE_CLOUD_API_KEY via User Properties (File->Project Properties, Script Properties)";
  }
  
  var url = URL_PREFIX + apiKey;
  
  // define the request
  var data = {
    "document": {
      "content": text,
      "type": "PLAIN_TEXT"
    },
    "encodingType": "UTF8"
  };
  
  var options = {
    "method" : "POST",
    "contentType" : "application/json",
    "payload" : JSON.stringify(data)
  };

  // make the request
  var response = UrlFetchApp.fetch(url, options);
  
  // get the response
  if (response.getResponseCode() != 200) {
    throw "Unexpected response code from Google.";
  }
  
  var responseText = response.getContentText();

  if (responseText == null || responseText == "") {
    throw "Empty response from Facebook.";
  }

  // parse the response
  var magnitude = 0, score = 0;
  try
  {
    var sentimentResponse = JSON.parse(responseText, false);
    magnitude   = parseFloat(sentimentResponse.documentSentiment.magnitude);
    score    = parseFloat(sentimentResponse.documentSentiment.score);
  }
  catch (e)
  {
    throw "Unreadable response from Google: " + e;
  }
  
  return [[magnitude, score]];
}

标签: google-apps-scriptgoogle-sheetsnlp

解决方案


我相信你的目标如下。

  • 您想将magnitude和的值放在score垂直方向。
  • 您正在使用的功能SentimentAnalysis作为自定义功能。
  • magnitude并且score是您期望的正确值。

在这种情况下,如何进行以下修改?

从:

return [[magnitude, score]];

至:

return [magnitude, score];

或者

return [[magnitude], [score]];

推荐阅读