Here's an efficient solution that avoids getting unnecessary extra data:
function getColumnData(headerName) {
const sheet = SpreadsheetApp.getActiveSheet();
// Get column index of headerName (starts at 1)
const columnIndex = sheet.getRange('1:1').getValues()[0].indexOf(headerName) + 1;
// Return only headerName data
return sheet.getRange(2, columnIndex, sheet.getLastRow() - 1, 1).getValues();
}
If you want to flatten out the array that is returned, then change the return statement to this:
return sheet.getRange(2, columnIndex, sheet.getLastRow() - 1, 1).getValues().flat();
If your columns are uneven (e.g., column A is filled to row 10, column B is filled to row 7), then your shorter columns will have empty array items. To filter these out, change the return statement to this:
return sheet.getRange(2, columnIndex, sheet.getLastRow() - 1, 1).getValues().flat().filter(Boolean);