如何将 Google 工作表单元格中存在的 json 数据解析为数据形式。
我导出了一个 csv 文件,其中一列包含 json 格式的表单数据。我已将该文件复制到 Google Sheet 中,如何解析包含 json 格式数据的特定列的数据。
例如
其中一个单元格中的数据如下所示
"utf8":"\u2713",
"authenticity_token":"[object Object]",
"task_id":"21",
"number_input_ACB70B69-6E86-420E-AAB4-263A335795DE":"33",
"text_input_3ABA1E2D-9726-4B9C-9EAB-85E5D93BE47B":"volunteer at aNGO",
"text_input_0A5BE0C2-0745-40C8-831B-5FDDE49B5353":"B.architecture",
"text_area_3FE1D3FE-FC9E-4370-90F2-3D86C4DA2243":"A little knowledge on classical dance"
我只需要上述数据中的四个字段:
- 文本输入 - 33
- 文本输入 - 非政府组织志愿者
- 文本输入 - B.architecture
- 文字区-古典舞小知识
最佳答案
Apps 脚本使用 Javascript 版本,因此您可以 retrieve a key来自如下 JSON:
json.key
或 json["key"]
单元格中 json 的问题在于它缺少起始和结束括号 {
和 }
,因此为了使其正常工作,您需要添加它们。否则它不会被视为 JSON 对象,而是被视为常规字符串。它应该看起来像这样:
{"utf8":"\u2713",
"authenticity_token":"[object Object]",
"task_id":"21",
"number_input_ACB70B69-6E86-420E-AAB4-263A335795DE":"33",
"text_input_3ABA1E2D-9726-4B9C-9EAB-85E5D93BE47B":"volunteer at aNGO",
"text_input_0A5BE0C2-0745-40C8-831B-5FDDE49B5353":"B.architecture",
"text_area_3FE1D3FE-FC9E-4370-90F2-3D86C4DA2243":"A little knowledge on classical dance"}
这将获取键的值并将它们写入 4 个单独的列中:
function main(){
var sprsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = sprsheet.getSheetByName("Your sheet name");
var string = sheet.getRange("A1").getValue(); //The cell with the json
var json = JSON.parse(string); //Converts the string to JSON object
sheet.getRange("B1:E1").setValues([[json["number_input_ACB70B69-6E86-420E-AAB4-263A335795DE"], json["text_input_3ABA1E2D-9726-4B9C-9EAB-85E5D93BE47B"], json["text_input_0A5BE0C2-0745-40C8-831B-5FDDE49B5353"], json["text_area_3FE1D3FE-FC9E-4370-90F2-3D86C4DA2243"]]]);
}
引用文献:
关于json - 如何将 google 工作表单元格中存在的 json 数据解析为数据形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61571602/