maxgotstuck
maxgotstuck

Reputation: 159

For Loop is not working as expected when checking array content in last column

I saved the information of this table in an 2D array using Google Apps Script. The whole array building process worked as aspected.

Area X Y Major Minor Angle SensorNo
1 25049 380.500 246.500 190.953 167.023 0 1
2 24248 393.500 247.000 192.976 159.986 0 2
3 18250 382.500 247.000 159.023 146.121 0 3

However, as I tried to build a for loop (as seen in the following code) that goes through every column of the first row of the array I did not get the expected output. I tried to tell the loop that when the array content equals "SensorNo" to save the respective n as a new variable and then break the loop. But whenever the array content I'm looking for is in the last column the variable remains undefined...

for(n=0;n<fileContentArray[0].length;n++){
  if(fileContentArray[0][n]=="SensorNo"){
    var sensorNoCol=n;
    break;
  }
}

I hope someone can help me. Best, Max

Upvotes: 0

Views: 54

Answers (1)

Cooper
Cooper

Reputation: 64082

function getHeaderColumns() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet Name");
  const hRow = 1;
  const hA = sh.getRange(hRow, 1, 1, sh.getLastColumn()).getValues()[0];
  let col = {};
  hA.forEach((h, i) => { col[h] = i + 1 });
  return col['SensorNo'];
}

Upvotes: 1

Related Questions