使用独立的Google Apps脚本和Google电子表格。我有这个脚本作为JSON返回一个空数组和三个对象,但我希望只得到三个对象。它是一个搜索,当搜索一个zipcode时,脚本将返回任何匹配。问题是,它成功返回匹配,但它也为每个不匹配的行返回一个null,按行google sheet上显示的顺序。要使其正常运行,应运行function testDoGetWithZipcode()
。
我不知道我是否应该获得这些空值,如果它们重要,或者我如何解决它。到目前为止,我似乎没有学到任何关于JSON的知识,但在此之前,我甚至在此之前做了一个半小时的关于Javascript和JSON的Lynda.com课程并阅读了JSON.org网站并阅读了有关JSON的Mozilla文档。我已经调整了所有函数中的变量,因为起初我认为它在function formatOrganization()
中,但现在我完全被难倒了。
s = SpreadsheetApp.openById("1280aUAvFoUDP2rtpCFS2JYR7TuQNYcd5gm8QudukiGc");
var sheet = s.getSheetByName("RAP - Data");
var data = sheet.getDataRange().getValues();
var headings = data[0];
function zipcodeQuery(zipcode) {
zipcodeArray = [];
for (var i = 1; i < data.length; i++){
if (zipcode === data[i][4].toString()){
zipcodeArray.push(data[i]);
}
}
return zipcodeArray
}
function formatOrganization(rowData){
var organization = {}
for (var i = 0; i < headings.length; i++){
Logger.log('Headings: ' + headings[i]);
organization[headings[i].toString()] = rowData[i];
}
return organization
}
function executeZipcodeQuery(request) {
zipcodes = request.parameters.zipcode;
// The object to be returned as JSON
response = {
organizations : []
}
// Fill the organzations dictionary with requested organizations
for (var i = 0; i < zipcodes.length; i++) {
sheetData = zipcodeQuery(zipcodes[i]);
if(sheetData !== undefined) {
for (var orgIndex = 0; orgIndex < sheetData.length; orgIndex++) {
var org = formatOrganization(sheetData[orgIndex]);
if(org !== undefined) {
Logger.log('Org object: ' + org);
if(typeof org === 'object') {
//FIXME
var orgId = parseInt(org.Id);
Logger.log('Org Id: ' + orgId);
response.organizations[orgId] = org
//response.organizations.push({orgId : org});
}
}
}
}
}
if (response.organizations.length > 0)
{
return ContentService.createTextOutput(JSON.stringify(response.organizations));
}
else
{
return ContentService.createTextOutput('Invalid Request. zipcode(s) not found.');
}
}
function testDoGetWithZipcode() {
var testRequest = {"parameter":{"zipcode":"19132"},"contextPath":"","contentLength":-1,"queryString":"zipcode=19132","parameters":{"zipcode":["19132"]}};
var textResult = doGet(testRequest);
textResult.setMimeType(ContentService.MimeType.JSON);
Logger.log('Mime Type: ' + textResult.getMimeType());
Logger.log('Result content: ' + textResult.getContent());
}
我得到的回报是这样的(删节,因为电子表格中有超过180行,并且它们都在null或对象的返回中表示):
[
null,
....
null,
{
"Id":61,
"Category":"Day / Drop in Centers",
"Organization Name":"Philadelphia Recovery Community Center (PRCC)",
"Address":"1701 W Lehigh Ave, Philadelphia, PA 19132",
"Zip Code":19132,
"Days":"Mon, Tues, Thurs, Fri: 12-8pm, Wed: 9-5pm, Sat: 9-1pm",
"Time: Open":"",
"Time: Close":"",
"People Served":"Women, Men, Families",
"Description":"Case management, outpatient treatment, youth programs, training programs",
"Phone Number":"215-223-7700"
},
....
null,
{
"Id":81,
"Category":"Emergency Shelter",
"Organization Name":"Station House",
"Address":"2601 N Broad St, Philadelphia, PA 19132",
"Zip Code":19132,
"Days":"",
"Time: Open":"",
"Time: Close":"",
"People Served":"Men",
"Description":"After hours reception for single men\n 2601 N. Broad Street\n After 4 pm",
"Phone Number":"215-225-9230"
},
null,
...
]
答案 0 :(得分:1)
您的原始对象是:
response = {
organizations : []
}
organizations
的键/值对的值是一个数组。但是你使用的符号就好像organizations
是一个对象。
response.organizations[orgId] = org
您可以使用以下命令将值推送到数组中:
response.organizations.push(org);
我可能会尝试这样的事情:
var tempObject = {}; //Reset every time
tempObject[orgId] = org;
response.organizations.push(tempObject);