Last active
October 22, 2015 12:47
-
-
Save MauricioMoraes/a4d05dc4fe0fb4d2e6b0 to your computer and use it in GitHub Desktop.
Get column numbers from column names - Google apps scripts for spreadsheets
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
// Pass the column headers range and names, and it will return an object with the relation between column names and column numbers | |
function getColumnNumbersForNames(sheetName, columnHeadersRangeA1Notation, columnNames){ | |
var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = thisSpreadsheet.getSheetByName(sheetName); | |
var columnHeadersRange = sheet.getRange(columnHeadersRangeA1Notation); | |
var columnNamesOnSheet = flatten(columnHeadersRange.getValues()); | |
var columnNumbersForNames = {}; | |
for(var count = 0; count < columnNames.length; count++){ | |
var columnName = columnNames[count] | |
columnNumbersForNames[columnName] = columnNamesOnSheet.indexOf(columnName) + 1 | |
} | |
return columnNumbersForNames | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: