我尝试从Google Developers(https://www.youtube.com/watch?v=R5kaYpRJIqo @ 1:32)
实施代码但我似乎在属性列表之后得到了" Missing}。 (第19行,文件"代码")"错误(latlng:[values [i] [1],values [i] [2]],)
这是我的完整代码:
var SPREADSHEET_ID = 'XXXXXXXXXXXXXXXXXXX';
var SHEET_NAME = 'Data';
function doGet(request) {
var range = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(SHEET_NAME).getDataRange();
var values = range.getValues();
var cities = [];
for (var i = 1; i < values.length; i++)
{
var city = {
name: values[i][0]
latlng: [values[i][1], values[i][2]],
population: values[i][3]
};
cities.push(city);
}
var json = Utilities.jsonStringify(cities);
if (request && request.parameters) {
var call back = request.parameters.jsonp;
if (callback) {
json = callback + '(' + json + ')';
}
return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JSON);
}
如何让此代码生效?非常感谢您的回复!
答案 0 :(得分:0)
你错过了一个}
字符,我相信它应该在代码底部return
之前,它是if (request && request.parameters) {
行的右括号。< / p>
答案 1 :(得分:0)
语法无错代码:http://jsfiddle.net/HrvG7/
检查在小提琴上报告为错误的评论
var SPREADSHEET_ID = 'XXXXXXXXXXXXXXXXXXX';
var SHEET_NAME = 'Data';
function doGet(request) {
var range = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(SHEET_NAME).getDataRange();
var values = range.getValues();
var cities = [];
for (var i = 1; i < values.length; i++) {
var city = {
name: values[i][0], //comma was missing
latlng: [values[i][1], values[i][2]],
population: values[i][3]
};
cities.push(city);
}
var json = Utilities.jsonStringify(cities);
if (request && request.parameters) {
var callback = request.parameters.jsonp; //there was a space between call and back
if (callback) {
json = callback + '(' + json + ')';
}
}
return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JSON);
}