Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将google工作表数据转换为json_Javascript_Json_Google Apps Script_Google Sheets_Data Conversion - Fatal编程技术网

Javascript 如何将google工作表数据转换为json

Javascript 如何将google工作表数据转换为json,javascript,json,google-apps-script,google-sheets,data-conversion,Javascript,Json,Google Apps Script,Google Sheets,Data Conversion,我目前正在尝试使用一个脚本,以便将google工作表数据转换为json,可以包含相邻和嵌套的对象。目前我拥有的是一个脚本,它可以将工作表数据转换为json,允许嵌套对象,但它不允许结束一个对象并启动一个新对象,因此不能有任何相邻对象,而是有一个父对象,其中包含子对象,这不是我想要的。我希望我只是在当前脚本中遗漏了一些东西,以便能够结束和启动新对象,所以我将在下面添加脚本,感谢您对这个问题的任何贡献 function formJSON() { var ss = SpreadsheetApp.g

我目前正在尝试使用一个脚本,以便将google工作表数据转换为json,可以包含相邻和嵌套的对象。目前我拥有的是一个脚本,它可以将工作表数据转换为json,允许嵌套对象,但它不允许结束一个对象并启动一个新对象,因此不能有任何相邻对象,而是有一个父对象,其中包含子对象,这不是我想要的。我希望我只是在当前脚本中遗漏了一些东西,以便能够结束和启动新对象,所以我将在下面添加脚本,感谢您对这个问题的任何贡献

function formJSON() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  var data = sheet.getDataRange().getValues();
  var currentObject = {};
  var output = currentObject;                    
  for (var i = 0; i < data.length; i++) {
    if (data[i][1]) {
      currentObject[data[i][0]] = data[i][1];
    }
    else {
      var newObject = {};
      currentObject[data[i][0]] = newObject;
      currentObject = newObject;
    }
  }
  Logger.log(JSON.stringify(output));
}
所需结果:

{
  "": {
    "asset": {
      "button": {
        "viewPDF": "View PDF",
        "viewSurvey": "View Survey",
        "viewPPT": "View PPT",
        "viewLink": "View Link",
        "rejoinMeeting": "Rejoing Meeting",
        "labels": {
          "associatedWith": "Associated Content",
          "attendees": "Attendees in this session",
          "filesAndDocs": "Files and Documents",
          "location": "Location",
          "messages": {
            "errorRetrieving": "There was an error retrieving the session details",
            "noAttendees": "Nobody is watching this session currently",
            "browser": {
              "messages": {
                "notSupported": "Your browser is not supported",
                "update": "Please update"
              }
            }
          }
        }
      }
    }
  }
}
"asset": {
    "buttons": {
      "viewPDF": "View PDF",
      "viewSurvey": "View Web Page",
      "viewPPT": "View Presentation",
      "viewLink": "View Link",
      "rejoinMeeting": "Rejoin Meeting"
    },
    "labels": {
      "associatedWith": "Associated Content",
      "attendees": "Attendees in this Session",
      "filesAndDocs": "Files and Documents",
      "location": "Location",
      "notStarted": "This session hasn't started yet.",
      "playlist": "Session Playlist",
      "premiumSponsors": "Premium Sponsors",
    },
    "messages": {
      "errorRetrieving": "There was an error retrieving the session details.",
      "noAttendees": "Nobody is watching this session currently",
      "pointsForDocument": "viewing a document",
      "pointsForRatingAsset": "rating this asset",
      "pointsForVideo": "watching a video",
      "problemSaving": "There was a problem saving your rating. Please try again."
    }
  },
  "browser": {
    "messages": {
      "notSupported": "Your Browser Is Not Supported",
      "update": "Please download the most up-to date version of one of the following and try again"
    }
  },

请看以下内容:

var data=sheet.getDataRange().getValues();
var currentObject={};
var title='';
var newObject={};
对于(变量i=0;i
虽然这不是一个完整的解决方案,但我认为它应该为您指出正确的方向

  • 我们的想法是,您应该有一些变量(在本例中为
    title
    ),该变量在
    else
    语句中定义/覆盖,并且是在下一个
    if
    条件中为其指定嵌套对象的键
  • 再次输入
    else
    条件后,
    title
    将被下一个嵌套对象键覆盖

能否提供所需JSON对象的示例结构?是的,我会进行编辑。我现在已经更新了。我正在查看。谢谢!几天来一直在试图解决这个问题,所以有了这个,与我添加的图像相比,实际的谷歌工作表会有什么变化?如果你只保留资产(少一层嵌套),代码将按照你的需要工作。