1

我正在尝试从 Google 工作表中执行一个循环,从而用 a0、a1、a2 等从它中出来。我可以手动设置变量,但无法正确循环。

google.load('visualization', '1', {
    packages: ['gauge']
});
google.setOnLoadCallback(queryValue);

function queryValue() {
    var query = new google.visualization.Query('https://spreadsheets.google.com/spreadsheet/tq?range=B22:B37&key=0AhCv9Xu_eRnSdFNhSzNQUFd3b1ZfRHgtQURINFpzeGc&gid=7');
    query.send(function (response) {
        if (response.isError()) {
            alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
            return;
        }
        var data = response.getDataTable();

        //date label loop
        for (var i = 0; i <= 15; i++) {
            document.getElementById('b' + (i + 22)).innerHTML = data.getValue(i, 0);
            console.log("i =" + b22)
        }

    });
    queryValue1()
}

function queryValue1() {
    var query = new google.visualization.Query('https://spreadsheets.google.com/spreadsheet/tq?range=A22:A37&key=0AhCv9Xu_eRnSdFNhSzNQUFd3b1ZfRHgtQURINFpzeGc&gid=7');
    query.send(function (response) {
        if (response.isError()) {
            alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
            return;
        }
        var data1 = response.getDataTable();
        var a1 = data1.getValue(0, 0);
        var a2 = data1.getValue(1, 0);
        var a3 = data1.getValue(2, 0);
        var a4 = data1.getValue(3, 0);
        var a5 = data1.getValue(4, 0);
        var a6 = data1.getValue(5, 0);
        var a7 = data1.getValue(6, 0);
        var a8 = data1.getValue(7, 0);
        var a9 = data1.getValue(8, 0);
        var a10 = data1.getValue(9, 0);
        var a11 = data1.getValue(10, 0);
        var a12 = data1.getValue(11, 0);
        var a13 = data1.getValue(12, 0);
        var a14 = data1.getValue(13, 0);
        var a15 = data1.getValue(14, 0);
        var a16 = data1.getValue(15, 0);

        queryValue3(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16);
    });


}

function queryValue3(met1, met2, met3, met4, met5, met6, met7, met8, met9, met10, met11, met12, met13, met14, met15, met16) {
    var query3 = new google.visualization.Query('https://spreadsheets.google.com/spreadsheet/tq?range=B2:B17&key=0AhCv9Xu_eRnSdFNhSzNQUFd3b1ZfRHgtQURINFpzeGc&gid=10');
    query3.send(function (response) {
        if (response.isError()) {
            alert('Error in query3: ' + response.getMessage() + ' ' + response.getDetailedMessage());
            return;
        }

        var data3 = response.getDataTable();

        var m1 = data3.getValue(0, 0);
        var m2 = data3.getValue(1, 0);
        var m3 = data3.getValue(2, 0);
        var m4 = data3.getValue(3, 0);
        var m5 = data3.getValue(4, 0);
        var m6 = data3.getValue(5, 0);
        var m7 = data3.getValue(6, 0);
        var m8 = data3.getValue(7, 0);
        var m9 = data3.getValue(8, 0);
        var m10 = data3.getValue(9, 0);
        var m11 = data3.getValue(10, 0);
        var m12 = data3.getValue(11, 0);
        var m13 = data3.getValue(12, 0);
        var m14 = data3.getValue(13, 0);
        var m15 = data3.getValue(14, 0);
        var m16 = data3.getValue(15, 0);

        var red22 = "<span style='color:#ff0000'> ";
        var yellow22 = "<span style='color:#FF9900'> ";
        var green22 = "<span style='color:#009900'> ";

此代码查询 Google 表格并在页面上绘制 16 个仪表,动态更改记录数据的月份,仪表上的标签更改颜色以匹配仪表上的颜色,红色、黄色或绿色。 这是页面

如您所见,我还有一些改进的余地。我已经设法让这一切正常工作,但我缩短了几个循环。

4

1 回答 1

0

我认为返回数组而不是变量会更有效。

像这样的东西:

var a = [];
for (var z = 0; z <= 15; z++) {
  a[z] = data1.getValue(z, 0);
}

要获取 a0、a1、a2 等的值,您只需使用 a[0]、a[1]、a[2] 等。

于 2013-03-07T04:21:37.417 回答