Skip to content

Instantly share code, notes, and snippets.

@phillypb
Created December 14, 2018 20:33
Show Gist options
  • Save phillypb/7010e014796da9fe8b725907ea107eb8 to your computer and use it in GitHub Desktop.
Save phillypb/7010e014796da9fe8b725907ea107eb8 to your computer and use it in GitHub Desktop.
function getColByName(name) {
// get column headers as an array to search through
var headers = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Students').getDataRange().getValues().shift();
// search array looking for specific text to return its position
var colindex = headers.indexOf(name);
return colindex+1;
}
function getColumnData() {
/* calls 'getColByName' function and searches for 'forename' to then return the relevant column index as a number
stored as variable */
var forename = getColByName('forename');
Logger.log(forename);
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get active sheet
var sheet = ss.getActiveSheet();
// get active cell
var activeCell = sheet.getActiveCell();
// find current row for active cell
var currentRow = activeCell.getRow();
/* get student data
get 'forename' by using previous 'getColByName' function (which specifies the startColumn value now) in the current
row data via getRange(startRow, startColumn, numRows, numColumns) */
var dataName = sheet.getRange(currentRow, forename, 1, 1).getValue();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment