0

In Google Sheets Script editor, I have the code:

function recordHistory() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Stock History");
  var source = sheet.getRange("B2:C2");
  var values = source.getValues();
  values[0][2] = values[0][1];
  values[0][1] = values[0][0];
  values[0][0] = new Date();
  sheet.appendRow(values[0]);
};

What is the correct way to shunt values[0][0] & values[0][1] into values[0][1] & values[0][2] so I don't have to use:

  values[0][2] = values[0][1];
  values[0][1] = values[0][0];
Rubén
  • 34,714
  • 9
  • 70
  • 166
Steve
  • 2,066
  • 13
  • 60
  • 115

2 Answers2

0

Google Apps Script is based on JavaScript so you could use the JavaScript array methods to achieve your goal, like unshift.

References

How can I add new array elements at the beginning of an array in JavaScript?

Community
  • 1
  • 1
Rubén
  • 34,714
  • 9
  • 70
  • 166
0

Try this:

values.unshift(new Date());

It will add elements to the beginning of an array.

Hope this will help you.

Thanks.

YNK
  • 129
  • 2
  • 15