wulfae
wulfae

Reputation: 63

script referencing two rows google

I'm using this script that I found online to record names and numbers with a timestamp in a history page of my google sheet for my records (my reference for where is at home, sorry!) and it works like a charm!

function recordHistory_names() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("History");
  var source = sheet.getRange("A1:IA1");
  var values = source.getValues();
  values[0][0] = new Date();
  sheet.appendRow(values[0]);

Only problem is, I have two lines in my 'history' document that I want to record, the names in row 1 and the amounts in row 2. I know nothing about writing scripts, and my attempts to find an answer on how to alter the formula to get 2 rows instead of only one have not really succeeded.

I tried just changing the 'getRange' to A1:IA2, hoping it would record that, but it only gave me the A2:IA2 range. I've got a second instance of this code set up to record A2:IA2 set to run with a trigger, but the two triggers run about a minute apart which is enough time for my data to have changed, since there will be multiple contributors to this document.

How do I trigger two instances into one instance at once? Or call the two lines I need in one code block?

I know these are probably super basic questions, I just have no idea where to start. Thanks for any help!

Upvotes: 1

Views: 57

Answers (1)

Alan Wells
Alan Wells

Reputation: 31300

Try this:

function recordHistory_names() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("History");

  var source = sheet.getRange("A1:IA2");
  var values = source.getValues();

  values[0][0] = new Date();
  sheet.appendRow(values[0]);
  sheet.appendRow(values[1]);
}

The getValues() method returns a two dimensional array, which is arrays inside of another array.

[ [inside array], [another inside array], [a third inside array] ]

Each of the inside arrays are the values of each of the cells in a row.

[ [row one], [row two], [row three] ]

If you want to get the array of values for row two, the syntax is:

arrayName[1]

The count starts at zero, so row two is index one.

Upvotes: 1

Related Questions