1

I'm new to Google Apps Script and I'm trying to make a script where I'll take a single string value and copy to multiple rows in a google sheet. I've taken an array to save the single value multiple times. But still I can't get it done. Every time I run the script, I get this error,

Cannot convert Array to Object[][]

Here are my codes,

function myFun() {
  var ss = SpreadsheetApp.openById(SHEET_ID);
  var sheet = ss.getSheetByName("Form Responses");
  var new_vals = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();

  var master_ss = SpreadsheetApp.getActiveSpreadsheet();
  var master_sheet = master_ss.getSheetByName("Sheet1");
  var lr = master_sheet.getLastRow()+1;

  var ss_real_name = "District";
  var ss_real_names = [];
  for (var i=0; i<new_vals.length; i++)
  {
    ss_real_names.push(ss_real_name);
  }

  master_sheet.getRange(lr, 1, new_vals.length).setValues(ss_real_names);
}

Is there something wrong in my code? How can I save the single string value in multiple rows?

1 Answer 1

2

Google Apps script writes values as arrays of arrays with every array inside of the outer array being a row and the elements in the inner arrays going into the columns.

If you want to write the data as rows you need to create an array filled with one element arrays. Try ss_real_names.push([ss_real_name]);.
If you wanted to write them as a column vector you could just say setValues([ss_real_names]) instead.

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

1 Comment

Yes, that was it. Thank you very much for the response!

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.