Load or Read a JSON from local in Google AppScript

自闭症网瘾萝莉.ら 提交于 2020-01-14 04:17:08

问题


I am looking to read a json file, namely config.json from a AppScript to generate a Google Form from the meta data in the config.json. I am not sure how to read the JSON file - could someone help me with it please? Some example code would be greatly appreciated.. Thanks


回答1:


Assuming that your file is in Google Drive, you can use the DriveApp service to read the file and parse it.

function getFileContent() {
  var fileName = "config.json.txt";
  var files = DriveApp.getFilesByName(fileName);
  if (files.hasNext()) {
    var file = files.next();
    var content = file.getBlob().getDataAsString();
    var json = JSON.parse(content);
    Logger.log(json);
  }
}

Make sure that Google Drive has not converted the text file into a native format else the script won't be able to parse it.



来源:https://stackoverflow.com/questions/53359704/load-or-read-a-json-from-local-in-google-appscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!