简体   繁体   中英

Google Spreadsheet Script Trim First X Characters Each Cell in Range

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM