Pages

Thursday, 20 December 2018

Get column by name

The following Google Apps Script code gets the value of a cell based on a specified column name as the current row ('forename' in this instance). This is different to the typical method where a script relies on the specific column being defined by which number it is, which is prone to failing if a user then adds/removes a column within the data sheet.

In this example the function 'getColByName' looks through the headings of each column, uses 'indexOf' to find the position of 'forename' (in this example). It then returns the position (with a '+1' to account for the array starting at zero). Student data is then acquired via 'getRange' - by using the 'activeCell' (for the row number) and the position of the 'forename' column (as the starting column).

Get Column by Name.xlsx

No comments:

Post a comment