Skip to content

Instantly share code, notes, and snippets.

@phillypb
Created December 14, 2018 14:17
Show Gist options
  • Save phillypb/26765cab50df076a42369bc199fb6cf9 to your computer and use it in GitHub Desktop.
Save phillypb/26765cab50df076a42369bc199fb6cf9 to your computer and use it in GitHub Desktop.
function specificDataImproved() {
// 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();
Logger.log('Current row number is ' + currentRow);
// get last column of data
var lastCol = sheet.getLastColumn();
Logger.log('Last column number is ' + lastCol);
// GET 'Students' DATA ...
// get an array of all values on current row via getRange(startRow, startColumn, numRows, numColumns)
var theData = sheet.getRange(currentRow, 1, 1, lastCol).getValues()[0];
/* output is a 1-D array, defined by .getValues()[0] meaning there is only 1 set of square brackets
containing all the data - typically only suitable for single rows */
Logger.log(theData);
// create variables for the specific data required. Number in square bracket used to select relevant piece of data from array.
var name = theData[2] + ' ' + theData[1]; // concatenates first & last name
var postcode = theData[3];
var gender = theData[4];
var shoesize = theData[6];
// create 2-D array of above student data for later copying into destination 2-D array
var output = [ [name], [postcode], [gender], [shoesize] ];
Logger.log(output);
// SET 'justOne' VALUES ...
// get destination sheet
var destSheet = ss.getSheetByName('justOne');
// get justOne destination cells as 2-D array via getRange(startRow, startColumn, numRows, numColumns) and setValues from above 'output' 2-D array
var destRange = destSheet.getRange(3, 2, 4, 1).setValues(output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment