0

Still new and just learning how to use arrays. I am getting the error "Cannot convert Array to Object[][]. (line 46, file "Submit to Record")

Line 46 is

  targetSheet.getRange(lastRow+1, 1, 1, arrayOfData.length).setValues(arrayOfData);

I had this error once before, but it was because of an array inside an array issue. Now I don't know what's wrong.

The entire code is

function submitButtonClick() {   

  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();

  Logger.log('sheet.getName(): ' + sheet.getName());


  if (sheet.getName() !== "SubmitReceipt") {return;};

  var targetSheet = ss.getSheetByName("ReceiptRecord");

  var arrayOfData = [];

  var week = sheet.getRange(6,9).getValue();
  var emplN = sheet.getRange(4,9).getValue();
  var purDate = sheet.getRange(9,9).getValue();
  var purFrom = sheet.getRange(11,9).getValue();
  var custC = sheet.getRange(14,9).getValue();
  var deptC = sheet.getRange(16,9).getValue();
  var lotC = sheet.getRange(18,9).getValue();
  var laborC = sheet.getRange(20,9).getValue();
  var itemC = sheet.getRange(22,9).getValue();
  var hyperL = sheet.getRange(28,9).getValue();
  var notes = sheet.getRange(44,8).getValue();

  arrayOfData[0] = week;
  arrayOfData[1] = emplN;
  arrayOfData[2] = purDate;
  arrayOfData[3] = purFrom;
  arrayOfData[4] = custC;
  arrayOfData[5] = deptC;
  arrayOfData[6] = lotC;
  arrayOfData[7] = laborC;
  arrayOfData[8] = itemC;
  arrayOfData[9] = hyperL;
  arrayOfData[10] = notes;

  Logger.log('arrayOfData '+ arrayOfData)

  var lastRow = targetSheet.getLastRow();

  Logger.log('lastRow: ' + lastRow);
  Logger.log('arraylength ' + arrayOfData.length);

  targetSheet.getRange(lastRow+1, 1, 1, arrayOfData.length).setValues(arrayOfData);

  sheet.getRange(6,9).clearContent();
  sheet.getRange(4,9).clearContent();
  sheet.getRange(9,9).clearContent();
  sheet.getRange(11,9).clearContent();
  sheet.getRange(14,9).clearContent();
  sheet.getRange(16,9).clearContent();
  sheet.getRange(18,9).clearContent();
  sheet.getRange(20,9).clearContent();
  sheet.getRange(22,9).clearContent();
  sheet.getRange(28,9).clearContent();
  sheet.getRange(44,8).clearContent();

}

I know this code is clunky and could be written more efficiently and condensed, but I am writing this way on purpose because I am new to JS and this is an easy way for me to keep my head on straight about what is happening in the code. I hope my sanity efforts are not the cause of my problem. Please help. :)

6
  • 1
    Could you show the logger.log result ? I guess you should simply write setValues([arrayOfData]) but I'm just guessing ;-) Commented Feb 12, 2016 at 20:36
  • You were correct! That's all it needed. Thanks so much for an observant set of eyes. Commented Feb 12, 2016 at 20:44
  • 1
    you're welcome:). Your array was a simple array, setValues() needs an array of arrays, even for a single row of data. Commented Feb 12, 2016 at 20:47
  • 2
    yes. btw, I allowed myself to edit your question a bit... I removed the javascript tag because this is strictly specific to Google Apps script and removed the "run snippet" gadget since in can obviously not be executed from witihn this site. I hope you won't mind. I also upvoted to compensate the -1 that came probably from the "pure javascript" forum ;-) Commented Feb 12, 2016 at 21:31
  • 1
    The 2D array corresponds to an array of arrays, each cell in a row is an element of the row array and each row is an element of the upper level array. A single cell would be shown like that :[[value]], that's to say one row array in a one column array. Commented Feb 12, 2016 at 21:35

1 Answer 1

1

Serge insas answered the question in the comments. He said:

I guess you should simply write

setValues([arrayOfData])

but I'm just guessing ;-)"

That did indeed fix the problem. Thanks, Serge insas!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.