Last active
August 29, 2015 14:25
-
-
Save MauricioMoraes/2eadec206c42a179459f to your computer and use it in GitHub Desktop.
Google Apps Script for Spreadsheets - Gets the column number from a given sheet name and column name. The function assumes the column names are on the first row. The resulting column number is zero-based (Column A -> 0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function columnNumberByName(sheetName, ColumnName) { | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spreadsheet.getSheetByName(sheetName) | |
var firstRowRange = sheet.getRange(1, 1, 1, sheet.getMaxColumns()); | |
var columnNames = firstRowRange.getValues()[0]; | |
for (var columnNumber in columnNames) { | |
if (columnNames[columnNumber] == ColumnName) { | |
return parseInt(columnNumber); | |
} | |
} | |
throw 'No column named: ' + ColumnName + ' was found.'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment