我想进行多次调用以获取JSONP并将其值插入到for循环内的数组中。但是,目前我的结果是一个填充undefined
值的数组。我怎样才能正常工作?
代码:
function fetchColumnRange(sheetId,colStart,colEnd,row) {
var colStartNum = isNumber(colStart) ? colStart : excelColumnNumber(colStart);
var colEndNum = isNumber(colEnd) ? colEnd : excelColumnNumber(colEnd);
var values = [], value;
for (i = colStartNum; i <= colEndNum; i++) {
value = fetchCellValue(sheetId,i,row); // IT HAS TO WAIT, BUT HOW???
values.push(value);
}
return values;
}
function fetchCellValue(sheetId,col,row) {
var script = document.createElement('script');
var colNum = isNumber(col) ? col : excelColumnNumber(col); // Column input can be either a number or a letter
script.src = 'https://spreadsheets.google.com/feeds/cells/0AtMEoZDi5-pedElCS1lrVnp0Yk1vbFdPaUlOc3F3a2c/' + sheetId + '/public/values/R' + row + 'C' + colNum + '?alt=json-in-script&callback=insertCellValue';
document.body.appendChild(script);
}
function insertCellValue(data) {
var value = data.entry['gs$cell']['$t'];
return value;
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
console.log(fetchCellValue(1,'A',2));
console.log(fetchCellValue(1,'B',2));
console.log(fetchColumnRange(1,'A','B',2));
输出:
["Test 1"]
["Lorem ipsum"]
[undefined, undefined]