简体   繁体   中英

Returning null values in Google Apps Script

The function below is returning null values the second and third time it goes through the 'for loop' and I can't figure out why. The for loop containing 'i=start' is looping through the 'decisionPoints' variable and returning data based on the the value in the first column. The first few rows in 'decisionPoints' have '1' in the first column. The next few rows have '2' in the first column and the next few have '3'. When the loop searches for '2' and '3', it returns null values for each row before the row containing the first value it's looking for. So, when it searches for '2', I see three null values for the preceding rows containing '1'. When it searches for '3', I see six null values for the preceding rows containing '1' or '2'.

Can anyone explian why?

var loBuild = function(loNumber,category){
      var lo = [],
          spreadsheet = SpreadsheetApp.getActiveSpreadsheet(), 
          decisionPointsSheet = spreadsheet.getSheetByName("Decision Points"),   
          lastColumn = decisionPointsSheet.getLastColumn(),
          lastDecisionPoint = decisionPointsSheet.getLastRow(), 
          decisionPoints = decisionPointsSheet.getRange(1,1,lastDecisionPoint,lastColumn).getValues(),
          count = 0,
          loColumn = [];
      decisionPoints.shift(); 
      for(i in decisionPoints){
        loColumn.push(decisionPoints[i][0]);
        if(decisionPoints[i][0] === loNumber){
          count++;
        }
      }
    var start = loColumn.indexOf(loNumber);
    for(i = start; i < count+start; i++){
        lo[i] = [];
        var dp = decisionPoints[i][1];
        var dpLabel = decisionPoints[i][3];
        for(j = 0; j < lastColumn; j++){
          switch(j){
            case 0:
              lo[i][j] = dp;
              break;
            case 1:
              lo[i][j] = "=countifs('" + dpLabel + "'!F:F,\"" + category + "\")"
              break;
            case 2:
              lo[i][j] = "=countifs('" + dpLabel + "'!F:F,\"FCC\")"
              break;
          }
        }
      }
      return(lo);
    }
for(i = start; i < count+start; i++){
        lo[i] = [];

If start=3 , you begin populating your lo array from the 4th element (lo[3]) - thus lo[0], lo[1] and lo[2] will be automatically prepended into your array with null values, since arrays are 0-based.

It looks like you are trying to create a sheet row values array to replace/update existing sheet row? In that case it's probably better to make lo an array of objects containing row index reference and array of new values:

Important note: code below assumes that data in your sheet is sorted by first column (loNumber values). This assumption is based on your posted code sample, specifically how you set your count variable (you code would loop over wrong rows if sheet is not sorted by loNumber column).

lo = [];
...
var start = loColumn.indexOf(loNumber);
    for(i = start; i < count+start; i++){
        var objRow = {rowIndex: i, rowValues: []}; // row object
        var dp = decisionPoints[i][1];
        var dpLabel = decisionPoints[i][3];
        for(j = 0; j < lastColumn; j++){
          switch(j){
            case 0:
              objRow.rowValues[j] = dp;
              break;
            case 1:
              objRow.rowValues[j] = "=countifs('" + dpLabel + "'!F:F,\"" + category + "\")"
              break;
            case 2:
              objRow.rowValues[j] = "=countifs('" + dpLabel + "'!F:F,\"FCC\")"
              break;
            default:
              objRow.rowValues[j] = ""; // if j is > 2
          }
        }
        lo.push(objRow); // push row object into lo array
      }
      return(lo);

Eg assuming start=3 and count=2 , you will get this lo array:

[
  {rowIndex:3, rowValues:[row3_col0_value, row3_col1_formula, row3_col2_formula]}, 
  {rowIndex:4, rowValues:[row4_col0_value, row4_col1_formula, row4_col2_formula]}
]

You can then loop over lo array and setValues() in corresponding sheet row:

for ( var i=0; i<lo.length; i++ ) {
  var rowData = lo[i];
  sheet
    .getRange( rowData.rowIndex-1, 1, 1, rowData.rowValues.length )
    .setValues( [rowData.rowValues] );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM