在Google AppScript中从本地加载或读取JSON [英] Load or Read a JSON from local in Google AppScript

查看:95
本文介绍了在Google AppScript中从本地加载或读取JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从AppScript中读取一个json文件,即config.json,以便根据config.json中的元数据生成Google表单.我不确定如何读取JSON文件-有人可以帮我吗?一些示例代码将不胜感激..谢谢

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

推荐答案

假设您的文件位于Google云端硬盘中,则可以使用DriveApp服务读取文件并进行解析.

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);
  }
}

请确保Google云端硬盘未将文本文件转换为纯格式,否则脚本将无法对其进行解析.

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.

这篇关于在Google AppScript中从本地加载或读取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆