0

I have a column that contains a text like this on each row (text is different) - ABC1 London - ACompany: Ground Floor, Main Tower, 127 Bridge Street, London AB1 1JZ.

The script I have extracted the city name from that text and store it into a new array.

When I try to write those values into a new column I get Cannot convert Array to Object[][]. (line 18, file "Code").

I have tried to loop through that array again, but it returns 101 arrays (I have 101 rows) so when I set cities[0].length as width it pastes it on 101 column.

All I need is to get the city name from the text and paste it on the same sheet to a different column - Ex. the text is on column B12 and I want to paste just the city name into R12 - hope it makes sense.

Please see below the code I have

function getCity() {
  var ss = SpreadsheetApp.openById('ID');
  var sh = ss.getSheetByName('Response Data');
  var range = sh.getDataRange();
  var values = range.getValues();
  var cities = [];
  var newCity = []
  for (var i=1;i<values.length;i++)
  {

    var cellText = values[i][11];
    if(typeof cellText === 'number') {continue;};
    cities.push(cellText.substring(5, (cellText + " -").indexOf("-") - 1));
  }

  Logger.log(cities);

  sh.getRange(2, 18, cities.length, 1).setValues(cities);


}

Thank you!

2
  • Any errors that you are getting? Commented Sep 13, 2017 at 10:01
  • Cannot convert Array to Object[][]. (line 18, file "Code") Commented Sep 13, 2017 at 10:02

1 Answer 1

1

Try this code and see if you're getting the same error,

function getCity() {
  var ss = SpreadsheetApp.openById('ID');
  var sh = ss.getSheetByName('Response Data');
  var range = sh.getDataRange();
  var values = range.getValues();
  var cities = [];
  var newCity = []
  for (var i=1;i<values.length;i++)
  {
    var cellText = values[i][11];
    if(typeof cellText === 'number') {continue;};
    cities.push([cellText.substring(5, (cellText + " -").indexOf("-") - 1)]);
  }
  Logger.log(cities);
  sh.getRange(2,18,cities.length,1).setValues(cities);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I get Incorrect range height, was 1 but should be 101 (line 15, file "Code")
Share sample spreadsheet with sample data, there is something wrong .getRange(2, 18, cities.length, 1) with this part
It does! :) Thank you very much .... need to push more and learn arrays :)
All the best :)

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.