user3713584
user3713584

Reputation: 9

Google script to extract the first word in a cell

[google-apps-script] Google script to extract the first word in a cell. I would like write a google script to extract the first word in a cell in a google spreadsheet.

For example if the cell contains, 'John White'. I would like to extract the word, 'John'.

Upvotes: 0

Views: 4385

Answers (1)

nwill001
nwill001

Reputation: 745

Sure can! If you know what cell you want to reference and what you want to split them by you can easily create a function to find the first word in a cell.

function getCellWord(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var range = sheet.getRange('A1:A1');
  var wordArray = range.getValue().split(" "); //Split by space

  Logger.log(wordArray[0]); // first word
  Logger.log(wordArray[1]); // second word
  Logger.log(wordArray[2]); // third word

}

Upvotes: 1

Related Questions