0
votes

I have a very basic question – I am trying to populate a first empty row in Google Sheets with a hard-coded array (I will then work on populating it with data pulled from another sheet).

I keep getting the "Incorrect range width, was 14 but should be 1."

I am trying to do it using the following code:

function populateSaleData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var saleData = new Array()
  saleData.push(["43376","John","Product","30","Dollars","30","yes","5","card","712.5","Adam","[email protected]","US","XYZ123456"])

  var destSheet = ss.getSheetByName('Sales');
  var lastFilledRowInColumnA = getLastPopulatedRow(destSheet.getRange('A:A').getValues());
  var destRange = destSheet.getRange(lastFilledRowInColumnA+1,1);
  destRange.setValues(saleData);
};

function getLastPopulatedRow(data) {
  for (var i=data.length-1;i>=0;i--)
    for (var j=0;j<data[0].length;j++)
      if (data[i][j]) return i+1;
  return 0;
};

I would be very grateful for any help.

1
I think that the reason of the issue is at destSheet.getRange(lastFilledRowInColumnA+1,1). In this case, each element of 2 dimensional array is required to be "1". So how about modifying to destSheet.getRange(lastFilledRowInColumnA+1,1,saleData.length,saleData[0].length)? The reference is here.Tanaike
This worked, thank you! (and thanks for directing me to the reference)thaddeus
Thank you for replying. I'm glad your issue was solved. Can you post the answer and accept it? I think that it will be useful for users who have the same issue.Tanaike

1 Answers

0
votes

Tanaike's comment resolved my issue:

I think that the reason of the issue is at destSheet.getRange(lastFilledRowInColumnA + 1, 1). In this case, each element of 2-dimensional array is required to be "1". So how about modifying to destSheet.getRange(lastFilledRowInColumnA + 1, 1, saleData.length, saleData[0].length)? The method reference is here.