Google表格JSON导出,正确格式化JSON以进行推送

时间:2015-06-24 14:24:43

标签: javascript json loops google-sheets geckoboard

我正在尝试将Google表格中的数据导出到BI信息中心(Geckoboards)。对于我使用的特定小部件,需要JSON格式如下:

   {
   "points": {
     "point": [
      {
        "city": {
          "city_name": "London",
          "country_code": "GB"
        },
        "size": 10
      },
      {
        "city": {
          "city_name": "San Francisco",
          "country_code": "US",
          "region_code": "CA"
但是,我现在不需要担心尺寸属性。目前,我已经为此目的修改了一个示例中的Google脚本,但它不起作用,我认为我没有正确设置循环以正确格式生成JSON:

var apiKey = "#########################omitted";

function geckoDocsJSONmap()
{

var jsonSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("JSON Demo Data");

var widgetKey;

  widgetKey = ##########omitted";
  var aCity = jsonSheet.getRange("A2:A5").getValues();
  var aCountry = jsonSheet.getRange("B2:B5").getValues();
  var aRegion = jsonSheet.getRange("C2:C5").getValues();
  gdMap_(widgetKey, aCity, aCountry, aRegion);

function pushWidgetUpdate_(widgetKey, payload, options)
  {  
  var payload = JSON.stringify(payload);
  var options =
      {
        "method" : "post",
        "payload" : payload
      };  
  var jsonResponse = UrlFetchApp.fetch("https://push.geckoboard.com/v1/send/" + widgetKey, options);
  Logger.log(JSON.parse(jsonResponse.getContentText()));
  return(true);
}

function gdMap_(widgetKey, aCity, aCountry, aRegion)
{
  for (var i = 0; i < aCity[0].length; i++)
     {
  var payload = 
      {
        api_key:apiKey,
        data: 
          {
            points: 
            {
              point : [
                {
                  city: {
                    city_name: aCity,
                    country_code: aCountry,
                    region_code: aRegion }
                  }
                  ]
                }

          }
            }
          }


  return(pushWidgetUpdate_(widgetKey, payload));
}

}

我似乎无法提供我正在使用的Google表格的图片,因为我没有足够的声誉,但我已在此处发布:

Google Sheet

非常感谢任何帮助。我的JavaScript能力仍然非常新手,Google Scripts更是如此。

0 个答案:

没有答案