json - 如何通过谷歌脚本(gs)将整个谷歌电子表格作为文本文件读取(按行)
问题描述
我有一个带有 json 格式文本的谷歌表,它位于随机单元格中,需要通过谷歌应用程序脚本阅读。有一些方法可以按列或作为实际上插入不必要的分号的数组来读取它。请告知如何类似于 UrlFetchApp.fetch 和 JSON.parse 但直接从电子表格中逐行读取,跳过空单元格?例如 A1 B1 C1 ... A2 B2 C2 ... 等(无空格)
解决方案
function readJson(){
const sh = SpreadsheetApp.getActive().getSheetByName("Sheet1")
const allValues = sh.getDataRange().getValues()
const arrJson = []
allValues.forEach(row=>{
row.filter(cell=>cell).forEach(cell=>{
try{
const json = JSON.parse(cell)
arrJson.push(json)
} catch(ignore){
// JSON parse failed, probably not JSON
}
})
})
// arrJson contains all json data from cells in Sheet1
}
推荐阅读
- c# - 使用 roslyn,如何获得一种“var”?
- python - def 中的语法无效
- gitlab - Gitlab 测试突然出现 Symfony 项目错误
- javascript - 如何验证搜索结果是否仅包含直达航班的结果
- c# - 如何在 ISession.QueryOver 中添加“或”语句?
- android - 是否有示例代码或插件可以帮助了解如何播放音频文件 listview builder?
- html - 如何创建这样的 CSS 网格布局?
- javascript - 如何从 DOM 和数组中删除项目
- php - 更新 MYSQL 中的特定行
- python - SQLalchemy:动态创建具有不同列集的表