Google Spreadsheet Script Trim First X Characters Each Cell in Range

google-apps-script, google-sheets, javascript

Solution

Try the bellow pice of code. Adapt it to work with spreadsheets.

function myFunction() {
  var column = ["1 - Apples",
    "2 - Oranges",
    "3 - Bananas",
    "7 - Pineapples",
    "2 - Oranges",
    "1 - Apples",
    "9 - Cherries"];

  for(var x in column) {
    column[x] = column[x].substring(4);
    //or
    //column[x] =  column[x].split(" - ")[1]
  }

  Logger.log(column);
}

live version here.

Problem

I have a spreadsheet containing a column that looks like this: 1 - Apples 2 - Oranges 3 - Bananas 7 - Pineapples 2 - Oranges 1 - Apples 9 - Cherries ... I am trying to write a script that trims the first 4 characters from each string leaving only remaining "fruit" substring.

Original source