thurzo101
thurzo101

Reputation: 125

Get a spreadsheets last row as an array to use fields data

I want to get the last row of spreadsheet to use the fields to add that data to a document and then to send and email (that I can do) etc. I'm guessing I need and array but I just can't see how! This is what I have:

function createNda() {

  // Get the last row
  var lastRowSelect = sheet.getLastRow();

  return lastRowSelect;

  // Get the Range
  var dataRange = sheet.getRange(lastRowSelect, 1, 1, 8);

  // Get the values of the last row
  var data = dataRange.getValues();

  // Present the data of the row

  var ndaData = data;

  // I want to be able to grab the fields like this:

  var Name = ndaData[2];
  var Title = ndaData[3];
  var Email = ndaData[4];
  var Company = ndaData[5];
  var Address = ndaData[6];

So I can add the data to the a document:

 var doc = DocumentApp.openById(newNda.getId());

 var doc = DocumentApp.openById(newNdaId);

 var body = doc.getBody();

 body.replaceText("{{ nda_name }}", ndaData[2]);
 body.replaceText("{{ nda_title }}", ndaData[3]);
 body.replaceText("{{ nda_company }}", ndaData[4]);
 body.replaceText("{{ nda_address }}", ndaData[5]);

I'm just not getting there.

Any help much appreciated.

Upvotes: 0

Views: 409

Answers (1)

Alan Wells
Alan Wells

Reputation: 31300

The getValues() method gets a two dimensional array. So, to get inner array values, you need two indexes. Or first get the inner array, which is the row, then use the row array to get the cell values.

var thisRow = ndaData[0];

var Name = thisRow[2];
var Title = thisRow[3];
var Email = thisRow[4];
var Company = thisRow[5];
var Address = thisRow[6];

Upvotes: 2

Related Questions