gpt4 book ai didi

javascript - 从 Google 表格创建 JSON 对象

转载 作者:搜寻专家 更新时间:2023-11-01 04:59:30 25 4
gpt4 key购买 nike

我正在尝试学习 AppScript,并以 Google 表格为例。我想使用工作表中填充的一些数据创建一个简单的 JSON 对象。

表格示例

Name    ID   Price   QtyABC     123  100      1DEF     342   56      2HIJ     233   90      3IJK     213   68      5

I want the JSON out to be something like

[
{
"Name": "ABC",
"ID": "123",
"Price": 100,
"Qty": 1
},
{
"Name": "DEF",
"ID": "342",
"Price": 56,
"Qty": 2
},
{
"Name": "HIJ",
"ID": "233",
"Price": 90,
"Qty": 3
},
{
"Name": "IJK",
"ID": "213",
"Price": 68,
"Qty": 5
}
]

我开始关注这个 Youtube 教程:https://www.youtube.com/watch?v=TQzPIVJf6-w .但是,该视频谈到将每个列标题创建为对象。我希望列名是 key,行值是 value

这是我当前的 AppScript 代码

function doGet() {
var result={};
var rewards = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1')
.getDataRange()
.getValues();

result.rewardObj = makeObject(rewards);
//Logger.log(result.rewardObj);
return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON)


}

function makeObject(multiArray)
{
var obj = {};
var colNames = multiArray.shift();
var rowNames = multiArray.slice(0,1);
var rowCount = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getLastRow();
var colCount = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getLastColumn();

for(var j=0;j<4;j++)
{
for(var i=0;i<4;i++)
{
//obj[colNames] = rowNames.map(function(item){return item[i];});
obj[colNames[j][i]] = multiArray[j][i];
}
}
Logger.log(rowCount)
Logger.log(colCount)
Logger.log(multiArray[57][12]);
return obj;
}

输出是单个对象

{"rewardObj":{"Name":"ABC","ID":"123","Price":"100","Qty":"1"}}

PS:我不是程序员,我只是在学习一些 hackkey 方式的脚本。为不了解基础知识而道歉:)

最佳答案

请尝试:

function getJsonArrayFromData(data)
{

var obj = {};
var result = [];
var headers = data[0];
var cols = headers.length;
var row = [];

for (var i = 1, l = data.length; i < l; i++)
{
// get a row to fill the object
row = data[i];
// clear object
obj = {};
for (var col = 0; col < cols; col++)
{
// fill object with new values
obj[headers[col]] = row[col];
}
// add object in a final result
result.push(obj);
}

return result;

}

测试函数:

function test_getJsonArrayFromData()
{
var data =
[
['Planet', 'Mainland', 'Country', 'City'],
['Earth', 'Europe', 'Britain', 'London'],
['Earth', 'Europe', 'Britain', 'Manchester'],
['Earth', 'Europe', 'Britain', 'Liverpool'],
['Earth', 'Europe', 'France', 'Paris'],
['Earth', 'Europe', 'France', 'Lion']
];

Logger.log(getJsonArrayFromData(data));

// => [{Mainland=Europe, Country=Britain, Planet=Earth, City=London}, {Mainland=Europe, Country=Britain, Planet=Earth, City=Manchester}, {Mainland=Europe, Country=Britain, Planet=Earth, City=Liverpool}, {Mainland=Europe, Country=France, Planet=Earth, City=Paris}, {Mainland=Europe, Country=France, Planet=Earth, City=Lion}]

}

关于javascript - 从 Google 表格创建 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47555347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com