Google脚本数组值仅为Null

时间:2015-12-28 08:14:06

标签: google-apps-script

function getID(){
     var ss = SpreadsheetApp.openById("SheetID");   var sheet = ss.getSheetByName("DVAL1");   //var rec =
     sheet.getRange("B"+1).getValue();

       var currentRowAT = 1;
         var currentCellValueAT = "start";
         while (currentCellValueAT != ""){
           if (currentCellValueAT = sheet.getRange("B" + currentRowAT).getValue() != ""){
           var actualvalue = sheet.getRange("B" + currentRowAT).getValue();
             var rowValues = [];
             Logger.log(actualvalue);
             rowValues[currentRowAT]=actualvalue;
           currentRowAT = currentRowAT +1;
           }
         }
         Logger.log(rowValues); 
      }

结果:

[15-12-28 00:06:15:693 PST] 1.0 [15-12-28 00:06:15:799 PST] 2.0
 [15-12-28 00:06:15:896 PST] 3.0 [15-12-28 00:06:16:014 PST] 4.0
 [15-12-28 00:06:16:118 PST] 5.0 [15-12-28 00:06:16:239 PST] 6.0
 [15-12-28 00:06:16:340 PST] 7.0 [15-12-28 00:06:16:442 PST] 8.0
 [15-12-28 00:06:16:534 PST] 9.0 [15-12-28 00:06:16:631 PST] 10.0
 [15-12-28 00:06:16:727 PST] 11.0 [15-12-28 00:06:16:824 PST] 12.0
 [15-12-28 00:06:16:919 PST] 13.0 [15-12-28 00:06:17:014 PST] 14.0
 [15-12-28 00:06:17:107 PST] 15.0 [15-12-28 00:06:17:154 PST] [null,
 null, null, null, null, null, null, null, null, null, null, null,
 null, null, null, 15.0]

1 个答案:

答案 0 :(得分:0)

您正在使用语法将元素添加到JSON对象而不是数组方法。这一行:

rowValues[currentRowAT]=actualvalue;

更改为:

rowValues.push(actualvalue);

并移动这一行:

var rowValues = [];
while循环的

out 。它在每个循环中将数组重置为空数组。